我创建了一个非常简单的CSV,因此我可以练习将CSV加载到Neo4j中。
CSV看起来像这样:
boxer_id name boxer_country total_wins bdate fought fight_id fight_location outcome
1 Glass Joe France 0 1/2/80 2 100 Las Vegas L
2 Bald Bull Turkey 2 2/3/81 1 100 Macao W
3 Soda Popinski Russia 6 3/4/82 4 101 Atlantic City L
4 Sandman USA 9 4/5/83 3 101 Japan W
我想制作2个节点,boxer
和fight
。
但是我在将拳击手连接到战斗时遇到了麻烦。
据我所知:
正如你所看到的,我成功阅读了节点,但我不知道如何创建拳击手和拳击比赛之间的关系。
我想做类似的事情:
CREATE (boxer)-[:AGAINST]->(boxer)
但这没有意义。我需要使用字段fought
,其中包含有关谁在环中遇到谁的信息。
任何建议都将不胜感激。我不确定如何在READ CSV环境中执行此操作。
这是我的代码:
// The goal here is to create a node called Boxer, and pull in properties.
LOAD CSV WITH HEADERS FROM
'file:///test.csv' AS line
WITH line, SPLIT(line.bdate, '/') AS bdate
CREATE (b:boxer {boxer_id: line.boxer_id})
SET b.byear= TOINT(bdate[2]),
b.bmonth= TOINT(bdate[0]),
b.bday = TOINT(bdate[1]),
b.name = line.name,
b.country = line.boxer_country,
b.total_wins = TOINT(line.total_wins)
// Now we make a node called Fight
LOAD CSV WITH HEADERS FROM
'file:///test.csv' AS line
CREATE (f:fight {fight_id: line.fight_id, fight_loc: line.fight_location})
// Now we set relationships
// ????
答案 0 :(得分:1)
你可以添加一些线来匹配你已经创建的拳击手,并创建它们与新创建的战斗之间的关系。我认为这些方面的内容可能对你有用......
LOAD CSV WITH HEADERS FROM
'file:///test.csv' AS line
MATCH (b1:boxer {boxer_id: line.boxer_id})
WITH line, b1
MATCH (b2:boxer {boxer_id: line.fought})
MERGE (f:fight {fight_id: line.fight_id})
CREATE (b1)-[:AGAINST]->(b2)
CREATE (b1)-[:FOUGHT_IN]->(f)
CREATE (b2)-[:FOUGHT_IN]->(f)
答案 1 :(得分:0)
一种选择是将战斗模型化为Boxer节点之间的关系,而不是创建战斗节点:
LOAD CSV WITH HEADERS FROM 'file:///test.csv' AS line
MERGE (b1:Boxer {boxer_id: line.boxer_id})
MERGE (b2:Boxer {boxer_id: line.fought})
CREATE (b1)-[f:fought]->(b2)
SET f.location = line.fight_location,
f.outcome = line.outcome
然而,将战斗模型建模为节点可能更有意义,因为它们是事件。在这种情况下,这样的事情:
LOAD CSV WITH HEADERS FROM 'file:///text.csv' AS line
MATCH (b:Boxer {boxer_id: line.boxer_id})
MERGE (f:fight {fight_id: line.fight_id})
ON CREATE SET f.location = line.fight_location
CREATE (b)-[r:FOUGHT_IN]->(f)
WITH r, CASE line.outcome WHEN "W" THEN [1] ELSE [] END AS win
FOREACH (x IN win | SET r.winner = TRUE)
请注意,我们将战斗的结果存储为:FOUGHT_IN
关系中的财产。
修改已更新以使用MERGE
以避免创建重复的战斗节点。使用MERGE
时,您还应该在运行导入脚本之前创建唯一性约束:CREATE CONSTRAINT ON (f:Fight) ASSERT f.fight_id IS UNIQUE;
。