Neo4j-加载具有序列关系的CSV

时间:2019-05-02 20:07:22

标签: csv import neo4j cypher

我正计划将火车时刻表和车站从CSV加载到Neo4j中。

源数据

TrainNo TrainName   SEQ   StationCode   Arrival Departure Distance
1           TN_1          1      S1           8      9       0
1           TN_1          2      S2           10     11     10
1           TN_1          3      S3           12     1      15 
1           TN_1          4      S4           3      4       15
2           TN_2          1      S1         
2           TN_2          2      S2         
2           TN_2          3      S5         
2           TN_2          4      S6         
2           TN_2          5      S7         
2           TN_2          6      S8         

我需要建立这样的节点和关系

S1--(TrainNo,TrainName,SEQ,Arrival,Depature,Distance)--S2--(TrainNo,TrainName,SEQ,Arrival,Depature,Distance)--S3--(TrainNo,TrainName,SEQ,Arrival,Depature,Distance)-S4

基本上,TrainNo,TrainName,Seq,Arrival,Dapature和Distance将位于关系上,并且相同的关系将在车站之间形成一条路线。

Neo4j-3.5

1 个答案:

答案 0 :(得分:0)

您可以按火车和序列进行排序和分组

load csv with headers from "" as row
with row order by TrainNo, SEQ
with TrainNo, collect(row) as stations
unwind range(stations, size(stations)-2) as idx
with TrainNo, stations[idx] as start, stations[idx+1] as end
match (s1:Station {code:start.StationCode})
match (s2:Station {code:end.StationCode})
// depends on your model
create (s1)-[:ROUTE {train:TrainNo}]->(s2);

// alternative
// move this before the unwind
match (train:Train {trainNo:TrainNo})
create (s1)-[:LEAVES]->(l:Leg)-[:ENTERS]>(s2)
craete (l)-[:OF_TRAIN]-(train);