当前位置:网站首页>Neo4j - CQL use

Neo4j - CQL use

2022-06-13 09:01:00 Q z1997

3.1 Neo4j - CQL brief introduction

Neo4j Of Cypher Language is built for processing graphic data ,CQL representative Cypher query language . image Oracle The database has queries
Language SQL,Neo4j have CQL As a query language .

  • It is Neo4j Query language of graphic database .
  • It is a declarative pattern matching language
  • It follows SQL grammar .
  • Its grammar is very simple and human 、 Readable format .

 Insert picture description here
 Insert picture description here
Use cypher Language to describe relationships :

(fox)<-[:knows]-( Zhou Yu )-[:knows]->( The various ge )-[:knows]->(fox)

3.2 Common commands

https://neo4j.com/docs/cypher-manual/3.5/clauses/

LOAD CSV

# take csv copy to  
%NEO4J_HOME%\import Catalog  
load csv from 'file:/// Journey to the west .csv' as line 
create (: Westward journey  {
    name:line[0],tail:line[1],label:line[3]})

CREATE establish
create The create model statement is used to create a data model
Create nodes

# Create a simple node  
create (n) 

# Create multiple nodes  
create (n),(m) 

# Create a node with labels and attributes and return the node  
create (n:person {
    name:' buddha '}) return n

Create relationships

Neo4j The graph database follows the attribute graph model to store and manage its data .
According to the attribute graph model , The relationship should be directed . otherwise ,Neo4j An error message will be thrown .
Based on directionality ,Neo4j Relationships are divided into two main types .

  • One way relationship
  • Two way relationship
# Create a relationship with a new node  
CREATE (n:person {
    name:' Yang Jian '})-[r: The master ]->(m:person {
    name:' Immortal Yuding '}) return type(r) 

# Use known nodes to create relationships with attributes  
match (n:person {
    name:' Monk sha '}),(m:person{
    name:' Tang's monk '}) create (n)-[r:` The master `{
    relation:' The master '}]->(m) return r 

# Retrieve the details of the relationship node  
match (n:person)-[r]-(m:person) return n,m

Create full path

create p=(:person{
    name:' The Dragon King '})-[: elder foster brother ]->(:person{
    name:' But the king bull '})<-[: elder foster brother ]- (:person {
    name:' Lord Peng '}) return p

MATCH Inquire about

Neo4j CQL MATCH The command is used for

  • Get data about nodes and properties from the database
  • Get the relevant nodes from the database , Data on relationships and properties
MATCH (n:` Westward journey `) RETURN n LIMIT 25

RETURN return

Neo4j CQL RETURN Clause for

  • Retrieve some properties of the node
  • Retrieve all properties of the node
  • Retrieve some properties of nodes and associations
  • Retrieve all attributes of nodes and associations
MATCH (n:` Westward journey `) RETURN id(n),n.name,n.tail,n.relation

WHERE Clause

image SQL equally ,Neo4j CQL stay CQL MATCH The order provides WHERE Clause to filter MATCH Result of query .

MATCH (n:person) where n.name=' The Monkey King ' or n.name=' Pig eight quit ' RETURN n 

# Create relationships  
match (n:person),(m:person) where n.name=' The Monkey King ' and m.name=' Pig eight quit ' create (n)-[r: Younger martial brother ]->(m) return n.name,type(r),m.name

DELETE Delete

Neo4j Use CQL DELETE Clause

  • Delete node .
  • Delete nodes and related nodes and relationships .
#  Delete node  ( Premise : The node does not have a relationship ) 
MATCH (n:person{
    name:" White dragon horse "}) delete n 
#  Delete the relationship  
MATCH (n:person{
    name:" Monk sha "})<-[r]-(m) delete r return type(r)

REMOVE Delete

Sometimes based on client requirements , We need to add or remove attributes... To an existing node or relationship . We use Neo4j CQL REMOVE Clause to delete an existing attribute of a node or relationship .

  • Delete the label of a node or relationship
  • Delete the attributes of a node or relationship
# Delete attribute  
MATCH (n:role {
    name:"fox"}) remove n.age return n 

# Create nodes  
CREATE (m:role:person {
    name:"fox666"}) 
# Remove the label  
match (m:role:person {
    name:"fox666"}) remove m:person return m

SET Clause

Sometimes , According to our client requirements , We need to add new attributes to existing nodes or relationships . Do that ,Neo4j CQL Provides a SET Clause .

  • Add a new attribute to an existing node or relationship
  • Add or update property values
MATCH (n:role {
    name:"fox"}) set n.age=32 return n

ORDER BY Sort

Neo4j CQL stay MATCH The order provides “ORDER BY” Clause , Yes MATCH The results returned by the query are sorted . We can sort rows in ascending or descending order . By default , It sorts the rows in ascending order . If we want to sort them in descending order , We need to use DESC Clause .

MATCH (n:` Westward journey `) RETURN id(n),n.name order by id(n) desc

UNION Clause

And SQL equally ,Neo4j CQL There are two sentences , Combine two different results into a set of results

  • UNION
    It combines common rows from two sets of results and returns them to a set of results . It does not return duplicate rows from two nodes .
    Limit : The result column type and the name from both sets of results must match , This means that the column names should be the same , Columns should have the same data type .

  • UNION ALL
    It combines and returns all rows of two result sets into a single result set . It also returns lines repeated by two nodes .
    Limit : Result column type , And the names from both result sets must match , This means that the column names should be the same , The data type of the column should be the same .

MATCH (n:role) RETURN n.name as name 
UNION 
MATCH (m:person) RETURN m.name as name 

MATCH (n:role) RETURN n.name as name 
UNION all 
MATCH (m:person) RETURN m.name as name

LIMIT and SKIP Clause

Neo4j CQL Provided LIMIT Clause and SKIP To filter or limit the number of rows returned by the query .LIMIT Back to the first few lines ,SKIP Ignore the first few lines .

#  First two lines  
MATCH (n:` Westward journey `) RETURN n LIMIT 2 

#  Ignore the first two lines  
MATCH (n:person) RETURN n SKIP 2

NULL value

Neo4j CQL Treat null values as missing or undefined values for attributes of nodes or relationships .
When we create a node with an existing node label name but no attribute value specified , It will create a NULL New node for attribute value

match (n:` Westward journey `) where n.label is null return id(n),n.name,n.tail,n.label

IN The operator

And SQL equally ,Neo4j CQL Provides a IN Operator , In order to CQL The set of values provided by the command .

match (n:` Westward journey `) where n.name in[' The Monkey King ',' Tang's monk '] return id(n),n.name,n.tail,n.label

INDEX Indexes

Neo4j SQL Supports indexes on node or relationship properties , To improve application performance .
We can index the properties of all nodes with the same label name .
We can do it in MATCH or WHERE or IN Use these index columns on operators to improve CQL Command Implementation .

Neo4J Index operation

  • Create Index Create index
  • Drop Index Discard index
#  Create index  create index on :` Westward journey ` (name) 
#  Delete index  drop index on :` Westward journey ` (name)

UNIQUE constraint

stay Neo4j In the database ,CQL CREATE The command always creates a new node or relationship , This means that even if you use the same value , It will, too
Insert a new line . According to our application requirements for some nodes or relationships , We must avoid this repetition .
image SQL equally ,Neo4j The database also supports NODE or Relationship Property of UNIQUE constraint
UNIQUE The advantages of constraints

  • Avoid duplicate records .
  • Enforce data integrity rules
# Create a unique constraint  
create constraint on (n:xiyou) assert n.name is unique 
# Delete unique constraint  
drop constraint on (n:xiyou) assert n.name is unique

 Insert picture description here

DISTINCT

This function is used like SQL Medium distinct keyword , All the different values are returned .

match (n:` Westward journey `) return distinct(n.name)

3.3 Common functions

 Insert picture description here
String function
And SQL equally ,Neo4J CQL Provides a set of String function , Used in CQL Get the required results in the query .
 Insert picture description here
AGGREGATION polymerization
and SQL equally ,Neo4j CQL Provide some in RETURN Aggregate function used in clause . It is similar to SQL Medium GROUP BY
Clause .
We can use MATCH In the command RETURN + Aggregate function to process a set of nodes and return some aggregate values .
 Insert picture description here
Relational functions
Neo4j CQL Provides a set of relational functions , To get the start node at , Know the details of the relationship when ending details such as nodes .
 Insert picture description here

3.4 neo4j-admin Use

Database backup
Yes Neo4j Data backup 、 Restore 、 When migrating operations , To shut down neo4j

cd %NEO4J_HOME%/bin 
# close 
neo4j neo4j stop 
# Backup  
neo4j-admin dump --database=graph.db --to=/neo4j/backup/graph_backup.dump

Database recovery
Restore 、 Before moving , To shut down neo4j service .

# Data import  
neo4j-admin load --from=/neo4j/backup/graph_backup.dump --database=graph.db -- force 
# Restart the service  
neo4j start
原网站

版权声明
本文为[Q z1997]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/164/202206130859552933.html