neo4j graph database

Table of Contents

  • 1. Introduction to neo4j
    • 1.1 What is a graph database
    • 1.2 What is neo4j
    • 1.3 Features of neo4j
    • 1.4 Advantages of neo4j
    • 1.5 The construction elements of neo4j
  • 2. Installation and deployment
    • 2.1 Environmental Description
    • 2.2 Download the installation package
    • 2.3 Unzip the installation package
    • 2.4 Configure and install jdk environment
    • 2.5 Configure neoj4 global variables
    • 2.6 Modify the neo4j configuration file
    • 2.7 Basic operation of the service
    • 2.8 Test Access
  • 3. Use DBeaver to connect to neo4j
  • 4. Basic operation of neo4j graph database
    • 4.1 Adding nodes
    • 4.2 Query
    • 4.3 Add relationship
    • 4.4 Modifications
    • 4.5 delete

1. Introduction to neo4j

1.1 What is a graph database

Graph database is a NoSQL database based on graph theory. Its data storage structure and data query methods are based on graph theory (it takes graph as the research object). Based on graphs composed of lines), graph databases are mainly used to store more connected data.

1.2 What is neo4j

Neo4j is an open source NoSQL graph database, developed in 2003, using scala and java languages, and released in 2007.

? It is one of the most advanced graph databases in the world, providing native graph data storage, retrieval and processing;
? Use the Property graph model to greatly improve and enrich the graph data model;
? Exclusive query language Cypher, intuitive and efficient;

1.3 Features of neo4j

? SQL is like a simple query language Neo4j CQL
? It follows the property graph data model
? It supports indexing by using Apache Lucence
? It supports UNIQUE constraints
? It contains a UI for executing CQL commands: Neo4j Data Browser
? It supports full ACID (Atomicity, Consistency, Isolation and Durability) rules
? It uses native graphics library and local GPE (Graphics Processing Engine)
? It supports query data export to JSON and XLS formats
? It provides REST API, which can be accessed by any programming language (such as Java, Spring, Scala, etc.)
? It provides Java scripts that can be accessed through any UI MVC framework like Node JS
? It supports two Java APIs: Cypher API and Native Java API to develop Java applications

Advantages of 1.4neo4j

? It easily represents connected data
? Retrieving/traversing/navigating more connected data is very easy and fast
? It represents semi-structured data very easily
? Neo4j CQL query language commands are human readable format, very easy to learn
? Use a simple yet powerful data model
? It does not require complex joins to retrieve connected/related data as it easily retrieves its neighbor nodes or relationship details without joins or indexes

1.5 The construction elements of neo4j

The neo4j graph database mainly has the following construction elements:

Node, attribute, relationship, label, data browser
Node
A node (Node) is a basic element in a graph database, used to represent an entity record, just like a record in a relational database. Nodes in Neo4j can contain multiple properties (Property) and multiple labels (Label).

? Node is the main data element
? Nodes are connected to other nodes through relationships
? A node can have one or more attributes (that is, attributes stored as key/value pairs)
? A node has one or more labels that describe its role in the graph

Attributes
Properties are key-value pairs used to describe graph nodes and relationships. where Key is a string and value can be obtained by using any
Neo4j data types to represent
? properties are named values where the name (or key) is a string
? Attributes can be indexed and constrained
? Composite indexes can be created from multiple attributes

Relationship
Relationships are also the basic elements of graph databases. When the nodes already exist in the database, the nodes need to be connected
Based on directionality, Neo4j relationships are divided into two main types:
? One-way relationship
? Two-way relationship

Label
Labels associate a common name with a set of nodes or relationships, which can contain one or more labels. We can create new labels for existing nodes or relationships, and we can remove labels from existing nodes or relationships.
? labels are used to group nodes
? A node can have multiple labels
? Labels are indexed to speed up finding nodes in the graph
? Native tab index optimized for speed

Neo4j Browser
Once we install Neo4j, we can access the Neo4j Data Browser

2. Installation and deployment

2.1 Environmental Description

System version:
[root@localhost bin]# cat /etc/redhat-release
CentOS Linux release 7.8.2003 (Core)

Software version:
neo4j-community-3.5.28-unix.tar.gz

