Introduction to graph database neo4 and integration with Java

1. Overview of neo4j

1 Introduction

  Neo is a web--network-oriented database--that is, it's an embedded, disk-based, fully transactional Java persistence engine, but it stores structured data on the web rather than table. A network (called a graph from a mathematical point of view) is a flexible data structure that allows for a more agile and rapid development model.
  You can think of Neo as a high-performance graph engine with all the characteristics of a mature and robust database. Programmers work under an object-oriented, flexible network structure rather than rigid, static tables - but they can enjoy all the benefits of a fully transactional, enterprise-class database.

2. Features

  • The query language Neo4j CQL is similar to commonly used SQL
  • Follows the property graph data model
  • Support for indexing by using Apache Lucence
  • Support for UNIQUE constraints
  • Contains a UI for executing CQL commands: Neo4j Data Browser
  • Supports full ACID (Atomicity, Consistency, Isolation and Durability) rules
  • Using native graphics library and local GPE (Graphics Processing Engine)
  • Support query data export to JSON and XLS formats
  • Provides a REST API that can be accessed by any programming language (such as Java, Spring, Scala, etc.)
  • Provides Javascript that can be accessed through any UI MVC framework such as Node JS
  • It supports two Java APIs: Cypher API and Native Java API to develop Java applications

3. neo4j data model

3.1. The main building blocks of the graph database data model are: node, relationship, property
insert image description here

  • node (node): It is the basic unit of the graph. It contains properties with key-value pairs
    insert image description here
  • property (property): is a key-value pair used to describe graph nodes and relationships, Key = value, where Key is a string, and value can be represented by using any Neo4j data type
  • Relationship (relationship): is another main building block of graph database. It connects two nodes as shown below.
    -insert image description here
  • label: Associates a common name with a set of nodes or relationships. A node or relationship can contain one or more labels. We can create new labels for existing nodes or relationships. We can remove existing labels from existing nodes or relationships. From the previous figure, we can observe that there are two nodes. The left nodes all have a label: "EMP", and the right nodes have a label: "Dept". The relationship between these two nodes also has a label: "WORKS_FOR", and Neo4j stores the data in the properties of the node or relationship.
4. Install download

2.1 、Environmental requirements

  • Neo4j version and JVM requirements
Neo4j Version JVM compliancy
3.x Java SE 8 Platform Specificaton
4.x Java SE 11 Platform Specificaton

2.2 、mac installation steps

  • Open up your terminal/shell.
tar -xf neo4j-community-3.5.25-unix.tar.gz
  • Place the extracted files in a permanent home on your server. The top level directory is referred to as NEO4J_HOME.
    To run Neo4j as a console application, use:
<NEO4J_HOME>/bin/neo4j console
  • To run Neo4j in a background process, use:
<NEO4J_HOME>/bin/neo4j start
  • Access: http://localhost:7474, default user name neo4j, password neo4j
    2.3, docker installation
docker run -it --name my_neo4j -p 7474:7474 -p 7687:7687 --volume=/Users/luoyuan/data/docker/neo4j/data:/data neo4j:4.1.3 

#neo4j自启动
docker container update --restart=always my_neo4j

2. Introduction to Neo4j-CQL

1 Overview

 CQL stands for Cypher Query Language. Like Oracle database has query language SQL, Neo4j has CQL as query language.

2. Neo4j CQL data type
CQL data types describe
boolean Used to represent Boolean literals: true, false
byte Used to represent 8-bit integers
short Used to represent 16-bit integers
int Used to represent 32-bit integers
long Used to represent 64-bit integers
float Used to represent 32-bit floating point numbers
double Used to represent 64-bit floating point numbers
char Used to represent 16-bit characters
string used to represent strings
3. CQL common commands

(1) CREATE: Create and create nodes, relationships, and attributes
(2) MATCH: Match to retrieve data about nodes, relationships, and attributes
(3) RETURN: Return query results
(4) WHERE: Provide conditions to filter and retrieve data
(5) DELETE: DELETE deletes nodes and relationships
(6) REMOVE: removes properties of deleted nodes and relationships
(7) ORDER BY: retrieves data sorted by ...
(8) SET adds or updates tags

4. CREATE detailed explanation
  • Create a node without attributes
  • Create nodes with attributes
  • Create relationship between nodes without attributes
  • Use attributes to create relationships between nodes
  • Create single or multiple labels for nodes or relationships

 Precautions

  • Neo4j database server uses this to store this node details in Database.As Neo4j DBA or Developer, we cannot use this to access node details.
  • The Neo4j database server creates an alias as an internal node name. As Neo4j DBA or Developer, we should use this tag name to access node details.

(1) Syntax command

CREATE (<node-name>:<label-name>)

(2) Neo4j CQL creates a node without attributes

#emp 是一个节点名
#Employee 是 emp 节点的标签名称
CREATE (emp:Employee)

(3) Neo4j CQL creates a node with attributes

#dept是一个节点名
#Dept是emp节点的标签名称
#属性名称是deptno,dname,location
CREATE (dept:Dept { deptno:10,dname:"Accounting",location:"Hyderabad" })
5. Detailed explanation of MATCH
  • Get data about nodes and attributes from the database
  • Get data about nodes, relationships and properties from the database

(1) Syntax command

MATCH 
(
   <node-name>:<label-name>
)

(2) Example

# 查询Dept下的内容
MATCH (dept:Dept) return dept

# 查询Employee标签下 id=123,name="Lokesh"的节点
MATCH (p:Employee {id:123,name:"Lokesh"}) RETURN p

## 查询Employee标签下name="Lokesh"的节点,使用(where命令)
MATCH (p:Employee)
WHERE p.name = "Lokesh"
RETURN m
6. Detailed explanation of RETURN clause
  • The RETURN clause cannot be used alone. We should use either MATCH or CREATE command.

(1) Syntax command

RETURN 
   <node-name>.<property1-name>,
   ........
   <node-name>.<propertyn-name>
7.MATCH & RETURN match and return
  • Retrieve some properties of a node
  • Retrieves all properties of a node
  • Retrieve some properties of nodes and relationships
  • Retrieves all properties of nodes and relationships

(1) Syntax command

MATCH Command
RETURN Command

(2) Match query

#dept是节点名称
#Dept是一个节点标签名
#deptno是dept节点的属性名称
#dname是dept节点的属性名
#location是dept节点的属性名
MATCH (dept: Dept)
RETURN dept.deptno,dept.dname,dept.location

insert image description here

8. CREATE+MATCH+RETURN

(1) This example demonstrates how to create two nodes using attributes and relationships between these two nodes.

 We'll create two nodes: a customer node (Customer) and a credit card node (CreditCard).

 Customer node contains: ID, Name, Date of Birth attributes

 CreditCard node contains: id, number, cvv, expiredate attributes

 Customer and credit card relationship: DO_SHOPPING_WITH

 CreditCard to Customer Relationship: ASSOCIATED_WITH

  • Create client node
#e是节点名称
#在这里Customer是节点标签名称
#id,name和dob是Customer节点的属性名称

CREATE (e:Customer{id:"1001",name:"Abc",dob:"01/10/1982"})
  • Create a CreditCard node
#c是一个节点名
#CreditCard是节点标签名称
#id,number,cvv和expiredate是CreditCard节点的属性名称

CREATE (cc:CreditCard{id:"5001",number:"1234567890",cvv:"888",expiredate:"20/17"})
9. Relationship Basics

(1) Based on directionality, Neo4j relationships are divided into two main types.

  • one-way relationship
  • two-way relationship
10.CREATE create label

(1) Label is the name or identifier of a node or relationship in the Neo4j database.

  • We can refer to this label name as a relationship as a "relationship type".
  • We can use the CQL CREATE command to create a single label for a node or relationship and to create multiple labels for a node. This means that Neo4j only supports a single relationship type between two nodes.

(2) Create a single label to the node

CREATE (<node-name>:<label-name>)

(3) Create multiple tags to nodes

CREATE (<node-name>:<label-name1>:<label-name2>.....:<label-namen>)
#m是一个节点名
#Movie, Cinema, Film, Picture是m节点的多个标签名称
CREATE (m:Movie:Cinema:Film:Picture)

(4) Single label to relationship

CREATE (<node1-name>:<label1-name>)-
	[(<relationship-name>:<relationship-label-name>)]
	->(<node2-name>:<label2-name>)

(5) Create a label for the relationship

#p1和profile1是节点名称和节点标签名称“From Node”
#p2和Profile2是“To Node”的节点名称和节点标签名称
#r1是关系名称
#LIKES是一个关系标签名称
CREATE (p1:Profile1)-[r1:LIKES]->(p2:Profile2)
11. WHERE child clause

(1) Syntax command

WHERE <condition>
WHERE <condition> <boolean-operator> <condition>
MATCH (emp:Employee) 
WHERE emp.name = 'Abc' OR emp.name = 'Xyz'
RETURN emp

(2) Create a relationship using the WHERE clause

MATCH (<node1-label-name>:<node1-name>),(<node2-label-name>:<node2-name>) 
WHERE <condition>
CREATE (<node1-label-name>)-[<relationship-label-name>:<relationship-name>
       {<relationship-properties>}]->(<node2-label-name>)
