Docker 特定操作

在使用 Docker 容器运行 Neo4j 时,可以使用 Neo4j 工具。

使用 Neo4j Admin

可以使用以下命令在容器内本地运行Neo4j Admin 工具

docker exec --interactive --tty <containerID/name> neo4j-admin <category> <command>

要确定容器 ID 或名称,请运行 docker ps 以列出当前正在运行的 Docker 容器。

有关 neo4j-admin 命令的更多信息,请参阅Neo4j Admin 和 Neo4j CLI

使用 Neo4j Import

可以使用以下命令在容器内本地运行Neo4j Import 工具

docker exec --interactive --tty <containerID/name> neo4j-admin database import full <options>

docker exec --interactive --tty <containerID/name> neo4j-admin database import incremental <options>

有关命令语法和选项的更多信息,请参阅完整导入增量导入

先决条件

  • 验证您已创建要作为卷安装到 Neo4j Docker 容器的文件夹。

  • 验证您要加载到 Neo4j 的 CSV 文件是否已按照CSV 标题格式 格式化。

  • 验证您是否已将 CSV 文件添加到将安装到容器中 /import 的文件夹。

使用 Neo4j 导入工具将 CSV 文件导入 Neo4j Docker 容器

这是一个启动带有安装卷 /data/import 的容器的示例,以确保其中数据的持久性,并使用 neo4j-admin database import full 命令加载 CSV 文件。您可以添加标志 --rm 以在容器退出时自动删除容器的文件系统。

docker run --interactive --tty --rm \
    --publish=7474:7474 --publish=7687:7687 \
    --volume=$HOME/neo4j/data:/data \
    --volume=$HOME/neo4j/import:/import \
    neo4j:5.25.1 \
neo4j-admin database import full --nodes=Movies=/import/movies_header.csv,/import/movies.csv \
--nodes=Actors=/import/actors_header.csv,/import/actors.csv \
--relationships=ACTED_IN=/import/roles_header.csv,/import/roles.csv databasename

使用 Neo4j Admin 进行内存建议

使用参数 --dockerneo4j-admin server memory-recommendation 命令会输出可以传递给 Neo4j Docker 容器的环境变量。以下示例显示了 neo4j-admin server memory-recommendation --docker 如何以 Docker 友好格式提供内存建议。

示例 1. 调用 neo4j-admin server memory-recommendation --docker
bin/neo4j-admin server memory-recommendation --memory=16g --docker

...
...
...
# Based on the above, the following memory settings are recommended:
NEO4J_server_memory_heap_initial__size='5g'
NEO4J_server_memory_heap_max__size='5g'
NEO4J_server_memory_pagecache_size='7g'
#
# It is also recommended turning out-of-memory errors into full crashes,
# instead of allowing a partially crashed database to continue running:
NEO4J_server_jvm_additional='-XX:+ExitOnOutOfMemoryError'
#
...
...

使用 Neo4j Admin 报告

The Neo4j Admin 报告工具 会生成正在运行的 Neo4j 数据库状态的报告。
在容器化环境中,其命令 neo4j-admin server report 必须使用脚本 neo4j-admin-report 调用。这确保了报告程序使用分析正在运行的 Neo4j 进程所需的全部必要文件权限运行。此脚本接受 neo4j-admin server report 命令的所有参数。

可以使用 docker exec 直接在容器内运行 neo4j-admin server report,但访问正在运行的进程以及将文件写入安装文件夹所需的权限可能会发生冲突。

neo4j-admin-report 脚本只是 neo4j-admin server report 的一个包装程序,它会自动处理大多数权限问题。

示例 2. 从容器化的 Neo4j 数据库获取 Neo4j 报告的示例

启动一个 Neo4j 容器并安装一个用于存储报告的文件夹。

docker run --interactive --tty --rm \
    --name=neo4j \
    --publish=7474:7474 --publish=7687:7687 \
    --volume=$HOME/neo4j/data:/data \
    --volume=$HOME/neo4j/reports:/reports \ (1)
    neo4j:5.25.1
1 报告的输出文件夹。

然后,使用 docker exec 运行 neo4j-admin-report 包装脚本,指定报告的输出目录。

docker exec --interactive --tty <containerID/name> \
    neo4j-admin-report \
        --to-path=/reports \ (1)
1 如果没有指定 --to-path 选项,报告将写入 /tmp/reports

$HOME/neo4j/reports 文件夹现在应该包含一个包含报告的 zip 文件。

使用 Cypher Shell

可以使用以下命令在容器内本地运行Neo4j Cypher Shell 工具

docker exec --interactive --tty <containerID/name> cypher-shell <options>

有关 cypher-shell 语法和选项的更多信息,请参阅语法

从 Neo4j Docker 容器中的数据库检索数据

