Search code examples
csvneo4jcypherload-csv

Loading CSV with Cypher with certain column names as properties


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.


Solution

  • Assuming you know the names of the first 3 elements of the CSV header, this query:

    • puts the header row back in the file and uses LOAD CSV WITH HEADER,
    • uses the apoc.map.removeKeys function to create a map without the first 3 elements of each row, and
    • uses the SET b += ... operation to add the elements of that map to b (without touching other existing properties with different names)
    LOAD CSV WITH HEADERS FROM 'file:///mycsv.csv' AS row
    MERGE (a:existingNode {name: row.H1})
    CREATE (b:NewNode {name1: row.H3, name2: row.H2, name3: row.H1})
    SET b += apoc.map.removeKeys(row, ['H1', 'H2', 'H3'])
    MERGE (a)-[:isAssociated]->(b)