This is the 28th day of my participation in Gwen Challenge

What is a secondary

Neo4j is a very popular graph database. Similar to graph theory, it has three main concepts:

  • node
  • Relationship between
  • attribute

Nodes and relationships both contain properties, while relationships connect nodes and can be one-way or bidirectional. On the intuitive page of Neo4j, nodes are represented by circles, and relationships are represented by lines and key headers.

Advantages and disadvantages of Neo4j

Advantages:

  • It is easy to store large amounts of connection data
  • Retrieving data is also fast and the results are intuitive
  • Easy to get started, CQL language readable
  • If the amount of data is no more than ten million, the performance can be guaranteed

Disadvantages:

  • Can only be standalone version, if there is massive data, can not do distributed
  • Graph data structure leads to poor write performance and lagging real-time read and write

Secondary purposes

Common for the following purposes:

  • The social network
  • Recommendation system
  • Fraud analysis
  • Web security (spam, etc.)

Secondary installation

It’s very simple. Go to the official website and solve it yourself: neo4j.com

CQL

CQL is the query language of Neo4j graphics database. The following is an example to explain the main commands.

The CREATE command

The CREATE command provides the following functions:

  • Create a node
  • Create a relationship
  • Create labels for nodes or relationships

Create a node with the name DEPT, tag dept, and attribute dname and location. Dept is just a variable that can be named D or some other string, not an attribute of the node:

CREATE (dept:Dept { dname:"qjfy",location:"hky" })
Copy the code

Create a node named EMp, labeled Employee, and with two attributes, name and dname. Emp is just a variable, which can be named e or some other string, and is not an attribute of the node:

CREATE (emp:Employee{name:"wys",dname:"qjfy"})
Copy the code

Create an attribute free node named emp1 with labels Employee and Actor. Emp1 is just a variable like thing, which can be named E1 or some other string and is not an attribute of the node:

CREATE (emp1: Employee:Actor)
Copy the code

MATCH command

The MATCH command provides the following functions:

  • Gets data for nodes, relationships, and attributes

Generally MATCH command need and RETURN command used in combination, MATCH command cannot be used independently, or complains. Neo ClientError. Statement. SyntaxError.

The RETURN command

The RETURN command provides the following functions:

  • Retrieves the properties of the node
  • Retrieves properties of nodes and associations

Also, generally MATCH command need and RETURN command used in combination, can’t use the RETURN command alone, otherwise an error Neo. ClientError. Statement. SyntaxError.

The MATCH & RETURN command

The MATCH & RETURN command provides the following functions:

  • Retrieves the properties of the node
  • Retrieves properties of nodes and associations

Return the attributes dname and Location for all nodes labeled Dept. Because there is only one node, only one row of data is returned. The dept is like the object name and can be replaced with any other string, such as d:

MATCH (dept: Dept) RETURN dept.dname,dept.location
Copy the code

Return result:

dept.dname dept.location
“qjfy” “hky”

Or:

MATCH (d: Dept) RETURN d.dname,d.location
Copy the code

Return result:

d.dname d.location
“qjfy” “hky”

When no return attribute is specified, the graph structure is displayed, as shown in the following figure. The extra attribute is set internally in the database, and is similar to the id in mysql:

MATCH (dept: Dept) RETURN dept
Copy the code

The ID attribute

Is the default internal property for nodes and relationships. When a new node or relationship is created, it is assigned a number, which is automatically incremented if other new nodes or relationships are created. The ID cap is about 3.5 billion.

Create a relationship

Now that we have the Dept and Employee nodes, create a join relationship between them with no attributes and label WORK_AT and name r:

MATCH (e: Employee),(d: Dept) CREATE (e)-[r:WORK_AT]->(d) 
Copy the code

Then use the query statement to view the relationship:

MATCH (e)-[r:WORK_AT]->(d) RETURN r
Copy the code

Start = Employee (id 5026) and end = Dept (ID 5024);

{
  "identity": 14452,
  "start": 5026,
  "end": 5024,
  "type": "WORK_AT",
  "properties": {

  }
}
Copy the code

2. We now create a relationship between two nodes with a sal attribute:

MATCH (e: Employee),(d: Dept) CREATE (e)-[r:WORK_AT{sal:1000}]->(d) 
Copy the code

Then use the query statement to view the relationship:

MATCH  (e)-[r:WORK_AT{sal:1000}]->(d) return r
Copy the code

Results:

{
  "identity": 14453,
  "start": 5026,
  "end": 5024,
  "type": "WORK_AT",
  "properties": {
  "sal": 1000
  }
}
Copy the code

Create two nodes and relationships that do not exist in the database without attributes:

CREATE (w1:WeChatUser)-[like:LIKES]->(w2:WeChatUser) 
Copy the code

After successful creation, use the query statement to view the relationship:

MATCH (w1)-[like:LIKES]->(w2) RETURN like
Copy the code

In the command output, start is the WeChatUser node whose ID is 5027, and end is the WeChatUser node whose ID is 5028.

{
  "identity": 14454,
  "start": 5027,
  "end": 5028,
  "type": "LIKES",
  "properties": {

  }
}
Copy the code

