Neo4j CSV文件加载空单元格

问题描述 投票:2回答:2

我正在将一个基本的CSV文件加载到Neo4j数据库中,该数据库有两列 - “name”和“property”。 name列始终具有值,“property”列可以具有值或空格。我想将值与“property1”关系联系起来。

我正在使用此代码:

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

我收到一条错误消息:

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

任何帮助,将不胜感激。

csv neo4j cypher
2个回答
3
投票

您的查询有两个问题:

  1. 在第5行错过了一个关闭的人
  2. Properties不在第二个FOREACH的范围内,因为它在之前的FOREACH中声明(在FOREACH中声明的别名仅限于FOREACH条款中)

试试这个:

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

3
投票

另一种方法是使用WHERE仅在没有缺失值时才创建关系:

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)

仅当属性字段不为null时,才会创建链接和B节点。

© www.soinside.com 2019 - 2024. All rights reserved.