2.2 Download the installation package

[root@localhost src]# curl -O http://dist.neo4j.org/neo4j-community-3.5.28-unix.tar.gz
[root@localhost src]# ls
debug kernels neo4j-community-3.5.28-unix.tar.gz

2.3 Unzip the installation package

[root@localhost src]# tar xf neo4j-community-3.5.28-unix.tar.gz -C /usr/local/

2.4 Configure and install jdk environment

Download or upload the jdk installation package with xftp:
[root@localhost src]# ls
debug jdk-8u131-linux-x64.tar.gz kernels neo4j-community-3.5.28-unix.tar.gz

decompress
[root@localhost src]# tar xf jdk-8u131-linux-x64.tar.gz -C /usr/local/
[root@localhost src]# cd /usr/local/
[root@localhost local]# ls
bin etc games include jdk1.8.0_131 lib lib64 libexec neo4j-community-3.5.28 sbin share src

[root@localhost local]# mv jdk1.8.0_131/java
[root@localhost local]# vim /etc/profile
add at the end
#JAVA_HOME
export JAVA_HOME=/usr/local/java
#JRE_HOME
export JRE_HOME=/usr/local/java/jre
#CALSSPATH
export CLASSPATH=$CLASSPATH:${JAVA_HOME}/lib:${JRE_HOME}/lib
#PATH
export PATH=$PATH:${JAVA_HOME}/bin:${JRE_HOME}/bin

Reload the configuration file and check the java version
[root@localhost local]# source /etc/profile
[root@localhost local]# java -version
java version "1.8.0_131"

2.5 Configure neoj4 global variables