以下是如何使用 cypher-shell 命令从 neo4j 数据库检索数据的示例。

  1. 运行一个新的容器,安装与导入示例 中相同的卷 /data

    docker run --interactive --tty --name <containerID/name> \
        --publish=7474:7474 --publish=7687:7687 \
        --volume=$HOME/neo4j/data:/data \
        neo4j:5.25.1
  2. 使用容器 ID 或名称进入容器,然后运行 cypher-shell 命令并进行身份验证。

    docker exec --interactive --tty <containerID/name> cypher-shell -u neo4j -p <password>
  3. 检索一些数据。

    neo4j@neo4j> match (n:Actors)-[r]->(m:Movies) return n.name AS Actors, m.title AS Movies, m.year AS MovieYear;
    +-------------------------------------------------------------+
    | Actors               | Movies                   | MovieYear |
    +-------------------------------------------------------------+
    | "Keanu Reeves"       | "The Matrix Revolutions" | 2003      |
    | "Keanu Reeves"       | "The Matrix Reloaded"    | 2003      |
    | "Keanu Reeves"       | "The Matrix"             | 1999      |
    | "Laurence Fishburne" | "The Matrix Revolutions" | 2003      |
    | "Laurence Fishburne" | "The Matrix Reloaded"    | 2003      |
    | "Laurence Fishburne" | "The Matrix"             | 1999      |
    | "Carrie-Anne Moss"   | "The Matrix Revolutions" | 2003      |
    | "Carrie-Anne Moss"   | "The Matrix Reloaded"    | 2003      |
    | "Carrie-Anne Moss"   | "The Matrix"             | 1999      |
    +-------------------------------------------------------------+
    
    9 rows available after 61 ms, consumed after another 7 ms

将 Cypher 脚本文件传递给 Neo4j Docker 容器

有不同的方法可以将 Cypher 脚本文件传递给 Neo4j Docker 容器,所有这些方法都使用 Cypher Shell 工具。

  • 使用 cypher-shell 命令的 --file 选项,后跟文件名。语句执行完毕后,cypher-shell 会关闭。

  • 在 Cypher 交互式 shell 中,使用 :source 命令后跟文件名。

  • 使用 catcurl 命令与 cypher-shell 将脚本文件的內容管道传输到容器。

要使用 Cypher Shell 的 --file 选项或 :source 命令,Cypher 脚本文件必须可以在容器内部读取,否则 cypher-shell 将无法打开该文件。启动容器时,必须将包含示例的文件夹安装到容器中。

以下是使用这些命令的语法示例

example.cypher 脚本
match (n:Actors)-[r]->(m:Movies) return n.name AS Actors, m.title AS Movies, m.year AS MovieYear;
使用 --file 选项调用 cypher-shell
# Put the example.cypher file in the local folder ./examples.

# Start a Neo4j container and mount the ./examples folder inside the container:

docker run --rm \
--volume /path/to/local/examples:/examples \
--publish=7474:7474 \
--publish=7687:7687 \
--env NEO4J_AUTH=neo4j/ \
neo4j:5.25.1

# Run the Cypher Shell tool with the --file option passing the example.cypher file:

docker exec --interactive --tty  cypher-shell -u neo4j -p  --file /examples/example.cypher
使用 :source 命令运行 Cypher 脚本文件
# Put the example.cypher file in the local folder ./examples.

# Start a Neo4j container and mount the ./examples folder inside the container:

docker run --rm \
--volume /path/to/local/examples:/examples \
--publish=7474:7474 \
--publish=7687:7687 \
--env NEO4J_AUTH=neo4j/ \
neo4j:5.25.1

# Use the container ID or name to get into the container, and then, run the cypher-shell command and authenticate.

docker exec --interactive --tty  cypher-shell -u neo4j -p 

# Invoke the :source command followed by the file name.

neo4j@neo4j> :source example.cypher
使用 Cypher Shell 调用 curl
curl http://mysite.com/config/example.cypher | sudo docker exec --interactive <containerID/name> cypher-shell -u neo4j -p <password>
使用 Cypher Shell 调用 cat
cat example.cypher | sudo  docker exec --interactive  <containerID/name> cypher-shell -u neo4j -p <password>
示例输出
Actors, Movies, MovieYear
"Keanu Reeves", "The Matrix Revolutions", 2003
"Keanu Reeves", "The Matrix Reloaded", 2003
"Keanu Reeves", "The Matrix", 1999
"Laurence Fishburne", "The Matrix Revolutions", 2003
"Laurence Fishburne", "The Matrix Reloaded", 2003
"Laurence Fishburne", "The Matrix", 1999
"Carrie-Anne Moss", "The Matrix Revolutions", 2003
"Carrie-Anne Moss", "The Matrix Reloaded", 2003
"Carrie-Anne Moss", "The Matrix", 1999

这些命令将脚本文件的内容传递给使用 Cypher Shell 的 Docker 容器。然后,它们运行一个 Cypher 示例 LOAD CSV 数据集,该数据集可能托管在服务器上的某个位置(使用 curl),创建索引,约束或执行其他管理操作。