MATCH (cust:Customer),(cc:CreditCard) 
WHERE cust.id = "1001" AND cc.id= "5001" 
CREATE (cust)-[r:DO_SHOPPING_WITH{shopdate:"12/12/2014",price:55000}]->(cc) 
RETURN r
12. DELETE delete
  • delete node.
  • Deletes a node and related nodes and relationships.

(1) delete node

DELETE <node-name-list>
MATCH (e: Employee) DELETE e

(2) DELETE node and relational clause syntax

DELETE <node1-name>,<node2-name>,<relationship-name>
MATCH (cc: CreditCard)-[rel]-(c:Customer) 
DELETE cc,c,rel
13. REMOVE delete

(1) Neo4j CQL REMOVE command is used for

  • Remove the label of a node or relationship
  • Delete an attribute of a node or relationship

(2) The main difference between Neo4j CQL DELETE and REMOVE commands

  • The DELETE operation is used to delete nodes and associations.
  • The REMOVE operation is used to remove tags and attributes.

(3) Similarity between Neo4j CQL DELETE and REMOVE commands

  • These two commands should not be used alone.
  • Both commands should be used with the MATCH command.

(4) Syntax command

REMOVE <property-name-list>

(5) Create a node and permanently delete the attributes of this node from the database

CREATE (book:Book {id:122,title:"Neo4j Tutorial",pages:340,price:250}) 
#类似于以下两个SQL命令在
CREATE TABLE BOOK(
	id number,
	title varchar2(20),
	pages number,
	price number
);
INSERT INTO BOOK VALUES (122,'Neo4j Tutorial',340,250);
#删除“price”属性
MATCH (book { id:122 })
REMOVE book.price
RETURN book

(6) Delete the label of the node/relationship

REMOVE <label-name-list> 
MATCH (m:Movie) 
REMOVE m:Picture
14. SET clause
  • Add a new property to an existing node or relationship
  • Add or update property values

(1) Grammar

SET  <property-name-list>
MATCH (book:Book) RETURN book

MATCH (book:Book)  SET book.title = 'superstar'   RETURN book
15.Neo4j CQL ORDER BY child clause
  • Neo4j CQL provides the "ORDER BY" clause in the MATCH command to sort the results returned by the MATCH query.
  • By default, it sorts the rows in ascending order. If we want to sort them in descending order, we need to use the DESC clause.

(1) Syntax command

ORDER BY  <property-name-list>  [DESC]	
MATCH (emp:Employee) RETURN emp.empid,emp.name,emp.salary,emp.deptno
MATCH (emp:Employee)
RETURN emp.empid,emp.name,emp.salary,emp.deptno
ORDER BY emp.name
16. UNION
  • It combines and returns common rows from two sets of results into one set of results. It does not return duplicate rows from two nodes.
  • The result column types and the names from both sets of results must match, which means the column names should be the same and the data types of the columns should be the same.
    (1) Command syntax
<MATCH Command1>
   UNION
<MATCH Command2>
MATCH (cc:CreditCard)
RETURN cc.id as id,cc.number as number,cc.name as name,
   cc.valid_from as valid_from,cc.valid_to as valid_to
UNION
MATCH (dc:DebitCard)
RETURN dc.id as id,dc.number as number,dc.name as name,
   dc.valid_from as valid_from,dc.valid_to as valid_to

(2) UNION ALL child clause

  • Combines and returns all rows from the two result sets into a single result set. It also returns duplicate rows by two nodes.
  • Result column types and names from both result sets must match, which means column names should be the same and column data types should be the same.
<MATCH Command1>
UNION ALL
<MATCH Command2>
17. LIMIT and SKIP clauses

(1) Syntax command

MATCH (emp:Employee) 
RETURN emp
LIMIT 2
MATCH (emp:Employee) 
RETURN emp
SKIP 2
18. MERGE
  • Create nodes, relationships and attributes
  • to retrieve data from the database
  • The MERGE command is a combination of the CREATE command and the MATCH command.
  • MERGE = CREATE + MATCH
  • Neo4j CQL MERGE command searches the graph for a given pattern and if it exists it returns the result, if it does not exist in the graph then it creates new node/relationship and returns the result.

(1) Syntax command

MERGE (<node-name>:<label-name>
{
   <Property1-name>:<Pro<rty1-Value>
   .....
   <Propertyn-name>:<Propertyn-Value>
})
MATCH  (gp1:GoogleProfile1) 
RETURN gp1.Id,gp1.Name
MERGE (gp2:GoogleProfile2{ Id: 201402,Name:"Nokia"})

3. Integration of neo4j and spring

Guess you like

Origin blog.csdn.net/luoyuan323/article/details/110426193