[root@localhost src]# vim /etc/profile
add at the end
export NEO4J_HOME=/usr/local/neo4j-community-3.5.28
export PATH=$PATH:$NEO4J_HOME/bin
[root@localhost src]# source /etc/profile
[root@localhost ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/java/bin:/usr/local/java/jre/bin:/ usr/local/java/bin:/usr/local/java/jre/bin:/usr/local/java/bin:/usr/local/java/jre/bin:/usr/local/neo4j-community-3.5. 28/bin

2.6 Modify neo4j configuration file

[root@localhost src]# cd /usr/local/neo4j-community-3.5.28/conf/
[root@localhost conf]# vim neo4j.conf
Modify the corresponding configuration as follows

Modify line 22, add a # in front, you can read files from any path
22 #dbms.directories.import=import

Modify lines 35 and 36 to set jvm initial heap memory and jvm maximum heap memory
The maximum memory of the jvm given by the production environment is as large as possible, but it should be smaller than the physical memory of the machine
35 dbms.memory.heap.initial_size=512m
36 dbms.memory.heap.max_size=512m

Modify line 46, you can think of this as a cache, if the machine configuration is high, the bigger the better
46 dbms.memory.pagecache.size=10g

Modify line 54, remove #, you can remotely access the neo4j database through ip
54 dbms.connectors.default_listen_address=0.0.0.0

The default bolt port is 7687, the http port is 7474, and the https gateway is 7473. It is also possible not to modify the following 3 items
Modify line 71, remove #, set the http port to 7687, the port can be customized, as long as it does not conflict with other ports
71 #dbms.connector.bolt.listen_address=:7687
75 #dbms.connector.http.listen_address=:7474
79 #dbms.connector.https.listen_address=:7473

Modify line 227, remove #, allow to load csv from remote url
245 dbms.security.allow_csv_import_from_file_urls=true

Modify line 254 and set neo4j to be readable and writable
265 dbms.read_only=false

2.7 Basic service operations

start:
[root@localhost ~]# neo4j start

Check running status
[root@localhost ~]# neo4j status

service stop
[root@localhost ~]# neo4j stop

service restart
[root@localhost ~]# neo4j restart

2.8 Test Access

Browser access:
192.168.5.56:7474/browser/

The initial username and password are neo4j

You will be prompted to change your password after logging in

3. Use DBeaver to connect to neo4j

Enter ip, username, password

Test connection succeeded

4. Basic operation of neo4j graph database

First, we open the browser console of Neo4j (http://xxx.xxx.xxx.xxx:7474/browser), the user name is neo4j, and the default password is also neo4j. If you already have the password, then enter the one you modified Password will do. After logging in, we will see the following interface.

4.1 Adding nodes

Neo4j uses the create command to add, which is similar to insert in MySQL.

Create a student node (only the node has no attributes)
create (s:Student)


This means that we have created the student node

grammar format:
create (<node-name>:<label-name>)

? node-name: it is the node name we want to create
? label-name: it is the name of the label we want to create

Create a student node (create a node with attributes)

Create a student node with id10000, name Zhang San, age 18, gender male
create (s:Student{<!-- -->id:10000, name:"Zhang San",age:18,sex:1})

After execution, you will see the following results:

This means that we have created a node with four attributes: id, name, age, and sex.

It is not difficult to understand that id, name, age, and sex are similar to the fields in our MySQL tables.

The syntax for creating a node with attributes is as follows:

create (<node-name>:<label-name> {<!-- -->
    <property1-name>:<property1-value>,
    <property2-name>:<property2-value>,
    ...,
    <property3-name>:<property3-value>
})

property1-name is the property name, and property1-Value is the property value.

4.2 Query

We created nodes without attributes and nodes with attributes in the previous step, so the question is, how do we check it? query~

Neo4j uses the match … return … command to query, which is similar to the select in MySQL.
We query the node information we just created.

Query all students
match (s:Student) return s


The two nodes are displayed in the form of a graph, and we can also switch the Graph (graph), Table (table), Text (text) on the left to display in different forms.

Query all or some fields

You only need to splice the fields to be displayed with node name + dot + attribute field, as follows:
match (s:Student) return s.id,s.name,s.age,s.sex

Query the information of students whose age is equal to 18
match (s:Student) where s.age=18 return s.id,s.name,s.age,s.sex

Query all boys (sex=1) and sort them in reverse order by age
match (s:Student) where s.sex=1 return s.id,s.name,s.age,s.sex order by s.age desc


Query the name is not null, and group by gender

It should be noted here that grouping in CQL is different from SQL. In CQL, there is no need to explicitly write group by grouping fields, which are automatically determined by the interpreter: that is, fields without aggregate functions are automatically determined as grouping fields.

match (s:Student) where s.name is not null return s.sex,count(*)


Union joint query (query students whose gender is male or female and whose age is 19)

match (s:Student) where s.sex=1 and s.age=19 return s.id,s.name,s.sex,s.age
union
match (s:Student) where s.sex=0 and s.age=19 return s.id,s.name,s.sex,s.age

4.3 Add relationship

East and Shenzhen establish a relationship, and Shenzhen belongs to Guangdong Province. But there is no Guangdong Province node and Shenzhen City node, yes, we just create a relationship for two nodes that do not exist.

create (c:City{<!-- -->id:30000,name:"Shenzhen"})-[belongto:BelongTo{<!-- -->type:"belongs to"}]-> (p:Province{<!-- -->id:40000,name:"Guangdong Province"})

match (c:City{<!-- -->id:30000,name:"Shenzhen"})-[belongto:BelongTo{<!-- -->type:"belongs to"}]-> (p:Province{<!-- -->id:40000,name:"Guangdong Province"}) return c,belongto,p


The syntax for creating a relationship between two nodes that don’t exist is as follows:

create (<node1-name>:<label1-name>
    {<!-- --><property1-name>:<property1-Value>,
    <property1-name>:<property1-Value>})-
[(<relationship-name>:<relationship-label-name>{<!-- --><property-name>:<property-value>})]
->(<node2-name>:<label2-name>
    {<!-- --><property1-name>:<property1-Value>,
<property1-name>:<property1-value>})

4.4 Modification

Now modify Zhang San's age from 18 to 59
match (s:Student) where s.name="Zhang San" set s.age=19 return s

4.5 Delete

Here is an example of deleting a student node that has no attributes:
First query the nodes without attributes in the students

match (s:Student) where s.name is null return s

Delete this node
match (s:Student) where s.name is null delete s

Delete a node with a relationship
match (c:City{<!-- -->id:30000,name:"Shenzhen"})-[belongto]->(p:Province{<!-- -->id:40000,name:" Guangdong Province"}) delete c,belongto,p