cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

How to add entities and relations with in CSV

Hello,

I have a CSV with columns a,R1,b,R2,c,r3,d

Here a,b,c and d are the values and R1 is the relationship which connects a and b R2 is the relationship which connects b and c . And R3 with c and d. How do I make this as a query in neo4j.

I'm new to this platform.

Thank you.

8 REPLIES 8

Hello @karthik1997 and welcome to the Neo4j community

  • Can you share a sample of your CSV?
  • Which label do you want to use for your nodes?
  • Is the direction of relationships important in your use case?

Regards,
Cobra

ameyasoft
Graph Maven
Try this:
Use LOAD CSV

MERGE (a:Test {name: row.a})
 MERGE (b:Test {name: row.b})
 MERGE (a)-[:R1]-(b)
 
 MERGE (c:Test {name: row.c})
 MERGE (b)-[:R2]-(c)
 
 MERGE (d:Test {name: row.d})
 MERGE (c)-[:R3]-(d)

Hello @ameyasoft

R1 R2 R3 are columns in CSV. Can we add them like [:R1] or do we have to do like [row.R1]. But giving like [row.R1] gave me an error.

Try this:

LOAD CSV WITH HEADERS FROM "file:///relations.csv" AS row
MATCH (f:Node), (s:Node)
WHERE f.Name = row.FromNode
AND s.Name = row.ToNode
CALL apoc.create.relationship(f, row.R1,{}, s) YIELD rel
REMOVE rel.noOp

Follow this link:

Whenever you use a CALL statement that has to return the value that comes out of the call YIELD. If you are using this with MATCH statements then you can use RETURN.

Try without that RETURN statement. Sometimes it works!

@ameyasoft

Thank you. Let me try it.

@ameyasoft how do i do this for c and d in one go? here we can connect a and b with r1 only.

LOAD CSV WITH HEADERS FROM "file:///relations.csv" AS row

MERGE (a:Test {name: row.a})
 MERGE (b:Test {name: row.b})
 CALL apoc.create.relationship(a, row.R1,{}, b) YIELD rel
 
 MERGE (c:Test {name: row.c})
 CALL apoc.create.relationship(b, row.R2,{}, c) YIELD rel1
 
 MERGE (d:Test {name: row.d})
CALL apoc.create.relationship(c, row.R3,{}, d) YIELD rel2