问题描述
我是neo4j的新手,我想上传一个csv文件并创建一组节点。但是我已经有一些现有的节点可能存在于该csv文件中。是否可以选择加载csv,基于每行创建节点,并且如果该节点已经存在,请跳过该行?
谢谢
解决方法
但是,您需要仔细阅读文档以了解如何使用MERGE
,因为使用不正确会导致意外创建节点和关系。
Merge将为您提供所需的内容,但是您必须小心如何唯一地识别记录,以防止创建重复项
由于关注范围似乎正在减少,我将首先放置所需的最终形式...
// This one is safe assuming name is a true unique identifier of your Friends
// and that their favorite colors and foods may change over time
LOAD CSV FROM 'data/friends.csv' AS line
MERGE (f:Friend { name: line[0]})
set a.favorite_food = line[1]
set a.favorite_color = line[2]
上面的合并将创建或找到具有该匹配名称的Friend节点,然后无论我们是创建它还是对其进行更新,都在其上设置属性。
如果我们要这样提供合并中的所有属性:
// This one is dangerous - all attributes must match in order
// to find the existing Friend node
LOAD CSV FROM 'data/friends.csv' AS line
MERGE (f:Friend { name: line[0],favorite_food: line[1],favorite_color: line[2]})
那么,每次(重新)加载我们的数据时,我们的favourite_food或favourite_color都更新时,我们将找不到现有的朋友。
这是任何一个没有想象力的人的例子...
//Last month's file contained:
Bob Marley,Hemp Seeds,Green
//This month's file contained:
Bob Marley,Soylent Green,Rainbow