How to create relationship between rows present in csv file in Neo4j?

I am working on data for pension money withdrawal request. So data consists of 2 columns as shown below:

Operator ID, Activity Done
12, enter request
12, open
13, change
14, update
15, close

I wanted to create a graph like:
12----->12----->13----->14----->15----->15

I have tried many ways but was only able to create relationship between columns but not between rows.

Someone please help!

Hi. here is one approach to the query you are looking for

LOAD CSV WITH HEADERS from "{link to file}" as line
CREATE (n:Operator) 
SET  n.linenumber = linenumber(),
n.id = line.`Operator ID`,
n.activity = line.`Activity Done`

WITH n
MATCH (n1:Operator) WHERE n1.linenumber = n.linenumber-1
CREATE (n1)-[:HAS_RELATION]->(n)
REMOVE n.linenumber,n1.linenumber
RETURN n1,n

Using the line number.

1 Like

Thank you @jaedag for the idea.

When applying it to my dataset it is showing: The execution plan for this query contains the Eager operator, which forces all dependent data to be materialized in main memory before proceeding. What does it mean? Do I need to worry about it?

in the LOAD CSV use a periodic commit LOAD CSV - Cypher Manual so as to reduce the memory footprint

1 Like

Yes sorry. That's because you are creating nodes and then matching those same nodes in one query. You can split it into two queries like this to avoid the warning.

USING PERIODIC COMMIT 1000
LOAD CSV WITH HEADERS from "http://localhost:11001/project-619d0035-18f6-4531-bb33-5502f63c41a9/test.csv" as line
CREATE (n:Operator) 
SET  n.linenumber = linenumber(),
n.id = line.`Operator ID`,
n.activity = line.`Activity Done`;

MATCH (n:Operator)
MATCH (n1:Operator) WHERE n1.linenumber = n.linenumber-1
CREATE (n1)-[:HAS_RELATION]->(n)
REMOVE n.linenumber,n1.linenumber
RETURN n1,n;

This also uses the PERIODIC COMMIT suggested by @dana_canzano

2 Likes