问题
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.
回答1:
There are two problems with your query:
- Missing a closing paren on line 5
Properties
is not in scope for the secondFOREACH
since it is declared in the previousFOREACH
(aliases declared within aFOREACH
are only scoped to within thatFOREACH
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)
)
回答2:
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.
来源:https://stackoverflow.com/questions/43235307/neo4j-csv-file-load-with-empty-cells