导出到 JSON
导出 JSON 过程将数据导出为 JavaScript 可视化工具支持的格式。我们也可能希望将数据导出为 JSON 格式,以便导入其他工具或用于一般查询结果共享。本节描述的过程支持导出到文件或作为流导出。
使用的格式是 jsonlines 或 JSONL,这是一种流式格式,每行包含一个 JSON 对象,在我们的例子中是节点或关系。
可用过程
下表描述了可用过程
限定名称 | 类型 |
---|---|
apoc.export.json.all |
|
apoc.export.json.data |
|
apoc.export.json.graph |
|
apoc.export.json.query |
|
名称 | 类型 | 默认值 | 描述 |
---|---|---|---|
writeNodeProperties |
布尔值 |
true |
如果为 true,则也导出属性。 |
stream |
布尔值 |
false |
将 JSON 直接流式传输到客户端的 |
jsonFormat |
枚举[JSON_LINES, ARRAY_JSON, JSON, JSON_ID_AS_KEYS] |
JSON_LINES |
导出的 JSON 格式 |
名称 | 描述 |
---|---|
JSON_LINES |
数据将导出为 JSON Lines |
ARRAY_JSON |
数据将导出为 JSON 数组: |
JSON |
数据将导出为包含两个(数组)字段 |
JSON_ID_AS_KEYS |
数据将导出为包含两个(映射)字段 |
导出的标签按字母顺序排列。 |
导出到文件
默认情况下,禁用导出到文件系统。我们可以通过在 apoc.conf
中设置以下属性来启用它:
apoc.export.file.enabled=true
有关访问 apoc.conf
的更多信息,请参阅[配置选项](../../config/)一章。
如果我们尝试使用任何导出过程而未首先设置此属性,将收到以下错误消息:
调用过程失败:原因:java.lang.RuntimeException: 未启用文件导出,请在 apoc.conf 中设置 apoc.export.file.enabled=true。否则,如果您在没有文件系统访问权限的云环境中运行,请使用 |
导出文件会写入 import
目录,该目录由 server.directories.import
属性定义。这意味着我们提供的任何文件路径都是相对于此目录的。如果我们尝试写入绝对路径,例如 /tmp/filename
,将收到类似于以下内容的错误消息:
调用过程失败:原因:java.io.FileNotFoundException: /path/to/neo4j/import/tmp/fileName (无此文件或目录) |
我们可以通过在 apoc.conf
中设置以下属性来启用写入文件系统上的任意位置:
apoc.import.file.use_neo4j_config=false
现在 Neo4j 将能够写入文件系统上的任何位置,因此在设置此属性之前,请务必确认这是您的意图。 |
示例
本节包含展示如何使用导出到 JSON 过程的示例。这些示例基于人物数据集,可以通过运行以下 Cypher 查询来导入:
CREATE (a:User {
name:'Adam', age:42, male:true, kids:['Sam','Anna','Grace'],
born:localdatetime('2015185T19:32:24'),
place:point({latitude: 13.1, longitude: 33.46789})
})
CREATE (b:User {name:'Jim', age:42})
CREATE (c:User {age:12})
CREATE (a)-[:KNOWS {since: 1993}]->(b)
下面的 Neo4j Browser 可视化显示了导入的图。
请注意,为了执行正确的 Point 序列化,不建议导出坐标为 x、y 和 crs: 'wgs-84' 的点,例如 point({x: 56.7, y: 12.78, crs: 'wgs-84'})
。否则,点将以经度和纬度(和高度)而不是 x 和 y(和 z)的形式导出。

