简体   繁体   中英

Neo4j - Load CSV with Sequence Relationship

I am planning to load train schedule and stations into Neo4j from CSV.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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