current position:Home>Neo4j: Running a Graph Database with Docker and the Cypher Query Language

Neo4j: Running a Graph Database with Docker and the Cypher Query Language

2022-08-06 09:37:11Artificial Intelligence Technology Xiaobai Cultivation Manual

简介

with data objects as the main one RDMS(关系数据库管理系统)相比,Relationships between entities in a graph database play a major role,and represented as triples.Graph databases can provide many superior properties,Especially when there are many interrelated data entities in the data.

Neo4j It was one of the first graph database systems,This article will study it.

Neo4j 使用 Cypher Query 语言和 cypher-shell tool for query work,并通过内置 UI 的通用 Web 浏览器访问 Neo4j 数据库.此外,Neo4j 支持 REST API.

Neo4j 有付费版,也有免费的社区版,但有一些限制(没有集群、There is no online backup、There is only one user database、No extensions etc).在这篇文章中,将使用 Docker 启动 Neo4j 社区版,Its query language is briefly introduced through cases,and how to perform a backup restore.

正文

使用 Docker 运行 Neo4j

$ docker run --rm --name neo4j -p 7474:7474 -p 7687:7687 neo4j:latest
…
Directories in use:
home: /var/lib/neo4j
config: /var/lib/neo4j/conf
logs: /logs
plugins: /var/lib/neo4j/plugins
import: /var/lib/neo4j/import
data: /var/lib/neo4j/data
certificates: /var/lib/neo4j/certificates
run: /var/lib/neo4j/run
Starting Neo4j.
…
2020–07–27 10:11:30.394+0000 INFO Bolt enabled on 0.0.0.0:7687.
2020–07–27 10:11:31.640+0000 INFO Remote interface available at [http://localhost:7474/](http://localhost:7474/)
2020–07–27 10:11:31.640+0000 INFO Started

检查一下 —— 打开浏览器,进入 http://localhost:7474,Then use the default loginUsername:neo4j,Password:neo4j 

管理员密码

设置新密码 : --env NEO4J_AUTH

$ docker run --rm --name neo4j --env NEO4J_AUTH=neo4j/pass -p 7474:7474 -p 7687:7687 neo4j:latest
Changed password for user ‘neo4j’.
…

cypher-shell

要使用数据库,可以通过 REST API or local tools — cypher-shell.

$ docker exec -ti neo4j cypher-shell -u neo4j -p pass
Connected to Neo4j 4.1.0 at neo4j://localhost:7687 as user neo4j.
Type :help for a list of available commands or :exit to exit the shell.
Note that Cypher queries must end with a semicolon.
[email protected]>

Neo4j 配置文件

主配置文件位于 $NEO4J_HOME/conf/neo4j.conf 路径,例如/var/lib/neo4j/conf/neo4j.conf

[email protected]:/var/lib/neo4j# head /var/lib/neo4j/conf/neo4j.conf
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
Neo4j configuration
For more details and a complete list of settings, please see
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
The name of the default database
dbms.default_database=neo4j

Redefine any settings — Mount the new configuration file to  /conf 目录.

neo4j.conf All settings of 这里找到.

要从 shell 获取当前配置 — 使用 dbms.listConfig() 来调用:

[email protected]> CALL dbms.listConfig()
YIELD name, value
WHERE name STARTS WITH ‘dbms.default’
RETURN name, value
ORDER BY name
LIMIT 3;

+ — — — — — — — — — — — — — — — — — — — — — — — — -+
| name | value |
+ — — — — — — — — — — — — — — — — — — — — — — — — -+
| “dbms.default_advertised_address” | “localhost” |
| “dbms.default_database” | “neo4j” |
| “dbms.default_listen_address” | “0.0.0.0” |
+ — — — — — — — — — — — — — — — — — — — — — — — — -+
3 rows available after 216 ms, consumed after another 13 ms

cypher-shell 与 CQL

Next let's do it with the actual data.

CREATE

创建一个node

[email protected]> create (test);
0 rows available after 56 ms, consumed after another 0 ms
Added 1 nodes

DELETE

删除node

[email protected]> MATCH (test) DETACH DELETE test;
0 rows available after 32 ms, consumed after another 0 ms
Deleted 1 nodes

To delete all records from the database —— 使用 (n)

[email protected]> MATCH (n) detach delete n;

Labels

创建一个带有 label1 标签的节点,其 Properties 包含两个键 — key1 和 key2

[email protected]> create (node1:label1 {key1: “value1”, key2: “value2”} );
0 rows available after 47 ms, consumed after another 0 ms
Added 1 nodes, Set 2 properties, Added 1 labels

结果

[email protected]> MATCH (node1) RETURN node1;
+ — — — — — — — — — — — — — — — — — — — — — — +
| node1 |
+ — — — — — — — — — — — — — — — — — — — — — — +
| (:label1 {key1: “value1”, key2: “value2”}) |
+ — — — — — — — — — — — — — — — — — — — — — — +

或者通过使用 RETURN —— Get the nodes in the same query right after they are created

[email protected]> CREATE (node2:label2 {key1: “value1”, key2: “value2”} ) RETURN node2;
+ — — — — — — — — — — — — — — — — — — — — — — +
| node2 |
+ — — — — — — — — — — — — — — — — — — — — — — +
| (:label2 {key1: “value1”, key2: “value2”}) |
+ — — — — — — — — — — — — — — — — — — — — — — +

使用 MATCH(n) RETURN n Displays all matching results in the browser

Relations

New relationships can be created between any new nodes or between nodes that already exist.

Create a relationship between the new nodes ——  -[r:RelationName]->

[email protected]> create (node3:label3 {key1: “value1”, key2: “value2”}) -[r:RelationName]-> (node4:label4{key1: “value1”, key2: “value2”}) RETURN node3, node4;
+ — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — -+
| node3 | node4 |
+ — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — -+
| (:label3 {key1: “value1”, key2: “value2”}) | (:label4 {key1: “value1”, key2: “value2”}) |
+ — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — -+
1 row available after 88 ms, consumed after another 8 ms
Added 2 nodes, Created 1 relationships, Set 4 properties, Added 2 labels

Create relationships between existing nodes  ——  使用 MATCH match these nodes

[email protected]> MATCH (node3:label3), (node4:label4) CREATE (node3) -[r:RelationName2]-> (node4) RETURN node3, node4;
+ — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — -+
| node3 | node4 |
+ — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — -+
| (:label3 {key1: “value1”, key2: “value2”}) | (:label4 {key1: “value1”, key2: “value2”}) |
+ — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — -+
1 row available after 124 ms, consumed after another 9 ms
Created 1 relationships

备份 && 恢复

数据存储在 $NEO4J_HOME/data 中,它实际上是 /data 的符号链接. 查看目录

[email protected]:/var/lib/neo4j# ls -l /var/lib/neo4j/data
lrwxrwxrwx 1 root root 5 Jul 23 09:01 /var/lib/neo4j/data -> /data
[email protected]:/var/lib/neo4j# ls -l /data/
total 12
drwxrwxrwx 4 neo4j neo4j 4096 Jul 27 11:19 databases
drwxr-xr-x 2 neo4j neo4j 4096 Jul 27 11:19 dbms
drwxrwxrwx 4 neo4j neo4j 4096 Jul 27 11:19 transactions

Database files are stored in the database directory,Two default databases can be found there - system 和 neo4j,可以通过 show databases 找到

[email protected]> show databases;
+ — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — +
| name | address | role | requestedStatus | currentStatus | error | default |
+ — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — +
| “neo4j” | “localhost:7687” | “standalone” | “online” | “online” | “” | TRUE |
| “system” | “localhost:7687” | “standalone” | “online” | “online” | “” | FALSE |
+ — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — +

 The system database is used for the system itself,而 nedo4j is the default user database.

Neo4j dump

Create a new directory to hold the data

$ mkdir -p /tmp/neo4/{data,logs}

重新启动 Neo4j ,Mount these directories into it

$ docker run --rm --name neo4j --env NEO4J_AUTH=neo4j/pass -p 7474:7474 -p 7687:7687 -v /tmp/neo4/data/:/data -v /tmp/neo4/logs/:/logs neo4j:latest
Changed password for user ‘neo4j’.
Directories in use:
home: /var/lib/neo4j
config: /var/lib/neo4j/conf
logs: /logs
plugins: /var/lib/neo4j/plugins
import: /var/lib/neo4j/import
data: /var/lib/neo4j/data
certificates: /var/lib/neo4j/certificates
run: /var/lib/neo4j/run
…

查看数据

$ ll /tmp/neo4/data/databases/
total 0
drwxr-xr-x 2 7474 7474 720 Jul 27 16:07 neo4j
-rw-r — r — 1 7474 7474 0 Jul 27 16:07 store_lock
drwxr-xr-x 3 7474 7474 740 Jul 27 16:07 system

连接,创建新数据

$ docker exec -ti neo4j cypher-shell -u neo4j -p pass
[email protected]> create (test:tobackup);
0 rows available after 131 ms, consumed after another 0 ms
Added 1 nodes

要创建 database dump,You first need to stop the instance(Because the community edition does not have an online backup function),Otherwise, the following error will appear

[email protected]:/var/lib/neo4j# neo4j-admin dump --database=neo4j --to=/data/backups/
The database is in use. Stop database ‘neo4j’ and try again.

 因此

$ docker stop neo4j

重新启动它,But added at this time bash order to prevent Neo4j 服务启动

$ docker run -ti --rm --name neo4j --env NEO4J_AUTH=neo4j/pass -p 7474:7474 -p 7687:7687 -v /tmp/neo4/data/:/data -v /tmp/neo4/logs/:/logs neo4j:latest bash
[email protected]:~$

创建一个 dump

[email protected]:~$ mkdir /data/backup
[email protected]:~$ neo4j-admin dump --database=neo4j --to=/data/backup/
Done: 34 files, 250.8MiB processed.

查看

[email protected]:~$ ls -l /data/backup/
total 12
-rw-r — r — 1 neo4j neo4j 9971 Jul 27 13:46 neo4j.dump

恢复

为第二个 Neo4j Instance creates a new directory  

$ mkdir -p /tmp/neo4–2/{data,logs}

Copy the backup directory from the first directory

$ sudo cp -r /tmp/neo4/data/backup/ /tmp/neo4–2/data/

Run the service as usual,挂载 /tmp/neo4-2,Substitute the port and its name

$ docker run — rm — name neo4j-2 — env NEO4J_AUTH=neo4j/pass -p 7475:7474 -p 7688:7687 -v /tmp/neo4–2/data/:/data -v /tmp/neo4–2/logs/:/logs neo4j:latest

Connect and check the data

$ docker exec -ti neo4j-2 cypher-shell -u neo4j -p pass
Connected to Neo4j 4.1.0 at neo4j://localhost:7687 as user neo4j.
Type :help for a list of available commands or :exit to exit the shell.
Note that Cypher queries must end with a semicolon.
[email protected]> match (n) return n;
+ — -+
| n |
+ — -+
+ — -+

好的 ——  这里没有找到,Because this is a brand new database.

退出,stop it and use bash 运行:

$ docker run -ti --rm --name neo4j-2 --env NEO4J_AUTH=neo4j/pass -p 7475:7474 -p 7688:7687 -v /tmp/neo4–2/data/:/data -v /tmp/neo4–2/logs/:/logs neo4j:latest bash

[email protected]:~$

使用 --force 键将 dump 加载到数据库中,因为默认的 neo4j 数据库已经存在:

[email protected]:~$ neo4j-admin load --from=/data/backup/neo4j.dump --database=neo4j --force
Done: 34 files, 250.8MiB processed.

退出,Reboot again in the normal way,启动Neo4j进程

$ docker run -ti --rm --name neo4j-2 --env NEO4J_AUTH=neo4j/pass -p 7475:7474 -p 7688:7687 -v /tmp/neo4–2/data/:/data -v /tmp/neo4–2/logs/:/logs neo4j:latest

连接,检查

[email protected]> match (n) return n;
+ — — — — — — -+
| n |
+ — — — — — — -+
| (:tobackup) |
+ — — — — — — -+

data in place  ——  全部完成.

copyright notice
author[Artificial Intelligence Technology Xiaobai Cultivation Manual],Please bring the original link to reprint, thank you.
https://en.chowdera.com/2022/218/202208060928126577.html

Random recommended