apoc.import.json
语法 |
|
||
描述 |
从提供的 JSON 文件导入图。 |
||
输入参数 |
名称 |
类型 |
描述 |
|
|
用于导入数据的文件名或二进制数据。 |
|
|
|
|
|
返回参数 |
名称 |
类型 |
描述 |
|
|
导入数据的文件名。 |
|
|
|
导入数据的来源:“file”、“binary”或“file/binary”。 |
|
|
|
文件格式:["csv", "graphml", "json"]。 |
|
|
|
导入的节点数量。 |
|
|
|
导入的关系数量。 |
|
|
|
导入的属性数量。 |
|
|
|
导入的持续时间。 |
|
|
|
返回的行数。 |
|
|
|
导入运行时批处理的大小。 |
|
|
|
导入运行时批处理的数量。 |
|
|
|
导入是否成功运行。 |
|
|
|
导入返回的数据。 |
|
配置参数
此过程支持以下配置参数
| 名称 | 类型 | 默认值 | 描述 |
|---|---|---|---|
|
|
|
unwind 的批处理大小。 |
|
|
|
事务的批处理大小。 |
|
|
|
用于填充 json 中存在的 "id" 字段的属性名称。例如,一行 |
|
|
|
自定义 Neo4j 类型(点日期)的映射标签/属性名/属性类型。即 { User: { born: 'Point', dateOfBirth: 'Datetime' } } |
|
|
|
自定义 Neo4j 类型(点日期)的映射关系类型/属性名/属性类型。即 { KNOWS: { since: 'Datetime' } } |
|
|
|
允许接收未压缩(值: |
|
|
false |
如果存在,则删除 json 中存在的 "id" 字段。请注意,这样我们就无法导入关系,因为我们利用 "id" 字段连接节点。 |
|
|
|
一个映射,以标签作为键,以在导入期间要过滤的属性键列表作为值。例如 |
|
|
|
一个映射,以关系类型作为键,以在导入期间要过滤的属性键列表作为值。例如 |
nodePropertyMappings 和 relPropertyMappings 支持以下 Neo4j 类型
-
点 -
日期 -
本地时间 -
本地日期时间 -
持续时间 -
带时区时间 -
带时区日期时间
使用示例
apoc.import.json 存储过程可用于导入通过 apoc.export.json.* 存储过程创建的 JSON 文件,这些文件使用配置参数 jsonFormat: 'JSON_LINES'(默认配置)导出。
all.json 包含 Neo4j 电影图的一个子集,由 apoc.export.json.all 生成。
{"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}}}
我们可以使用 apoc.import.json 导入此文件。
CALL apoc.import.json("file:///all.json")
| file | source | format | nodes | relationships | properties | time | rows | batchSize | batches | done | data
| "file:///all.json" | "file" | "json" | 3 | 1 | 15 | 105 | 4 | -1 | 0 | TRUE | NULL
唯一性约束
为了避免导入重复值,所有导入的节点标签和关系类型都需要设置唯一性约束。如果缺少此约束,该过程将返回一条错误消息,提供关于缺失约束的信息以及创建该约束的命令。(因为此过程使用 WRITE 模式,所以它本身无法添加约束。)
文件 missingConstraint.json 包含以下内容
{"type":"node","id":"1","labels":["Test"],"properties":{"name":"A test name"}}
在没有约束的数据库上执行以下查询将抛出错误
CALL apoc.import.json("file:///missingConstraint.json")
Failed to invoke procedure `apoc.import.json`: Caused by: java.lang.RuntimeException: Missing constraint required for import. Execute this query:
CREATE CONSTRAINT FOR (n:Test) REQUIRE n.neo4jImportId IS UNIQUE;
错误消息的最后一行提供了创建约束所需的语句
CREATE CONSTRAINT FOR (n:Test) REQUIRE n.neo4jImportId IS UNIQUE;
添加约束后,导入将成功运行
CALL apoc.import.json("file:///missingConstraint.json")
| file | source | format | nodes | relationships | properties | time | rows | batchSize | batches | done | data
| "file:///missingConstraint.json" | "file" | "json" | 1 | 0 | 2 | 24 | 1 | -1 | 0 | TRUE | NULL
二进制文件
您还可以从二进制 byte[] 文件导入,可以是未压缩(默认值,配置为 {compression: NONE})或压缩文件(允许的压缩算法有:GZIP、BZIP2、DEFLATE、BLOCK_LZ4、FRAMED_SNAPPY)。也就是说
CALL apoc.import.json(`binaryFileNotCompressed`, {compression: 'NONE'})
或
CALL apoc.import.json(`binaryGzipByteArray`, {compression: 'GZIP'})
例如,这个与 apoc.util.compress 函数配合得很好
WITH apoc.util.compress('{"type":"node","id":"2","labels":["User"],"properties":{"age":12}}', {compression: 'DEFLATE'}) AS jsonCompressed
CALL apoc.import.json(jsonCompressed, {compression: 'DEFLATE'})
YIELD source, format, nodes, relationships, properties
RETURN source, format, nodes, relationships, properties
| source | format | nodes | relationships | properties
| "binary" | "json" | 1 | 0 | 2