0

我对 Neo4j 世界很陌生,所以如果这是一个微不足道的问题,请原谅我。我有 2 个表已使用 LOAD CSV 加载到数据库中

艺术家:

artist_name,artist_id
"Bob","abc"
"Jack","def"
"James","ghi"
"Someone","jkl"
"John","mno"

代理列表:

"Agency"
"A"
"B"
"C"
"D"

最后,我有一张中间表,上面有艺术家和代表他们的机构。

艺术家机构:

artist_name,artist_id,agency
"Bob","abc", "A"
"Bob","abc", "B"
"Jack","def", "C"
"James","ghi", "C"
"Someone","jkl","B"
"Someone","jkl", "C"
"John","mno", "D"

请注意,一些艺术家可能是多个机构的一部分(这就是我没有在艺术家表中包含机构变量的原因)

我试图获得四个基于 :REPRESENTS 关系连接到每个艺术家的代理节点。基本上是这样的: (agency:Agency) - [:REPRESENTS] -> (artist:Artist)

我试过的代码是:

LOAD CSV WITH HEADERS FROM "file:///agency_list.csv" as agencies
CREATE (agency:Agency {agency: agencies.Agency})

USING PERIODIC COMMIT 1000
LOAD CSV WITH HEADERS FROM "file:///artists.csv" as artists
CREATE (artist:Artist {artist: artists.artist_name, artist_id: artists.artist_id})

USING PERIODIC COMMIT 1000
LOAD CSV WITH HEADERS FROM "file:///artist_agencies.csv" as line 
CREATE (ag:Agency) - [:REPRESENTS] -> (ar:Artist {track_artist_uri:line.track_artist_uri})

到目前为止,我得到了这个,每个蓝色节点都是一个机构名称的副本。而不是只有一个代理节点通过 :REPRESENTS 关系连接到所有艺术家。结果

我想我的问题是我不知道如何通过这个中间的artist_agencies 表将艺术家表与agent_list 表联系起来。有没有更好的方法来做到这一点,还是我走在正确的轨道上?

谢谢!乔伊

4

1 回答 1

0

artist_agencies.csv查询需要在创建它们之间的关系之前找到合适的和节点AgencyArtist例如:

USING PERIODIC COMMIT 1000
LOAD CSV WITH HEADERS FROM "file:///artist_agencies.csv" as line
MATCH (ag:Agency) WHERE ag.agency = line.agency
MATCH (ar:Artist) WHERE ar.artist_id = line.artist_id
CREATE (ag)-[:REPRESENTS]->(ar)

旁白:该artist_agencies.csv文件不需要该artist_name列。

[更新]

如果artist_agencies.csv数据可能导致创建重复关系,请替换CREATE为(更昂贵的)MERGE以避免这种情况。并确保您没有重复AgencyArtist节点。

于 2019-11-07T01:16:33.503 回答