Neo4j CSV file load with empty cells

蓝咒 提交于 2019-12-13 17:07:44

问题


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:

  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)
)



回答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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!