将整个数据库导出到 JSON
apoc.export.json.all
过程将整个数据库导出到 JSON 文件或作为流导出。
all.json
文件:CALL apoc.export.json.all("all.json",{useTypes:true})
文件 | 源 | 格式 | 节点数 | 关系数 | 属性数 | 时间 | 行数 | 批处理大小 | 批次 | 完成 | 数据 |
---|---|---|---|---|---|---|---|---|---|---|---|
"all.json" |
"database: nodes(3), rels(1)" |
"json" |
3 |
1 |
10 |
7 |
0 |
-1 |
0 |
TRUE |
NULL |
all.json
的内容如下所示:
{"type":"node","id":"0","labels":["User"],"properties":{"born":"2015-07-04T19:32:24","name":"Adam","place":{"crs":"wgs-84","latitude":13.1,"longitude":33.46789,"height":null},"age":42,"male":true,"kids":["Sam","Anna","Grace"]}}
{"type":"node","id":"1","labels":["User"],"properties":{"name":"Jim","age":42}}
{"type":"node","id":"2","labels":["User"],"properties":{"age":12}}
{"id":"0","type":"relationship","label":"KNOWS","properties":{"bffSince":"P5M1DT12H","since":1993},"start":{"id":"0","labels":["User"],"properties":{"born":"2015-07-04T19:32:24","name":"Adam","place":{"crs":"wgs-84","latitude":13.1,"longitude":33.46789,"height":null},"age":42,"male":true,"kids":["Sam","Anna","Grace"]}},"end":{"id":"1","labels":["User"],"properties":{"name":"Jim","age":42}}}
data
列中返回整个数据库的流:CALL apoc.export.json.all(null,{useTypes:true, stream: true})
YIELD file, nodes, relationships, properties, data
RETURN file, nodes, relationships, properties, data
文件 | 节点数 | 关系数 | 属性数 | 数据 |
---|---|---|---|---|
|
|
|
|
"{\"type\":\"node\",\"id\":\"0\",\"labels\":[\"User\"],\"properties\":{\"born\":\"2015-07-04T19:32:24\",\"name\":\"Adam\",\"place\":{\"crs\":\"wgs-84\",\"latitude\":13.1,\"longitude\":33.46789,\"height\":null},\"age\":42,\"male\":true,\"kids\":[\"Sam\",\"Anna\",\"Grace\"]}} {\"type\":\"node\",\"id\":\"1\",\"labels\":[\"User\"],\"properties\":{\"name\":\"Jim\",\"age\":42}} {\"type\":\"node\",\"id\":\"2\",\"labels\":[\"User\"],\"properties\":{\"age\":12}} {\"id\":\"50000\",\"type\":\"relationship\",\"label\":\"KNOWS\",\"properties\":{\"since\":1993},\"start\":{\"id\":\"0\",\"labels\":[\"User\"]},\"end\":{\"id\":\"1\",\"labels\":[\"User\"]}}" |
将指定节点和关系导出到 JSON
apoc.export.json.data
过程将指定节点和关系导出到 JSON 文件或作为流导出。
KNOWS
关系和 User
节点导出到 knows.json
文件:MATCH (nod:User)
MATCH ()-[rels:KNOWS]->()
WITH collect(nod) as a, collect(rels) as b
CALL apoc.export.json.data(a, b, "knows.json", null)
YIELD file, source, format, nodes, relationships, properties, time, rows, batchSize, batches, done, data
RETURN file, source, format, nodes, relationships, properties, time, rows, batchSize, batches, done, data
文件 | 源 | 格式 | 节点数 | 关系数 | 属性数 | 时间 | 行数 | 批处理大小 | 批次 | 完成 | 数据 |
---|---|---|---|---|---|---|---|---|---|---|---|
"knows.json" |
"data: nodes(3), rels(3)" |
"json" |
3 |
1 |
10 |
1 |
0 |
-1 |
0 |
TRUE |
NULL |
knows.json
的内容如下所示:
{"type":"node","id":"0","labels":["User"],"properties":{"born":"2015-07-04T19:32:24","name":"Adam","place":{"crs":"wgs-84","latitude":13.1,"longitude":33.46789,"height":null},"age":42,"male":true,"kids":["Sam","Anna","Grace"]}}
{"type":"node","id":"1","labels":["User"],"properties":{"name":"Jim","age":42}}
{"type":"node","id":"2","labels":["User"],"properties":{"age":12}}
{"id":"0","type":"relationship","label":"KNOWS","properties":{"bffSince":"P5M1DT12H","since":1993},"start":{"id":"0","labels":["User"],"properties":{"born":"2015-07-04T19:32:24","name":"Adam","place":{"crs":"wgs-84","latitude":13.1,"longitude":33.46789,"height":null},"age":42,"male":true,"kids":["Sam","Anna","Grace"]}},"end":{"id":"1","labels":["User"],"properties":{"name":"Jim","age":42}}}
data
列中返回所有 KNOWS
关系和 User
节点的流:MATCH (nod:User)
MATCH ()-[rels:KNOWS]->()
WITH collect(nod) as a, collect(rels) as b
CALL apoc.export.json.data(a, b, null, {stream: true})
YIELD file, nodes, relationships, properties, data
RETURN file, nodes, relationships, properties, data
文件 | 节点数 | 关系数 | 属性数 | 数据 |
---|---|---|---|---|
|
|
|
|
"{\"type\":\"node\",\"id\":\"0\",\"labels\":[\"User\"],\"properties\":{\"born\":\"2015-07-04T19:32:24\",\"name\":\"Adam\",\"place\":{\"crs\":\"wgs-84\",\"latitude\":13.1,\"longitude\":33.46789 ,\"height\":null},\"age\":42,\"male\":true,\"kids\":[\"Sam\",\"Anna\",\"Grace\"]}} {\"type\":\"node\",\"id\":\"1\",\"labels\":[\"User\"],\"properties\":{\"name\":\"Jim\",\"age\":42}} {\"type\":\"node\",\"id\":\"2\",\"labels\":[\"User\"],\"properties\":{\"age\":12}} {\"id\":\"50000\",\"type\":\"rel |
将 Cypher 查询结果导出到 JSON
apoc.export.json.query
过程将 Cypher 查询的结果导出到 JSON 文件或作为流导出。
age
属性大于 10
的 User
节点导出到 users-age.json
文件:CALL apoc.export.json.query(
"MATCH (u:User) WHERE u.age > $age return u",
"users-age.json",
{params:{age:15}}
)
文件 | 源 | 格式 | 节点数 | 关系数 | 属性数 | 时间 | 行数 | 批处理大小 | 批次 | 完成 | 数据 |
---|---|---|---|---|---|---|---|---|---|---|---|
"users-age.json" |
"statement: cols(1)" |
"json" |
2 |
0 |
8 |
3 |
2 |
-1 |
0 |
TRUE |
NULL |
users-age.json
的内容如下所示:
{"u":{"type":"node","id":"0","labels":["User"],"properties":{"born":"2015-07-04T19:32:24","name":"Adam","place":{"crs":"wgs-84","latitude":13.1,"longitude":33.46789,"height":null},"age":42,"male":true,"kids":["Sam","Anna","Grace"]}}}
{"u":{"type":"node","id":"1","labels":["User"],"properties":{"name":"Jim","age":42}}}
{"u":{"type":"node","id":"2","labels":["User"],"properties":{"age":12}}}
data
列中返回 age
属性大于 10
的 User
节点的流:CALL apoc.export.json.query(
"MATCH (u:User) WHERE u.age > $age return u",
null,
{params:{age:15}, stream: true}
)
YIELD file, nodes, relationships, properties, data
RETURN file, nodes, relationships, properties, data
文件 | 节点数 | 关系数 | 属性数 | 数据 |
---|---|---|---|---|
|
|
|
|
"{\"u\":{\"type\":\"node\",\"id\":\"0\",\"labels\":[\"User\"],\"properties\":{\"born\":\"2015-07-04T19:32:24\",\"name\":\"Adam\",\"place\":{\"crs\":\"wgs-84\",\"latitude\":13.1,\"longitude\":33.46789,\"height\":null},\"age\":42,\"male\":true,\"kids\":[\"Sam\",\"Anna\",\"Grace\"]}}} {\"u\":{\"type\":\"node\",\"id\":\"1\",\"labels\":[\"User\"],\"properties\":{\"name\":\"Jim\",\"age\":42}}}" |
complex-cypher-structure.json
文件:WITH "RETURN {
value:1,
data:[
10, 'car', null,
point({ longitude: 56.7, latitude: 12.78 }),
point({ longitude: 56.7, latitude: 12.78, height: 8 }),
point({ x: 2.3, y: 4.5 }),
point({ x: 2.3, y: 4.5, z: 2 }),
date('2018-10-10'),
datetime('2018-10-18T14:21:40.004Z'),
localdatetime({ year:1984, week:10, dayOfWeek:3, hour:12, minute:31, second:14, millisecond: 645 }),
{x:1, y:[1,2,3,{age:10}]}
]
} as key" AS query
CALL apoc.export.json.query(query, "complex-cypher-structure.json")
YIELD file, source, format, nodes, relationships, properties, time, rows, batchSize, batches, done, data
RETURN file, source, format, nodes, relationships, properties, time, rows, batchSize, batches, done, data
文件 | 源 | 格式 | 节点数 | 关系数 | 属性数 | 时间 | 行数 | 批处理大小 | 批次 | 完成 | 数据 |
---|---|---|---|---|---|---|---|---|---|---|---|
"complex-cypher-structure.json" |
"statement: cols(1)" |
"json" |
0 |
0 |
16 |
2 |
1 |
-1 |
0 |
TRUE |
NULL |
complex-cypher-structure.json
的内容如下所示:
{"key":{"data":[10,"car",null,{"crs":"wgs-84","latitude":12.78,"longitude":56.7,"height":null},{"crs":"wgs-84-3d","latitude":12.78,"longitude":56.7,"height":8.0},{"crs":"cartesian","x":2.3,"y":4.5,"z":null},{"crs":"cartesian-3d","x":2.3,"y":4.5,"z":2.0},"2018-10-10","2018-10-18T14:21:40.004Z","1984-03-07T12:31:14.645",{"x":1,"y":[1,2,3,{"age":10}]}],"value":1}}
age
属性大于 10
的 User
节点列表导出到 users-list.json
文件:CALL apoc.export.json.query(
"MATCH (u:User) RETURN COLLECT(u) as list",
"users-list.json",
{params:{age:10}}
)
文件 | 源 | 格式 | 节点数 | 关系数 | 属性数 | 时间 | 行数 | 批处理大小 | 批次 | 完成 | 数据 |
---|---|---|---|---|---|---|---|---|---|---|---|
"users-list.json" |
"statement: cols(1)" |
"json" |
3 |
0 |
9 |
1 |
1 |
-1 |
0 |
TRUE |
NULL |
users-list.json
的内容如下所示:
{"list":[{"type":"node","id":"0","labels":["User"],"properties":{"born":"2015-07-04T19:32:24","name":"Adam","place":{"crs":"wgs-84","latitude":13.1,"longitude":33.46789,"height":null},"age":42,"male":true,"kids":["Sam","Anna","Grace"]}},{"type":"node","id":"1","labels":["User"],"properties":{"name":"Jim","age":42}},{"type":"node","id":"2","labels":["User"],"properties":{"age":12}}]}
KNOWS
关系连接的 User
节点的映射表示导出到 users-map.json
文件:CALL apoc.export.json.query(
"MATCH (u:User)-[r:KNOWS]->(d:User) RETURN u {.*}, d {.*}, r {.*}",
"users-map.json",
{params:{age:10}}
)
文件 | 源 | 格式 | 节点数 | 关系数 | 属性数 | 时间 | 行数 | 批处理大小 | 批次 | 完成 | 数据 |
---|---|---|---|---|---|---|---|---|---|---|---|
"users-map.json" |
"statement: cols(3)" |
"json" |
0 |
0 |
11 |
8 |
1 |
-1 |
0 |
TRUE |
NULL |
users-map.json
的内容如下所示:
{"u":{"born":"2015-07-04T19:32:24","name":"Adam","place":{"crs":"wgs-84","latitude":13.1,"longitude":33.46789,"height":null},"age":42,"male":true,"kids":["Sam","Anna","Grace"]},"d":{"name":"Jim","age":42},"r":{"bffSince":"P5M1DT12H","since":1993}}
在此示例中,在 Cypher 语句中返回图实体时,我们使用了 {.*}
语法。此语法返回图实体的映射表示,这意味着仅导出属性;标签和关系类型将被排除在外。
KNOWS
关系(包括起始和结束节点及其属性)导出到 knows-with-node-properties.json
文件:CALL apoc.export.json.query(
"MATCH p = (u:User)-[rel:KNOWS]->(u2:User) RETURN rel",
"knows-with-node-properties.json",
{writeNodeProperties:true}
)
文件 | 源 | 格式 | 节点数 | 关系数 | 属性数 | 时间 | 行数 | 批处理大小 | 批次 | 完成 | 数据 |
---|---|---|---|---|---|---|---|---|---|---|---|
"knows-with-node-properties.json" |
"statement: cols(1)" |
"json" |
0 |
1 |
1 |
20 |
2 |
-1 |
0 |
TRUE |
NULL |
knows-with-node-properties.json
的内容如下所示:
{"rel":{"id":"0","type":"relationship","label":"KNOWS","properties":{"bffSince":"P5M1DT12H","since":1993},"start":{"id":"0","labels":["User"],"properties":{"born":"2015-07-04T19:32:24","name":"Adam","place":{"crs":"wgs-84","latitude":13.1,"longitude":33.46789,"height":null},"age":42,"male":true,"kids":["Sam","Anna","Grace"]}},"end":{"id":"1","labels":["User"],"properties":{"name":"Jim","age":42}}}}
您也可以压缩要导出的文件。[更多信息请参见此处](../../overview/apoc.export/compression/) |