The following queries assume the nodes already exist. If you also want to create the nodes as necessary, replace MATCH
with MERGE
.
Option 1:
You can have each row in the CSV file contain a variable number of node ids for the nodes that need to be connected together in a single chain, in order. In this case, the CSV file should not have a header row.
LOAD CSV FROM "file:///events.csv" AS ids
UNWIND [i IN RANGE(1, SIZE(ids)-1) | {a: ids[i-1], b: ids[i]}] AS pair
MATCH (a:Event {id: pair.a})
MATCH (b:Event {id: pair.b})
MERGE (a)-[:NEXT]->(b)
Option 2:
You can have each row in the CSV file contain just a pair of node ids that need to be connected together, in order. In this case, the CSV file could have a header row, as demonstrated by this example (using a
and b
as the headers).
LOAD CSV WITH HEADERS FROM "file:///events.csv" AS pair
MATCH (a:Event {id: pair.a})
MATCH (b:Event {id: pair.b})
MERGE (a)-[:NEXT]->(b)