Load nodes into Neo4j from CSV according to their id?

I have the following CSV:

id  attr   value
1   abc    1.1    
1   eww    -9.4  
1   ssv    likj
2   we2    1 
2   eww   900  
3   kuku   -91  
3   lulu   383
3   ssv    bubu 

I would like to create 3 nodes that consists of:

Node 1: {id:1, abc: 1.1, eww: -9.4, ssv: "likj"}
Node 2: {id:2, we2: 1, eww: 900}
Node 3: {id:3, kuku: -91, lulu: 383, ssv: "bubu"}

How can I build it in Neo4j cypher?

>Solution :

I assume you have found the documentation about LOAD CSV , so I’ll skip that. AND I am assuming that the nodes will have a MyThing label , and that id is a property, so not the internal Neo4j id.

Before doing the import, create a constraint:

CREATE CONSTRAINT ON (n:MyLabel) ASSERT n.id IS UNIQUE

You also may want to install the apoc plugin, because it has a bunch a very nice functions in it for cases like this.

then, something like this:

LOAD CSV ..  AS line

WITH line.id AS id, 
     COLLECT([line.attr,line.value]) AS keyValuePairs
WITH id,
     apoc.map.fromPairs(keyValuePairs) AS map

MERGE (n:MyLabel {id:id})
SET n += map

Leave a Reply