简体   繁体   English

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

[英]Neo4j - Load CSV with Sequence Relationship

I am planning to load train schedule and stations into Neo4j from CSV. 我计划从CSV加载火车时刻表和车站到Neo4j。

Source Data 来源数据

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         

I need to build nodes and relationship like this 我需要像这样构建节点和关系

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

Basically, the TrainNo, TrainName,Seq, Arrival, Depature and Distance will be on the relationships, and the same relationships will form a route between the stations. 基本上,TrainNo,TrainName,Seq,Arrival,Depature和Distance将在关系上,并且相同的关系将形成站之间的路线。

Neo4j - 3.5 Neo4j - 3.5

you can sort and group by train and sequence 您可以按列车和顺序进行排序和分组

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM