Neo4j preliminary understanding and use

Introduction

Graph Database is a special existence in the NoSQL database family, used to store rich relational data. Neo4j is currently the most popular graph database and supports complete transactions. In the attribute graph, the graph is composed of vertices (Vertex), edges (Edge) and properties (Property). Both vertices and edges can set attributes. The vertices are also called nodes, and the edges are also called relations. Each node and Relationships can consist of one or more attributes. The graph created by Neo4j uses vertices and edges to construct a directed graph, and its query language cypher has become the de facto standard.

Model rules

  • Data in nodes, relationships and attributes
  • Nodes and relationships contain attributes
  • Relationship connection Nodes
  • Attributes are key-value pairs
  • Nodes are represented by circles, and relationships are represented by arrow keys.
  • Relationships have directions: one-way and two-way.
  • Each relationship contains “start node” or “from node” and “to node” or “end node”

picture description

above w3c You can also refer to the tutorial and official website

Installation

  • First install jdk, and configure the environment variables, slightly
  • First go to https://neo4j. com/product/ download, you need to register to download
1. tar -xvf compressed package
2. cd unzip the file/bin
3. neo4j install
1. pre> 

Run start

Start
sudo service neo4j start or sudo neo4j start
Close
sudo service neo4j stop or sudo neo4j stop

web access

http://localhost:7474/browser/

The initial user is neo4j, the password is neo4j, and the password needs to be changed after logging in. For the web-side tutorial, see w3c. But I Prefer to access at the command line

Command line access

Refer to https://neo4j-client.net/, the installation is as follows:

sudo add-apt- repository ppa:cleishm/neo4j
sudo apt-get update
sudo apt-get install neo4j-client libneo4j-client-dev

Login: The default port is 7687. The password is neo4j or The password you changed after logging in to the web terminal

neo4j-client -u neo4j localhost 7687
:quit //Exit

You can change it after logging in on the neo4j-client command line Password:

CALL dbms.changePass word('your new password')

Use

  • create to create a node or relationship
// format
create (node_name:lable_name
{
property1_name:property1_value
p2:v2
p3:v3
});
// node_name is a relational database The alias of the table
// label_name is similar to that indicated, indicating a type of entity

// Example
create (dog:DOG{name:"dog_name",age:20 });
  • match query
// query a certain attribute of a node
match(node_name:node_label)
where node_name .p1=v1
return node.p3 as p3

// Query the entire node
match(node_name:node_label)
where node_name.p1=v1 and/or node_name.p2>v2
return node_name

// For example
match (dog:Dog) where dog.name ='dog_name' return dog
    < li>relationship
// Add a relationship to an existing node
match (a:A),(b,B)
where a.p1=p1 and b .p2=v2 or ...
create (a)-[r:R{p3:v3,p4:v4,...}]->(b)

// Create a relationship while creating a new node, and you can even append return
create (a:A{...})-[r:R{...}]->(b:B{...} ) return r

// Query relationship
m atch (a:A)-[r:R]->(b:B)
where a.p1=v1 or r.p2=v2 and b.p3=v3
return r
  • label A node has multiple labels
create (a:A:B...) ...

// match
match (a:A:B) will return the node whose label is both A and B
match(a:A) will return A and also A:B, that is, the node whose label contains A
  • delete delete a node or relationship. Before deleting a node, you must delete its related relationship.
match (a:A) where a.p1=v1 delete a< br />
match (a:A) where a.p1=v1 delete a.p1

match (a:A) delete a

// delete All R relationships between A\B
match (a:A)-[r:R]->(b:B) delete r

// delete the relationship and node at the same time< br />match (a:A)-[r:R]->(b:B) where a.p1=v1 delete a,b,r
  • remove remove node or relationship You can add return
The syntax is basically the same as delete
match (a:A) where ... remove a.p1 return ...
< br />match (a:A)-[r:R]->(b:B) where ... remove r.p2
  • set add or modify attributes
  • < /ul>

    match (a:B) where ... set a.p1=v1
    • order by returns the results in order of one or more attribute values, the default is in ascending order, In descending order, add DESC after the order list
    match (a:A) where ... return a.p1, a.p2, a.p3 order by a.p1,a.p2  
    • The union connection returns the result (the name must be the same), and the duplicate rows are removed. Note that the connection here is not the same as the relational database, here is just splicing the result lists returned by the two return statements.
    match (a:A) return a.p1 as p1, a.p2 as p2
    union
    match (b:B) return b.p1 as p1, b. p3 as p2

    union all Same as above, except that duplicate rows are not removed

    • limit limits the number of returned results
    match (a: A) return a limit 10 // return the first 10 results
    • skip skip the number of previous results
    match (a:A) return a skip 10 // Across the top ten results
    • The merge command searches for a given pattern in the graph, if it exists, it returns the result, if it does not exist in the graph, it creates a new node/ Relationship and return the result.
    merge (a:A{p1:v1})-[r:R{...}]->(b:B{...})
    < br />Equivalent to
    match (a:A)-[r:R]->(b:B) where a.p1=v1 and r... and b.... return a,r, b
    If a or b or r does not exist, just create
    • null If the return attribute does not exist, it will return a null as the value of the attribute
    • match (a:A) where a.name is not null // Query nodes whose name value is not null, that is, nodes with name attributes
      • in Determine whether the attribute value is In the list
      match (a:A) where a.name in ['given','zeng']
      • id "Id" is the node The default internal property of the relationship and /home/zjw/Pictures/neo4j_import_relationship.png. This means that when we create a new node or relationship, the Neo4j database server will assign a number for internal use. It will automatically increment. The maximum value is about 3.5 billion
      • upper converts uppercase letters. The same goes for lower
      match (a:A) where ... return upper(a.p1) as p1
      • substring It accepts a string as input and two indexes: one is the beginning of the index, the other is the end of the index, and returns the substring from StartInded to EndIndex-1.
      match (a:A) where ... return substring(a.p1,0,10) as p1
      • aggregate countmaxsumminavg
      match (a:A) where ... return count(*)

      return max(a.age), min(a.age), avg(a.age ), sum(a.age)
      • Relation function

        • startnode Get the starting node of the relationship
        • endnode The end of the relationship Node
        • id id of the relationship
        • type type of the relationship type
      MATCH (a)-[movie :ACTION_MOVIES]->(b)
      RETURN STARTNODE(movie)
      • Index
      create index on a:A (p1)
      create index on :A (p1)
      drop index on :A (p1)
      • unique
      create constraint on (a: A) assert a.p1 is unique
      • Other
      where exists (a.name) //Node exists attributes
      where n.name contains'giv' // The attribute contains
      where a.name starts with'g' // The beginning of the attribute
      where a.name ends with'n' //The end of the attribute
      where n.name =~'.*ive.*' // Use regular expressions

      Import data

      csv format: the first line is the field name, followed by the value of each field p>

      p1,p2
      v1,v2
      v1,v2
      ...
      l oad csv with headers from "file path" as line
      merge (a:A{p1:line.p1, p2:line.2})

      Official document

Leave a Comment

Your email address will not be published.