1. Introduction
1.1 Commonly used commands and functions
Cypher query language is also called CQL.
Commonly used commands are as follows:
CQL command | effect | usage |
---|---|---|
CREATE | create | Create nodes, relationships and attributes |
MATCH | match | Retrieve data about nodes, relationships and properties |
RETURN | return | return query result |
WHERE | where | Provide conditions to filter and retrieve data |
DELETE | delete | Delete nodes and relationships |
REMOVE | remove | Delete attributes of nodes and relationships |
ORDER BY | sorted by | Sort retrieved data |
SET | Group | Add or update tags |
The functions commonly used in CQL are as follows:
Custom List Function | usage | |
---|---|---|
String | string | They are used to use String literals |
Aggregation | polymerization | They are used to perform some aggregate operations on CQL query results |
Relationship | relation | They are used to get the details of the relationship, such as startnode, endnode, etc. |
2. Description of commonly used commands
2.1 CREATE command
2.1.1 Create a node without attributes
node-name is the name of the node, and label-name is the node label name.
CREATE (<node-name>:<label-name>)
2.1.2 Create a node with attributes
Propertyn-name is the name of the created node property, and Propertyn-Value is the value of the created node property.
CREATE (
<node-name>:<label-name>
{
<Property1-name>:<Property1-Value>
........
<Propertyn-name>:<Propertyn-Value>
}
)
2.2 MATCH command
The MATCH command is used to fetch data about nodes, relationships and attributes from the database.
MATCH
(
<node-name>:<label-name>
)
The MATCH command is usually used together with the RETURN command.
2.3 RETURN command
The RETURN command is used to retrieve properties of nodes and associations.
RETURN
<node-name>.<property1-name>,
........
<node-name>.<propertyn-name>
Examples of MATCH and RETURN commands:
CREATE (t: test {name:"xzw", sex:"m", loc: "QD"})
MATCH (t: test)
RETURN t.name,t.sex
2.4 Relationship
Based on directionality, Neo4j relationships are divided into two main types: unidirectional and bidirectional.
2.4.1 Create a relationship without attributes
relationship-name is the name of the relationship, and relationship-label-name is the label name of the relationship. The return clause in the above syntax is not required.
CREATE
(<node1-label-name>:<node1-name>)-
[<relationship-label-name>:<relationship-name>]->
(<node1-label-name>:<node1-name>)
RETURN <relationship-label-name>
Example relationship:
CREATE (f1:Favorite1)-
[like:LIKES]->
(f2:Favorite2)
RETURN like
2.4.2 Create a relationship with attributes
CREATE
(<node1-label-name>:<node1-name>{<define-properties-list>})-
[<relationship-label-name>:<relationship-name>{<define-properties-list>}]
->(<node1-label-name>:<node1-name>{<define-properties-list>})
RETURN <relationship-label-name>
CREATE
(v1:Video1{title:"Action1",updated_by:"A",uploaded_date:"10/10/2020"})
-
[movie:ACTION_MOVIES{rating:1}]
->(v2:Video2{title:"Action2",updated_by:"X",uploaded_date:"12/12/2020"})
MATCH (v1:Video1)-[movie:ACTION_MOVIES]->(v2:Video2)
RETURN movie
2.5.1 Boolean operators
boolean operator | describe |
---|---|
AND | It is a Neo4j CQL keyword that supports AND operations |
OR | It is a Neo4j CQL keyword to support OR operation |
NOT | It is a Neo4j CQL keyword to support NOT operation |
XOR | It is a Neo4j CQL keyword that supports XOR operation |
2.5.2 Comparison operators
boolean operator | describe |
---|---|
= | It is the Neo4j CQL "equal to" operator |
<> | It is a Neo4j CQL "not equal to" operator |
< | It is a Neo4j CQL "less than" operator |
> | It is a Neo4j CQL "greater than" operator |
<= | It is a Neo4j CQL "not equal to or equal to" operator |
>= | It is a Neo4j CQL "greater than or equal to" operator |
2.5.3 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>)
Example:
MATCH (c:Customer),(f:Fav)
WHERE c.id = "1" AND f.id= "1"
CREATE (c)-[r:Relat{location:"QD"}]->(f)
RETURN r
2.6 DELETE command
Deletes a node and related nodes and relationships.
2.6.1 Delete Node
DELETE <node-name-list>
Example:
MATCH (p:People) DELETE p
2.6.2 Delete nodes and relationships
DELETE <node1-name>,<node2-name>,<relationship-name>
You can delete all nodes and relationships in the database with the following command:
MATCH (n)
OPTIONAL MATCH (n)-[r]-()
DELETE n,r
2.7 REMOVECommand
Remove an attribute or label from an existing node or relationship. It is worth noting that both the DELETE and REMOVE commands should be used with the MATCH command.
REMOVE <property-name-list>/<label-name-list>
其中,<property-name-list>是一个属性列表,其用法如下:
<node-name>.<property1-name>,
<node-name>.<property2-name>,
....
<node-name>.<propertyn-name>
<label-name-list>它是一个标签列表,其用法如下:
<node-name>:<label2-name>,
....
<node-name>:<labeln-name>
2.8 SET command
The SET command is used to add or update attribute values to existing nodes or relationships
SET <property-name-list>
其中,<property-name-list>是一个属性列表,其语法如下:
<node-label-name>.<property1-name>,
<node-label-name>.<property2-name>,
....
<node-label-name>.<propertyn-name>
2.9 ORDER BY command
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.
ORDER BY <property-name-list> [DESC]
其中,<property-name-list>是属性列表,其语法如下:
<node-label-name>.<property1-name>,
<node-label-name>.<property2-name>,
....
<node-label-name>.<propertyn-name>
2.10 UNION command
It combines and returns common rows from both sets of results into one set of results, it does not return duplicate rows from two nodes. Similar to SQL, UNION ALL returns all data.
<MATCH Command1>
UNION
<MATCH Command2>
2.11 LIMIT and SKIP commands
LIMIT command to filter or limit the number of rows returned by a query, it trims the results at the bottom of the CQL query result set.
LIMIT <number>
-- 如果我们要修整CQL查询结果集顶部的结果,那么我们应该使用SKIP命令。--
SKIP <number>
2.12 MERGE command
The MERGE command is a combination of the CREATE command and the MATCH command. It searches the graph for a given pattern and if it exists it returns the result, if it doesn't exist in the graph then it creates new node/relationship and returns the result.
MERGE (<node-name>:<label-name>
{
<Property1-name>:<Pro<rty1-Value>
.....
<Propertyn-name>:<Propertyn-Value>
})
2.13 NULL values
Neo4j CQL treats null values as missing or undefined values for properties of nodes or relationships. When we create a node with an existing node label name but without specifying its property value, it will create a new node with NULL property value.
2.14 IN command
Like SQL, Neo4j CQL provides an IN operator to provide a collection of values to a CQL command.
IN[<Collection-of-values>]
3. Description of commonly used functions
3.1 String functions
Like SQL, Neo4J CQL provides a set of String functions for obtaining the desired results in CQL queries. The list of commonly used string functions is as follows:
Function | describe |
---|---|
UPPER | It is used to change all letters to uppercase |
LOWER | It is used to change all letters to lowercase |
SUBSTRING | It is used to get the substring of the given String |
REPLACE | It is used to replace a substring of a string |
3.1.1 UPPER function
Convert letters to uppercase, the syntax is:
UPPER (<input-string>)
where, can be a property name of a node or relationship from a Neo4J database.
3.1.2 LOWER function
Convert letters to lowercase, the syntax is:
LOWER (<input-string>)
3.1.3 SUBSTRING function
String interception function, the interception range is left closed and right open, the syntax is as follows:
SUBSTRING(<input-string>,<startIndex> ,<endIndex>)
3.2 AGGREGATION aggregate function
Neo4j CQL provides some aggregate functions used in the RETURN clause, which is similar to the GROUP BY clause in SQL.
aggregation function | describe |
---|---|
COUNT | It returns the number of rows returned by the MATCH command |
MAX | It returns the maximum value from a set of rows returned by the MATCH command |
MIN | It returns the minimum value of a set of rows returned by the MATCH command |
SUM | It returns the sum of all rows returned by the MATCH command |
AVG | It returns the average of all rows returned by the MATCH command |
3.2.1 COUNT function
It takes the result from the MATCH clause and counts the number of rows present in the result and returns that count. Its syntax is as follows:
COUNT(<value>)
3.2.2 MAX function
The maximum value function, the syntax is:
MAX(<property-name>)
3.2.3 MIN function
The minimum function, the syntax is:
MIN(<property-name>)
3.2.4 AVG function
Average function, the syntax is:
AVG(<property-name>)
3.2.5 SUM function
Sum function, the syntax is:
SUM(<property-name>)
3.3 Relational functions
Neo4j CQL provides a set of relationship functions to know the details of a relationship while getting details like start node, end node, etc.
Function | describe |
---|---|
STARTNODE | It is used to know the start node of the relationship |
ENDNODE | It is used to know the end node of the relationship |
ID | It is used to know the ID of the relationship |
TYPE | It is used to know the TYPE of a relation in the string representation |
Fourth, cypher imports csv type data
4.1 Brief steps
-
Make sure you have created the appropriate node and relationship types in your Neo4j database.
-
Have your CSV file ready. Put the csv file to be imported into the import directory:
- Make sure the columns in the file match the node and relationship type properties.
- 如果导入的csv文件中有中文,需要先将格式转换成UTF-8格式,否则会乱码
- 用NotePad++,editplus等文本编辑器打开csv文件,然后另存为,选择UTF-8编码,就可以了。
-
导入实体:
-
name.csv
charatername azula aang zuko ...
##导入实体 LOAD CSV WITH HEADERS FROM "file:///name.csv" AS row merge (d:name{name:row.charatername})
-
nation.csv
nationname firenation watertribe ...
##导入实体 LOAD CSV WITH HEADERS FROM "file:///nation.csv" AS row merge (d:nation{name:row.nationname})
-
-
导入关系:relation.csv
name relation nation azula include firenation zuko include firenation katara include watertribe ##导入关系第一种方法: LOAD CSV WITH HEADERS FROM "file:///relation.csv" AS row match (from:name{name:row.charatername}),(to:nation{name:row.nationname}) merge (from)-[r:relation{property:row.relation}]->(to)
-
运行Cypher查询以导入数据
4.2 高级步骤
CSV 文件首行是字段名,对于 neo4j 来说,其实是属性对应的列,假设我们有如下 CSV 实体数据文件:
Id,Name,Location,Email,BusinessType
1,Neo4j,San Mateo,[email protected],P
2,AAA,,[email protected],
3,BBB,Chicago,,G
由于 CSV 文件首行是字段名,则要加上 WITH HEADERS,并通过 MERGE 定义真实要设置的属性名和属性值,示例如下:
// 跳过 NULL 值
LOAD CSV WITH HEADERS FROM 'file:///companies.csv' AS row
WITH row WHERE row.Id IS NOT NULL
MERGE (c:Company {companyId: row.Id});
// clear data
MATCH (n:Company) DELETE n;
// 为 NULL 值设置默认值
LOAD CSV WITH HEADERS FROM 'file:///companies.csv' AS row
MERGE (c:Company {companyId: row.Id, hqLocation: coalesce(row.Location, "Unknown")})
// clear data
MATCH (n:Company) DELETE n;
// 设置空字符串为 NULL 值
LOAD CSV WITH HEADERS FROM 'file:///companies.csv' AS row
MERGE (c:Company {companyId: row.Id})
SET c.emailAddress = CASE trim(row.Email) WHEN "" THEN null ELSE row.Email END
如果 CSV 中有一个字段是要拆分的 item 列表,则可以使用 Cypher 的 split()
函数来分隔单元格中的数组。
条件转换可以通过 CASE
:
// clear data
MATCH (n:Company) DELETE n;
//set businessType property based on shortened value in CSV
LOAD CSV WITH HEADERS FROM 'file:///companies.csv' AS row
WITH row WHERE row.Id IS NOT NULL
WITH row,
(CASE row.BusinessType
WHEN 'P' THEN 'Public'
WHEN 'R' THEN 'Private'
WHEN 'G' THEN 'Government'
ELSE 'Other' END) AS type
MERGE (c:Company {companyId: row.Id, hqLocation: coalesce(row.Location, "Unknown")})
SET c.emailAddress = CASE trim(row.Email) WHEN "" THEN null ELSE row.Email END
SET c.businessType = type
RETURN *