我是使用Cypher语言(Neo4J)的初学者,但我一直试图找到解决问题的答案,但没有成功。 我已成功加载具有Has_Month和Has_Day关系的Year(year),Month(month)和Day(day)的节点。 我还成功加载了Airports(airportCode)节点。
现在,我有一个包含以下数据的CSV文件:
Origin,YearDeparture,MonthDeparture,DayDeparture
AGP,2018,02,16
CDG,2017,05,24
...
我想在Day和Origin(这是一个已经创建的机场)之间建立关系。 我需要执行以下伪代码:
if YearDeparture = Year.year AND
MonthDeparture = Month.month AND
DayDeparture = Day.day AND
Origin = Airport.airportCode then
CREATE (d)-[:HAS_AIRPORT]->(a)
(where d is a Day and a is an airport)
我尝试了以下代码但未成功:
LOAD CSV WITH HEADERS FROM "File:///AirportLinkedToDay.csv" AS csvLine
WITH csvLine WHERE NOT csvLine.Origin IS NULL
MATCH (y:Year {year: csvLine.YearDeparture})
MATCH (m:Month {month: csvLine.MonthDeparture})
MATCH (d:Day {day: csvLine.DayDeparture})
MATCH (a:AIRPORT {airportCode: csvLine.Origin })
MATCH (y)-[:HAS_MONTH]->(m)-[:HAS_DAY]->(d)
MERGE (d)-[:HAS_AIRPORT]->(a)
非常感谢您的帮助。 如果有不清楚的地方,请不要犹豫。 M。
更新:创建分层日期部分(年,月,日节点)的代码:
WITH range(2012, 2025) AS years, range(1,12) as months
FOREACH(year IN years |
MERGE (y:Year {year: year})
FOREACH(month IN months |
CREATE (m:Month {month: month})
MERGE (y)-[:HAS_MONTH]->(m)
FOREACH(day IN (CASE
WHEN month IN [1,3,5,7,8,10,12] THEN range(1,31)
WHEN month = 2 THEN
CASE
WHEN year % 4 <> 0 THEN range(1,28)
WHEN year % 100 <> 0 THEN range(1,29)
WHEN year % 400 = 0 THEN range(1,29)
ELSE range(1,28)
END
ELSE range(1,30)
END) |
CREATE (d:Day {day: day})
MERGE (m)-[:HAS_DAY]->(d))))
更新:创建机场节点的代码:
LOAD CSV WITH HEADERS FROM "File:///Airports.csv" AS csvLine
CREATE (p:AIRPORT { airportCode: csvLine.Code })
答案 0 :(得分:0)
主要问题是由于LOAD CSV
将所有输入值视为字符串这一事实引起的。为了与整数值匹配,您需要通过TOINTEGER()
函数将输入字符串转换为整数。例如:
LOAD CSV WITH HEADERS FROM "File:///AirportLinkedToDay.csv" AS row
WITH row WHERE NOT row.Origin IS NULL
MATCH (y:Year {year: TOINTEGER(row.YearDeparture)})
MATCH (m:Month {month: TOINTEGER(row.MonthDeparture)})
MATCH (d:Day {day: TOINTEGER(row.DayDeparture)})
MATCH (a:AIRPORT {airportCode: row.Origin })
MATCH (y)-[:HAS_MONTH]->(m)-[:HAS_DAY]->(d)
MERGE (d)-[:HAS_AIRPORT]->(a)
此外,如果您在以下位置创建了indexes,则查询的性能会更高: