加载 JSON

过程 APOC Core

apoc.load.jsonParams('urlOrBinary',{headers}, {payload}, $config) YIELD value

它从 JSON URL、RestAPI 端点 URL 或二进制源加载数据。

headers 和 payload 参数可用于执行 RestAPI。如果起始 JSON 是 JSON 列表,该过程将返回多个值;如果是单个 JSON,则返回单个结果。

签名

apoc.load.jsonParams(urlOrKeyOrBinary :: ANY?, headers :: MAP?, payload :: STRING?, path =  :: STRING?, config = {} :: MAP?) :: (value :: MAP?)

输入参数

名称 类型 默认值

urlOrKeyOrBinary

ANY?

null

headers

MAP?

null

payload

STRING?

null

path

STRING?

config

MAP?

{}

输出参数

名称 类型

value

MAP?

从文件读取

默认情况下,从文件系统导入是被禁用的。我们可以通过在 apoc.conf 中设置以下属性来启用它

apoc.conf
apoc.import.file.enabled=true

如果我们尝试在未首先设置此属性的情况下使用任何导入过程,我们将收到以下错误消息:

Failed to invoke procedure: Caused by: java.lang.RuntimeException: Import from files not enabled, please set apoc.import.file.enabled=true in your apoc.conf

导入文件从 import 目录读取,该目录由 server.directories.import 属性定义。这意味着我们提供的任何文件路径都是相对于此目录的。如果我们尝试从绝对路径读取,例如 /tmp/filename,我们将收到类似于以下的错误消息:

Failed to invoke procedure: Caused by: java.lang.RuntimeException: Can’t read url or key file:/path/to/neo4j/import/tmp/filename as json: /path/to/neo4j//import/tmp/filename (No such file or directory)

我们可以通过在 apoc.conf 中设置以下属性来启用从文件系统中任意位置读取文件:

apoc.conf
apoc.import.file.use_neo4j_config=false

Neo4j 现在将能够从文件系统中的任何位置读取,因此在设置此属性之前,请确保这是您的意图。

使用示例

我们可以通过将 config 参数 method 设置为 POST 来对 JSON 端点执行 POST 请求。我们还将使用 apoc.convert.toJson(包含在 APOC core 中)从 Cypher 映射构建 JSON 有效载荷。

以下示例向 Neo4j 的搜索 API 发送 POST 请求

CALL apoc.load.jsonParams(
  "https://neo4j.ac.cn/docs/search/",
  {method: "POST"},
  apoc.convert.toJson({query: "subquery", version: "4.0"})
);
表 1. 结果
value

{description: "The CALL {} clause evaluates a subquery that returns some values.", weight: 0.6460227966308594, title: "3.16. CALL {} (subquery) - Chapter 3. Clauses", uri: "https://neo4j.ac.cn/docs/cypher-manual/4.0/clauses/call-subquery/"}

{description: "This section provides examples of queries and Cypher commands that can be used with Neo4j Fabric.", weight: 0.05099273845553398, title: "7.3. Queries - Chapter 7. Fabric", uri: "https://neo4j.ac.cn/docs/operations-manual/4.0/fabric/queries/"}

{description: "WHERE adds constraints to the patterns in a MATCH or OPTIONAL MATCH clause or filters the results of a WITH clause.", weight: 0.03291567042469978, title: "3.6. WHERE - Chapter 3. Clauses", uri: "https://neo4j.ac.cn/docs/cypher-manual/4.0/clauses/where/"}

{description: "This appendix contains the recommended style when writing Cypher queries.", weight: 0.031550146639347076, title: "Appendix A. Cypher styleguide - The Neo4j Cypher Manual v4.0", uri: "https://neo4j.ac.cn/docs/cypher-manual/4.0/styleguide/"}

{description: "This section contains information on all the clauses in the Cypher query language.", weight: 0.02944066934287548, title: "Chapter 3. Clauses - The Neo4j Cypher Manual v4.0", uri: "https://neo4j.ac.cn/docs/cypher-manual/4.0/clauses/"}

{description: "", weight: 0.01821548491716385, title: "2.3. Expressions - Chapter 2. Syntax", uri: "https://neo4j.ac.cn/docs/cypher-manual/4.0/syntax/expressions/"}

© . All rights reserved.