neo4j-contrib/neo4j-apoc-procedures
GitHub: neo4j-contrib/neo4j-apoc-procedures
APOC 是 Neo4j 的扩展函数库,提供约 450 个过程和函数,弥补 Cypher 在数据集成、图算法和数据转换等方面的能力不足。
Stars: 1855 | Forks: 503
:readme:
:branch: 4.4
:docs: https://neo4j.com/labs/apoc/4.4
:apoc-release: 4.4.0.41
:neo4j-version: 4.4.49
:img: https://raw.githubusercontent.com/neo4j-contrib/neo4j-apoc-procedures/{branch}/docs/images
https://community.neo4j.com[image:https://img.shields.io/discourse/users?logo=discourse&server=https%3A%2F%2Fcommunity.neo4j.com[Discourse users]]
https://discord.gg/neo4j[image:https://img.shields.io/discord/787399249741479977?logo=discord&logoColor=white[Discord]]
= Neo4j {branch}.x 的精彩过程
// tag::readme[]
== 简介
// tag::intro[]
image::{img}/apoc.gif[float=right]
// tag::intro-text[]
Neo4j 3.x 引入了用户定义过程和函数的概念。
这些是某些功能的自定义实现,无法(轻易)用 Cypher 本身表达。
它们用 Java 实现,可以轻松部署到您的 Neo4j 实例中,然后直接从 Cypher 调用。
APOC 库包含许多(约 450 个)过程和函数,以协助处理数据集成、图算法或数据转换等领域的许多不同任务。
// end::intro-text[]
=== 许可证
Apache License 2.0
// tag::name-history[]
=== "APOC" 名称历史
// tag::name-history-text[]
http://matrix.wikia.com/wiki/Apoc[Apoc^] 是《黑客帝国》电影中 Nebuchadnezzar 号上的技术员和司机。他被 Cypher 杀死。
*APOC* 也是 2009 年 Neo4j 第一个捆绑的 http://neo4j.com/blog/convenient-package-neo4j-apoc-0-1-released/[A Package Of Component^](组件包)。
*APOC* 也代表 "Awesome Procedures On Cypher"(Cypher 上的精彩过程)
// end::name-history-text[]
// end::name-history[]
== 安装:使用 Neo4j Desktop
// tag::install-desktop[]
APOC Full 可以通过 http://neo4j.com/download[Neo4j Desktop] 安装,创建数据库后,进入 `Manage`(管理)屏幕,然后选择 `Plugins`(插件)选项卡。
在 APOC 框中点击 `Install`(安装)并等待,直到您在 "APOC" 旁看到一个绿色的勾号。
// end::install-desktop[]
image::{img}/desktop-apoc.jpg[width=800]
== 反馈
// tag::feedback[]
请通过 https://github.com/neo4j-contrib/neo4j-apoc-procedures/issues[GitHub issues] 提供反馈和报告错误,或加入 https://community.neo4j.com/t5/tag/apoc/tg-p[Neo4j Community Forum 并使用 APOC 标签提问^]。
// end::feedback[]
// tag::calling-procedures[]
== 在 Cypher 中调用过程和函数
// tag::usage[]
用户定义的 *函数* 可以像内置函数一样在 *任何* 表达式或断言中使用。
*过程* 可以通过 `CALL procedure.name();` 独立调用。
但您也可以将它们集成到您的 Cypher 语句中,这使得它们更加强大。
## .加载 JSON 示例
[source,cypher,subs=attributes]
WITH 'https://raw.githubusercontent.com/neo4j-contrib/neo4j-apoc-procedures/{branch}/core/src/test/resources/person.json' AS url
CALL apoc.load.json(url) YIELD value as person
## MERGE (p:Person {name:person.name})
ON CREATE SET p.age = person.age, p.children = size(person.children)
// end::usage[]
// end::calling-procedures[]
// end::intro[]
== APOC 过程和函数概览
所有包含的过程都列在 link:{docs}/overview[文档中的概览^] 中,并在后续章节中详细说明。
=== 内置帮助
// tag::help[]
image::{img}/apoc-help-apoc.jpg[width=600]
[cols="1m,5"]
|===
| call apoc.help('keyword') | 根据关键字列出名称、描述、签名、角色
|===
// end::help[]
== 详细功能文档
请参阅 link:{docs}[APOC User Guide^] 以获取库中每个主要功能的文档,包括数据导入/导出、图重构、数据转换等。
// tag::signature[]
== 过程和函数签名
要正确调用过程,您需要知道它们的参数名称、类型和位置。
为了 YIELD(产出)它们的结果,您必须知道输出列的名称和类型。
INFO:如果您错误地使用了过程,签名会显示在错误消息中。
您可以在 `CALL apoc.help("name")` 的输出中看到过程签名
## [source,cypher]
## CALL apoc.help("dijkstra")
签名总是 `name : : TYPE`,因此在这种情况下:
## apoc.algo.dijkstra
(startNode :: NODE?, endNode :: NODE?,
relationshipTypesAndDirections :: STRING?, weightPropertyName :: STRING?)
:: (path :: PATH?, weight :: FLOAT?)
.参数说明
[opts=header,cols="m,m"]
|===
| 名称 | 类型
h| 过程参数 |
| startNode | Node
| endNode | Node
| relationshipTypesAndDirections | String
| weightPropertyName | String
h| 输出返回列 |
| path | Path
| weight | Float
|===
// end::signature[]
== 手动安装:下载最新版本
// tag::install[]
由于 APOC 依赖于 Neo4j 的内部 API,您需要为您的 Neo4j 安装使用 *匹配的 APOC 版本*。
确保 *Neo4j 和 APOC 的前两个版本号相匹配*。
前往 http://github.com/neo4j-contrib/neo4j-apoc-procedures/releases/{apoc-release}[the latest release](最新版本)下载 *Neo4j version {branch}* 的二进制 jar 文件,并将其放入您的 `$NEO4J_HOME/plugins` 文件夹中。
您可以在 http://github.com/neo4j-contrib/neo4j-apoc-procedures/releases/[all releases here](所有版本)找到所有版本。
// end::install[]
== 手动配置
# [WARNING]
# // tag::warnings[]
出于安全原因,使用内部 API 的过程和函数默认处于禁用状态。
加载和启用 APOC 过程和函数可以使用 Neo4j 配置文件进行配置。
有关更多详细信息,请参阅 https://neo4j.com/labs/apoc/4.4/installation/#restricted[the APOC installation documentation]。
// end::warnings[]
// tag::version-matrix[]
=== 版本兼容性矩阵
由于 APOC 在某些地方依赖于 Neo4j 的内部 API,您需要为您的 Neo4j 安装使用正确的 APOC 版本。
APOC 使用一致的版本控制方案:`.` 版本。
版本号末尾的 `` 部分将随着每次 apoc 发布而递增。
[opts=header]
|===
|apoc 版本 | neo4j 版本
| https://github.com/neo4j-contrib/neo4j-apoc-procedures/releases/4.4.0.1[4.4.0.1^] | 4.4.0 (4.3.x)
| https://github.com/neo4j-contrib/neo4j-apoc-procedures/releases/4.3.0.4[4.3.0.4^] | 4.3.7 (4.3.x)
| https://github.com/neo4j-contrib/neo4j-apoc-procedures/releases/4.2.0.9[4.2.0.9^] | 4.2.11 (4.2.x)
| https://github.com/neo4j-contrib/neo4j-apoc-procedures/releases/4.1.0.10[4.1.0.10^] | 4.1.11 (4.1.x)
| https://github.com/neo4j-contrib/neo4j-apoc-procedures/releases/4.0.0.18[4.0.0.18^] | 4.0.12 (4.0.x)
| https://github.com/neo4j-contrib/neo4j-apoc-procedures/releases/3.5.0.15[3.5.0.15^] | 3.5.30 (3.5.x)
| https://github.com/neo4j-contrib/neo4j-apoc-procedures/releases/3.4.0.8[3.4.0.8^] | 3.4.18 (3.4.x)
| https://github.com/neo4j-contrib/neo4j-apoc-procedures/releases/3.3.0.4[3.3.0.4^] | 3.3.9 (3.3.x)
| https://github.com/neo4j-contrib/neo4j-apoc-procedures/releases/3.2.3.6[3.2.3.6^] | 3.2.14 (3.2.x)
| https://github.com/neo4j-contrib/neo4j-apoc-procedures/releases/3.1.3.9[3.1.3.9^] | 3.1.9 (3.1.x)
| https://github.com/neo4j-contrib/neo4j-apoc-procedures/releases/3.0.8.6[3.0.8.6^] | 3.0.12 (3.0.x)
| https://github.com/neo4j-contrib/neo4j-apoc-procedures/releases/3.5.0.0[3.5.0.0^] | 3.5.0-beta01
| https://github.com/neo4j-contrib/neo4j-apoc-procedures/releases/3.4.0.2[3.4.0.2^] | 3.4.5
| https://github.com/neo4j-contrib/neo4j-apoc-procedures/releases/3.3.0.3[3.3.0.3^] | 3.3.5
| https://github.com/neo4j-contrib/neo4j-apoc-procedures/releases/3.2.3.5[3.2.3.5^] | 3.2.3
| https://github.com/neo4j-contrib/neo4j-apoc-procedures/releases/3.1.3.8[3.1.3.8^] | 3.1.5
|===
// end::version-matrix[]
=== 获取 APOC 版本
要知道您当前的 `apoc` 版本,您可以使用 *函数* :
## [source,cypher]
## RETURN apoc.version();
=== 在 Neo4j Docker 镜像中使用 APOC
// tag::docker[]
APOC Full 可以通过 `NEO4JLABS_PLUGINS` 环境变量与 https://hub.docker.com/_/neo4j/[Neo4j Docker image] 一起使用。
如果我们使用此环境变量,APOC 插件将在运行时被下载并配置。
# [NOTE]
# 此功能旨在方便在开发环境中使用 APOC,但不建议在生产环境中使用。
## .以下是在 Docker 容器中运行带有最新版本 APOC 库的 Neo4j 4.0
[source,bash]
## docker run \
-p 7474:7474 -p 7687:7687 \
-v $PWD/data:/data -v $PWD/plugins:/plugins \
--name neo4j-apoc \
-e NEO4J_apoc_export_file_enabled=true \
-e NEO4J_apoc_import_file_enabled=true \
-e NEO4J_apoc_import_file_use__neo4j__config=true \
-e NEO4JLABS_PLUGINS=\[\"apoc\"\] \
neo4j:4.0
运行此命令后,我们应该在输出中看到以下两行:
## [source,text,subs=attributes]
## 为 https://neo4j-contrib.github.io/neo4j-apoc-procedures/versions.json 中的 Plugin 'apoc' 获取 versions.json
Installing Plugin 'apoc' from https://github.com/neo4j-contrib/neo4j-apoc-procedures/releases/download/{apoc-release}/{apoc-release}-all.jar to /plugins/apoc.jar
在生产环境中,我们应该下载与我们的 Neo4j 版本匹配的 APOC 发行版,将其复制到本地文件夹,并将其作为挂载在 `/plugins` 的数据卷提供。
## .以下是将 APOC 库下载到 `plugins` 目录中,然后将该文件夹挂载到 Neo4j Docker 容器
[source,bash,subs=attributes]
## mkdir plugins
pushd plugins
wget https://github.com/neo4j-contrib/neo4j-apoc-procedures/releases/download/{apoc-release}/apoc-{apoc-release}-all.jar
popd
docker run --rm -e NEO4_AUTH=none -p 7474:7474 -v $PWD/plugins:/plugins -p 7687:7687 neo4j:{branch}
如果您想将自定义的 apoc 配置传递给您的 Docker 实例,您可以使用环境变量,如下所示:
## [source,bash]
## docker run \
-p 7474:7474 -p 7687:7687 \
-v $PWD/data:/data -v $PWD/plugins:/plugins \
--name neo4j-apoc \
-e NEO4J_apoc_export_file_enabled=true \
-e NEO4J_apoc_import_file_enabled=true \
-e NEO4J_apoc_import_file_use__neo4j__config=true \
neo4j
// end::docker[]
// tag::build[]
=== 从源代码构建并安装当前开发分支
## git clone https://github.com/neo4j-contrib/neo4j-apoc-procedures
cd neo4j-apoc-procedures
./gradlew shadow
cp build/full/libs/apoc--all.jar $NEO4J_HOME/plugins/
$NEO4J_HOME/bin/neo4j restart
// 如果您想在磁盘存储上运行嵌入式模式或使用 neo4j-shell,请在 `conf/neo4j.conf` 中配置您的 `plugins` 目录,设置 `dbms.plugin.directory=path/to/plugins`。
包括运行测试在内的完整构建可以通过 `./gradlew build` 运行。
// end::build[]
// tag::codestyle[]
=== 应用代码风格
## ./gradlew spotlessApply
要应用 https://github.com/diffplug/spotless/tree/main/plugin-gradle#how-do-i-preview-what-spotlessapply-will-do[spotless] 代码风格,请运行上述 gradle 命令,这将移除所有未使用的 imports
// end::codestyle[]
标签:APOC, Cypher查询语言, DNS解析, JS文件枚举, Neo4j, Neo4j Desktop, 后台面板检测, 图算法, 域名枚举, 实用程序库, 开源项目, 数据库插件, 数据清洗, 数据转换, 数据集成, 用户自定义函数, 请求拦截