Create two nodes and relationships with attributes that do not exist in the database:

CREATE (w1:WeChatUser{name:"wys"})-[like:LIKES{rating:100}]->(w2:WeChatUser{name:"ppy"}) 
Copy the code

After successful creation, use the query statement to view the relationship:

MATCH (w1)-[like:LIKES{rating:100}]->(w2) RETURN like
Copy the code

In the command output, start is the WeChatUser node whose ID is 5029, and end is the WeChatUser node whose ID is 5030.

{
  "identity": 14455,
  "start": 5029,
  "end": 5030,
  "type": "LIKES",
  "properties": {
  "rating": 100
  }
}	
Copy the code

Select * from ‘LIKES’; select * from ‘LIKES’;

MATCH (a)-[r:LIKES]->(b) RETURN a,b
Copy the code

The two nodes with no content in the result are the two nodes we created directly in Step 3 without using attributes. The two nodes whose content is PPY and Wys are the ones we created using properties in Step 4.

WHERE the command

Similar to SQL, the WHERE command filters the results of a MATCH query.

Select Employee from Employee whose name is wys or whose dname is qjfy.

MATCH (e:Employee) WHERE e.name = 'wys' OR e.dname = 'qjfy' RETURN e
Copy the code

The DELETE command

The DELETE command provides the following functions:

  • Delete a node and its attributes
  • Delete the relationship between nodes

Delete all nodes of the existing Employee tag (provided there are no other relational connections on these nodes, otherwise an error will be reported) :

MATCH (e: Employee) DELETE e
Copy the code

Delete all existing LIKE relationships and their associated nodes:

MATCH (a:WeChatUser)-[r]->(b:WeChatUser) DELETE a,r,b
Copy the code

REMOVE the command

The REMOVE command provides the following functions:

  • Removes attributes of nodes or relationships
  • Remove the label of a node or relationship

1. Now create a node with attributes:

CREATE (emp:Employee{name:"wys",dname:"qjfy"})
Copy the code

Remove the dname attribute:

MATCH (emp:Employee { name:"wys" }) REMOVE emp.dname RETURN emp
Copy the code

Create a node with multiple tags:

CREATE (emp:Employee:Actor)
Copy the code

Remove the Actor tag from the node:

MATCH (e: Employee)  REMOVE e: Actor
Copy the code

The SET command

The SET command provides the following functions:

  • Adds new attributes to an existing node or relationship
  • Add or update property values

Now create a node:

CREATE (emp:Employee{name:"wys"})
Copy the code

Start by adding a property age and set the value to 27:

MATCH (e: Employee) WHERE e.name="wys" SET e.age = '27' RETURN e
Copy the code

The ORDER BY the command

Similar to SQL, the ORDER BY command sorts the results returned BY a MATCH query.

Create an Employee node with age 28:

CREATE (emp:Employee{name:"wyt",age:'28'})
Copy the code

Sort Employee nodes by age, default ascending order:

MATCH (e:Employee) RETURN e ORDER BY e.age
Copy the code

LIMIT and SKIP commands

Similar to SQL, the LIMIT command limits the number of rows returned by the query, and SKIP filters out the first few rows returned by the query.

Only one Employee node is returned:

MATCH (emp:Employee) RETURN emp LIMIT 1
Copy the code

Skip the first result and return the second and subsequent results:

MATCH (emp:Employee) RETURN emp SKIP 1
Copy the code

IN the command

Similar to SQL, the IN command allows multiple values to be specified IN a WHERE clause.

Return only the node named wys:

MATCH (e:Employee)  WHERE e.name IN ['wys'] RETURN e
Copy the code

A NULL value

NULL A missing or undefined value for an attribute of a node or relationship.

Return the node where the age attribute of the Employee tag is empty:

MATCH (e:Employee)  WHERE e.age IS NULL RETURN e
Copy the code

The MERGE command

MERGE Is similar to the combination of the CREATE command and the MATCH command. It provides the following functions:

  • Create nodes, relationships, or properties
  • Retrieve data from the database

If we repeat this twice with CREATE, since the CREATE command always adds new nodes to the database, two identical nodes are created, but if we repeat the following statement twice, only one node is created:

MERGE (e: Employee{ name:"wangda"})
Copy the code

So the MERGE command checks whether the node exists in the database. Create a new node if it does not exist. Otherwise it doesn’t create a new one.

The UNION command

There are two main UNION commands:

  • UNION, which combines common rows from two sets of results and returns them to one set of results. It does not return duplicate rows between the two nodes. The result column name and data type should be the same.

The name column is returned with no duplicate rows:

MATCH (a:Employee) return a.name as name UNION MATCH (b:Actor) return b.name as name
Copy the code
  • UNION ALL, which combines and returns ALL rows of the two result sets to form a single result set. It returns repeated rows in both nodes. As above, the column name and data type should be the same.

The name column in the returned result has duplicate rows:

MATCH (a:Employee) return a.name as name UNION ALL MATCH (b:Actor) return b.name as name
Copy the code