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.

Loading CSV with Cypher with certain column names as properties

kasthuri
Node Clone

I have a CSV like this with the first row being header (H1, H2, H3,...) -

H1,H2,H3,H4,H5,...
a1,a2,a3,a4,a5,...
b1,b2,b3,b4,b5,...

I already have a neo4j database in which I use the first column (H1) to merge the nodes and use the rows of H2 and H3 to create a new node. When I strip off the header row, my code looks like this -

LOAD CSV FROM 'file:///mycsv.csv' AS row
MERGE (a:existingNode {name:row[0]})
CREATE (b:NewNode {name1:row[2], name2: row[1], name3: row[0]})
MERGE (a)-[:isAssociated]->(b)

How do I import the CSV so that the rest of the headers H4, H5,... are set as properties of the created node (based on H2 and H3) without having to assign each header under the created node? I want the header names as properties. Thanks.

2 REPLIES 2

I have not tried this, but two suggestions to try:

Maybe you can use the APOC map functions (http://neo4j-contrib.github.io/neo4j-apoc-procedures/3.5/utilities/map-functions/) to build a map with keys and values and set the properties like this:

CREATE (b:NewNode)
SET b = propertymap

OR using APOC load csv (http://neo4j-contrib.github.io/neo4j-apoc-procedures/3.5/export-import/load-csv/) instead of LOAD CSV and get the columns as a map directly:

CALL apoc.periodic.iterate('
CALL apoc.load.csv('/mycsv.csv', {header:true, mapping:{H4:{type:'int/string/...'}, H5:{type:'int/string/...'}}) YIELD map RETURN map
','
MERGE (a:existingNode {name:row.H1})
WITH a, map
CALL apoc.map.removeKeys(map,["H1"]) 
CREATE (b:NewNode)
SET b = map
MERGE (a)-[:isAssociated]->(b)
', {batchSize:10000, iterateList:true, parallel:true});

kasthuri
Node Clone

Thank you very much. I will certainly try and get back if I get into issues.