neo4j:如何使用条件正确加载CSV?

时间:2016-08-14 15:12:40

标签: csv neo4j cypher

我要做的是将带有树数据结构的数据集从CSV导入到neo4j。节点与其父节点以及树中的深度级别(最大6)一起存储。所以我尝试使用CASE检查深度级别,然后像这样向其父级添加一个节点(到目前为止为了测试目的创建一个仅用于第一级的节点):

export FILEPATH=file:///Example.csv

CREATE CONSTRAINT ON (n:Node) ASSERT n.id IS UNIQUE;

USING PERIODIC COMMIT 500
LOAD CSV WITH HEADERS 
FROM {FILEPATH} AS line
WITH DISTINCT line,
line.`Level` AS level,
line.`ParentCodeID_Cal` AS parentCode,
line.`CodeSet` AS codeSet,
line.`Category` AS nodeCategory,
line.`Type` AS nodeType, 
line.`L1code` AS l1Code, line.`L1Description` AS l1Description, line.`L1Name` AS l1Name, line.`L1NameAb` AS l1NameAb,
line.`L2code` AS l2Code, line.`L2Description` AS l2Description, line.`L2Name` AS l2Name, line.`L2NameAb` AS l2NameAb,
line.`L3code` AS l3Code, line.`L3Description` AS l3Description, line.`L3Name` AS l3Name, line.`L3NameAb` AS l3NameAb,
line.`L1code` AS l4Code, line.`L4Description` AS l4Description, line.`L4Name` AS l4Name, line.`L4NameAb` AS l4NameAb,
line.`L1code` AS l5Code, line.`L5Description` AS l5Description, line.`L5Name` AS l5Name, line.`L5NameAb` AS l5NameAb,
line.`L1code` AS l6Code, line.`L6Description` AS l6Description, line.`L6Name` AS l6Name, line.`L6NameAb` AS l6NameAb,
codeSet + parentCode AS nodeId
CASE line.`Level` 
    WHEN '1' THEN CREATE (n0:Node{id:nodeId, description:l1Description, name:l1Name, nameAb:l1NameAb, category:nodeCategory, type:nodeType}) 
    ELSE 
END;

但我得到了这个结果:

  

警告:输入'S'无效:预期'l / L'(第17行,第3列(偏移量:   982))“案例级别”^

我知道语法有误。

我正在使用neo4j 3.0.4& Windows 10(使用neo4j shell运行D:\Program Files\Neo4j CE 3.0.4\bin>java -classpath neo4j-desktop-3.0.4.jar org.neo4j.shell.StartClient)。

1 个答案:

答案 0 :(得分:1)

您有几个语法错误。例如,CASE子句不能包含CREATE子句。

无论如何,您应该能够大大简化您的Cypher。例如,这可能适合您的需求:

USING PERIODIC COMMIT 500
LOAD CSV WITH HEADERS 
FROM {FILEPATH} AS line
WITH DISTINCT line, ('l' + line.Level) AS prefix
CREATE (:Node{
  id: line.CodeSet + line.ParentCodeID_Cal,
  description: line[prefix + 'Description'],
  name: line[prefix + 'Name'],
  nameAb: line[prefix + 'NameAb'],
  category: line.Category,
  type: line.Type})