在Neo4j中执行多行

时间:2017-11-17 11:13:22

标签: neo4j cypher

我正在尝试执行以下密码:

// Nodes created for Lessons, Submitter, Center and Topic
// Relations created 
// Uniqueness constraints.
CREATE CONSTRAINT ON (l:Lesson) ASSERT l.name IS UNIQUE;
// Load.
USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM
'file:///llis.csv' AS line
WITH line, SPLIT(line.LessonDate, '-') AS date
CREATE (lesson:Lesson { name: TOINT(line.`LessonId`) } )
SET lesson.year = TOINT(date[0]),
    lesson.month = TOINT(date[1]),
    lesson.day = TOINT(date[2]),
    lesson.title = (line.Title),
    lesson.abstract = (line.Abstract),
    lesson.lesson = (line.Lesson),
    lesson.org = (line.MissionDirectorate),
    lesson.safety = (line.SafetyIssue),
    lesson.url = (line.url)

但是,我不能在浏览器中执行此操作,因为它是两个单独的行。见错误:

节点(562)已存在,标签为Lesson,属性name = 0

我知道我打算在Neo4j-shell中这样做。我已经设法进入Neo4j-shell但是如何在那里运行这段代码呢?

2 个答案:

答案 0 :(得分:1)

将语法放在一个文件中,然后按如下方式运行:

Windows上的

type yourfile.cypher | bin\cypher-shell.bat -u <user> -p <password>
Linux上的

cat yourfile.cypher | bin/cypher-shell -u <user> -p <password>

希望这有帮助, 汤姆

答案 1 :(得分:0)

您的CSV数据可能包含多个具有相同LessionId值的行。如果是这样,每当Lesson(name)子句尝试使用现有CREATE创建第二个Lesson节点时,这将导致name上的唯一性约束违规。

以下版本的LOAD CSV查询使用MERGE代替CREATE,以避免重复Lesson个节点:

USING PERIODIC COMMIT
  LOAD CSV WITH HEADERS FROM 'file:///llis.csv' AS line
WITH line, SPLIT(line.LessonDate, '-') AS date
MERGE (lesson:Lesson { name: TOINT(line.LessonId) } )
ON CREATE SET
    lesson.year = TOINT(date[0]),
    lesson.month = TOINT(date[1]),
    lesson.day = TOINT(date[2]),
    lesson.title = (line.Title),
    lesson.abstract = (line.Abstract),
    lesson.lesson = (line.Lesson),
    lesson.org = (line.MissionDirectorate),
    lesson.safety = (line.SafetyIssue),
    lesson.url = (line.url);

如果ON CREATE实际创建了一个节点,则上述查询还使用SET仅执行MERGE操作。这有效地导致查询忽略CSV文件中重复先前看到的LessonId的所有行。如果由于某种原因,您实际上确实希望SET始终发生(即使对于具有重复LessonId值的行),您可以省略ON CREATE