Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Neo4j Cypher: MERGE conditionally with values from LOAD CSV

Tags:

csv

neo4j

cypher

I'm trying to import from a CSV where some lines have an account number and some don't. Where accounts do have numbers I'd like to merge using them: there will be records where the name on an account has changed but the number will always stay the same. For the other records without an account number the best I can do is merge on the account name.

So really I need some kind of conditional: if a line has a account number, merge on that, else merge on account name. Something like...

LOAD CSV WITH HEADERS FROM 'file:///testfile.csv' AS line
MERGE (x:Thing {
  CASE line.accountNumber WHEN NULL
    THEN name: line.accountName
    ELSE number: line.accountNumber
  END
})
ON CREATE SET
x.name = line.accountName,
x.number = line.accountNumber

Though of course that doesn't work. Any ideas?

like image 896
bicpence Avatar asked Dec 19 '22 12:12

bicpence


1 Answers

To test for a 'NULL' value in a .csv file in LOAD CSV, you have to test for an empty string.

testfile.csv

acct_name,acct_num
John,1
Stacey,2
Alice,
Bob,4

This assumes the account names are unique...

LOAD CSV WITH HEADERS FROM 'file:///testfile.csv' AS line

// If acct_num is not null, merge on account number and set name if node is created instead of found.
FOREACH(number IN (CASE WHEN line.acct_num <> "" THEN [TOINT(line.acct_num)] ELSE [] END) |
    MERGE (x:Thing {number:number})
    ON CREATE SET x.name = line.acct_name
)

// If acct_num is null, merge on account name. This node will not have an account number if it is created instead of matched.
FOREACH(name IN (CASE WHEN line.acct_num = "" THEN [line.acct_name] ELSE [] END) |
    MERGE (x:Thing {name:name})
)
like image 80
Nicole White Avatar answered Jan 17 '23 19:01

Nicole White