Money_Badger
Money_Badger

Reputation: 567

Neo4j CSV file load with empty cells

I am loading a basic CSV file into Neo4j database which has got two columns - "name" and "property". The name column always has a value and "property" column can either have a value or a blank space. I would like to values to be linked with a relationship "property1".

I am using this code:

LOAD CSV WITH HEADERS FROM 'file:///fileName.csv' AS line
MERGE (Test_Document:A {name: line.name})
WITH line, Test_Document 
FOREACH (x IN CASE WHEN line.property IS NULL THEN [] ELSE [1] END |
  MERGE (Properties:B {property1: line.property})
WITH Test_Document, Properties
FOREACH (y IN CASE WHEN Properties IS NULL THEN [] ELSE [1] END |
  MERGE (Test_Document)-[:property1]->(Properties))

I am getting an error message:

Unexpected end of input: expected whitespace, LOAD CSV, START, MATCH, UNWIND, MERGE, CREATE, SET, DELETE, REMOVE, FOREACH, WITH, CALL, RETURN or ')' (line 8, column 54 (offset: 423))
"  MERGE (Test_Document)-[:property1]->(Properties))"

Any help would be appreciated.

Upvotes: 2

Views: 1595

Answers (2)

Fabio Lamanna
Fabio Lamanna

Reputation: 21552

Another approach would use WHERE to create relationships only when there are not with missing values as:

LOAD CSV WITH HEADERS FROM 'file:///fileName.csv' AS line
WITH line, line.name AS Name, line.property AS Property
MERGE (Test_Document:A {name: Name})
WITH Property
WHERE Property <> ""
MERGE (Properties:B {property1: Property})
MERGE (Test_Document)-[:property1]->(Properties)

This creates the link and the B node only when the property field is not null.

Upvotes: 3

William Lyon
William Lyon

Reputation: 8546

There are two problems with your query:

  1. Missing a closing paren on line 5
  2. Properties is not in scope for the second FOREACH since it is declared in the previous FOREACH (aliases declared within a FOREACH are only scoped to within that FOREACH clause)

Try this:

LOAD CSV WITH HEADERS FROM 'file:///fileName.csv' AS line
MERGE (Test_Document:A {name: line.name})
WITH line, Test_Document 
FOREACH (x IN CASE WHEN line.property IS NULL THEN [] ELSE [1] END |
  MERGE (Properties:B {property1: line.property})
  MERGE (Test_Document)-[:property1]->(Properties)
)

Upvotes: 4

Related Questions