【发布时间】:2020-04-27 15:20:36
【问题描述】:
[问题 - 下面是我的最终解决方案]
我想将包含我的数据的 json 文件导入 Neo4J。 但是,它超级慢。
Json 文件结构如下
{
"graph": {
"nodes": [
{ "id": 3510982, "labels": ["XXX"], "properties": { ... } },
{ "id": 3510983, "labels": ["XYY"], "properties": { ... } },
{ "id": 3510984, "labels": ["XZZ"], "properties": { ... } },
...
],
"relationships": [
{ "type": "bla", "startNode": 3510983, "endNode": 3510982, "properties": {} },
{ "type": "bla", "startNode": 3510984, "endNode": 3510982, "properties": {} },
....
]
}
}
与此处建议的类似:How can I restore data from a previous result in the browser?。
通过查看答案。 我发现我可以使用
CALL apoc.load.json("file:///test.json") YIELD value AS row
WITH row, row.graph.nodes AS nodes
UNWIND nodes AS node
CALL apoc.create.node(node.labels, node.properties) YIELD node AS n
SET n.id = node.id
然后
CALL apoc.load.json("file:///test.json") YIELD value AS row
with row
UNWIND row.graph.relationships AS rel
MATCH (a) WHERE a.id = rel.endNode
MATCH (b) WHERE b.id = rel.startNode
CALL apoc.create.relationship(a, rel.type, rel.properties, b) YIELD rel AS r
return *
(我必须分两次这样做,否则由于两个unwind,它们是关系重复)。
但这超级慢,因为我有很多实体,我怀疑程序会搜索所有实体以查找每个关系。
同时,我知道"startNode": 3510983 指的是一个节点。
所以问题是:是否存在使用 ids 作为索引或其他东西来加速导入过程?
请注意,我的节点有不同的类型。所以我没有找到为所有这些创建索引的方法,我想那会太大(内存)
[我的解决方案]
CALL apoc.load.json('file:///test.json') YIELD value
WITH value.graph.nodes AS nodes, value.graph.relationships AS rels
UNWIND nodes AS n
CALL apoc.create.node(n.labels, apoc.map.setKey(n.properties, 'id', n.id)) YIELD node
WITH rels, COLLECT({id: n.id, node: node, labels:labels(node)}) AS nMap
UNWIND rels AS r
MATCH (w{id:r.startNode})
MATCH (y{id:r.endNode})
CALL apoc.create.relationship(w, r.type, r.properties, y) YIELD rel
RETURN rel
【问题讨论】:
标签: json neo4j neo4j-apoc