Docker 特有操作

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

使用 Neo4j Admin

在容器内本地运行 neo4j-admin CLI,使用以下命令:

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

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

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

使用 Neo4j Import

Neo4j 导入工具可以在容器内本地运行,使用以下命令:

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:2025.05.0 \
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 获取内存建议

带有 --docker 参数的 neo4j-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 报告

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:2025.05.0
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

Cypher Shell 可以在容器内本地运行,使用以下命令:

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

有关 cypher-shell 语法和选项的更多信息,请参阅Cypher Shell#cypher-shell-syntax[语法]。

从 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:2025.05.0
  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/your_password \
neo4j:2025.05.0

# 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/your_password \
neo4j:2025.05.0

# 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 使用),创建索引、约束或执行其他管理操作。

© . All rights reserved.