Neo4j 如何在单个csv文件中已创建的不同列的节点之间建立关系?

Neo4j 如何在单个csv文件中已创建的不同列的节点之间建立关系?,neo4j,cypher,graph-databases,Neo4j,Cypher,Graph Databases,我有一个单个csv文件,其内容如下- id,name,country,level 1,jon,USA,international 2,don,USA,national 3,ron,USA,local 4,bon,IND,national 5,kon,IND,national 6,jen,IND,local 7,ken,IND,international 8,ben,GB,local 9,den,GB,international 10,lin,GB,national 11,min,AU,natio

我有一个单个csv文件,其内容如下-

id,name,country,level
1,jon,USA,international
2,don,USA,national
3,ron,USA,local
4,bon,IND,national
5,kon,IND,national
6,jen,IND,local
7,ken,IND,international
8,ben,GB,local
9,den,GB,international
10,lin,GB,national
11,min,AU,national
12,win,AU,local
13,kin,AU,international
14,bin,AU,international
15,nin,CN,national
16,con,CN,local
17,eon,CN,international
18,fon,CN,international
19,pon,SZN,national
20,zon,SZN,international
LOAD CSV WITH HEADERS FROM "file:///demo.csv" AS row
MERGE (name:Name {name: row.name, id: row.id, country:row.country, level:row.level})
MERGE (country:Country {name: row.country})
MERGE (level:Level {type: row.level})
This query builds a cartesian product between disconnected patterns.
If a part of a query contains multiple disconnected patterns, this will build a cartesian product between all those parts. This may produce a large amount of data and slow down query processing. While occasionally intended, it may often be possible to reformulate the query that avoids the use of this cross product, perhaps by adding a relationship between the different parts or by using OPTIONAL MATCH (identifier is: (c))
首先,我在
id

CREATE CONSTRAINT idConstraint ON (n:Name) ASSERT n.id IS UNIQUE
然后我为
name
创建节点,然后为
country
创建节点,最后为
level
创建节点,如下所示-

id,name,country,level
1,jon,USA,international
2,don,USA,national
3,ron,USA,local
4,bon,IND,national
5,kon,IND,national
6,jen,IND,local
7,ken,IND,international
8,ben,GB,local
9,den,GB,international
10,lin,GB,national
11,min,AU,national
12,win,AU,local
13,kin,AU,international
14,bin,AU,international
15,nin,CN,national
16,con,CN,local
17,eon,CN,international
18,fon,CN,international
19,pon,SZN,national
20,zon,SZN,international
LOAD CSV WITH HEADERS FROM "file:///demo.csv" AS row
MERGE (name:Name {name: row.name, id: row.id, country:row.country, level:row.level})
MERGE (country:Country {name: row.country})
MERGE (level:Level {type: row.level})
This query builds a cartesian product between disconnected patterns.
If a part of a query contains multiple disconnected patterns, this will build a cartesian product between all those parts. This may produce a large amount of data and slow down query processing. While occasionally intended, it may often be possible to reformulate the query that avoids the use of this cross product, perhaps by adding a relationship between the different parts or by using OPTIONAL MATCH (identifier is: (c))
我可以很好地看到节点。然而,我希望能够查询一些东西,比如,对于一个给定的国家,有多少个名字?对于给定的级别,有多少个国家,以及该国家的名称

因此,我需要在节点之间建立关系。
为此,我试着这样做-

LOAD CSV WITH HEADERS FROM "file:///demo.csv" AS row
MATCH (n:Name {name:row.name}), (c:Country {name:row.country}) 
CREATE (n)-[:LIVES_IN]->(c)
RETURN n,c
然而,这给了我一个警告如下-

id,name,country,level
1,jon,USA,international
2,don,USA,national
3,ron,USA,local
4,bon,IND,national
5,kon,IND,national
6,jen,IND,local
7,ken,IND,international
8,ben,GB,local
9,den,GB,international
10,lin,GB,national
11,min,AU,national
12,win,AU,local
13,kin,AU,international
14,bin,AU,international
15,nin,CN,national
16,con,CN,local
17,eon,CN,international
18,fon,CN,international
19,pon,SZN,national
20,zon,SZN,international
LOAD CSV WITH HEADERS FROM "file:///demo.csv" AS row
MERGE (name:Name {name: row.name, id: row.id, country:row.country, level:row.level})
MERGE (country:Country {name: row.country})
MERGE (level:Level {type: row.level})
This query builds a cartesian product between disconnected patterns.
If a part of a query contains multiple disconnected patterns, this will build a cartesian product between all those parts. This may produce a large amount of data and slow down query processing. While occasionally intended, it may often be possible to reformulate the query that avoids the use of this cross product, perhaps by adding a relationship between the different parts or by using OPTIONAL MATCH (identifier is: (c))
此外,结果图看起来有点错误-每个名称节点与一个国家有2个关系,而我认为只有一个?

我也有一种唠叨的恐惧,我没有以优化或正确的方式做事。这只是一个演示。在我的真实数据集中,我经常不能同时运行多个CREATE或MERGE语句。我必须一次又一次地加载相同的CSV文件,才能完成创建节点的几乎所有工作。在创建关系时,由于笛卡尔乘积的形式,该命令基本上会给出Java堆内存错误


另外,我昨天刚开始学习neo4j。我真的不太了解它。我已经为此挣扎了一整天,因此想在这里提问。

您可以忽略笛卡尔产品警告,因为为了创建形成您需要的模式的关系,需要使用这种确切的方法


对于多个关系,您可能已经运行了两次查询。第二次运行将创建重复的关系。您可以对关系使用“合并”而不是“创建”,这样可以确保没有重复项。

您说得对!我把所有的东西都清理干净,然后再试一次,结果成功了。然而,我有一个问题…这是做我想做的事情的正确方法吗?我的意思是,考虑到所有内容都来自一个csv文件,创建这些类型的关系……有更好或更简单的方法吗?这是正确的方法:匹配要连接的节点,然后在它们之间创建或合并模式。如果你不需要目视确认,那么你可以省略退货。我认为我所做的是完全错误的。不知道为什么它以前工作,但当我再次运行它时,我再也看不到任何连接。具体来说,我认为用于建立关系的MATCH命令应该是
MATCH(n:Name{country:row.country}),(c:country{Name:row.country})
不是我刚才提到的对吗?我的意思是,你可以得到Name节点和Country节点,其中Name的Country属性与Country的Name属性相匹配……之前我盲目地获取所有的名称和国家。我又回到原点了,任何建议都会非常有用,如果它们都能起作用的话。请记住,在处理CSV时,
表示您正在处理每行的信息。所以之前,对于行,您按姓名匹配一个人,按国家名称匹配一个国家,并合并或创建他们之间的关系,这是一个好方法。在这里,您的新密码将根据其国家属性匹配名称,并根据其国家属性匹配国家,这应该也可以工作,即使在加载CSV之外。它不工作或者意味着输入错误,或者数据加载不正确(额外的空白?),或者不匹配。你应该仔细检查你的数据。两种思维过程都是正确的。我刷新了所有东西,重新编辑了它,它工作了。