使用Cypher在Neo4j中创建多个节点

时间:2016-07-08 08:48:06

标签: database graph neo4j

我有一个cypher查询的文本文件,格式如下。

create(n:`0`{`78`:78, `127`:127, `336`:336, `346`:346, `349`:349, `55`:55, `255`:255, `61`:61, `354`:354, `356`:356, `97`:97, `361`:361, `338`:338, `217`:217, `348`:348, `53`:53, `61`:61, `92`:92, `3`:3, `210`:210, `54`:54, `352`:352, `354`:354, `209`:209, `3`:3, `210`:210, `345`:345, `72`:72, `360`:360, `361`:361, `209`:209, `340`:340, `72`:72, `352`:352, `359`:359, `97`:97, `2`:2, `344`:344, `345`:345, `255`:255, `65`:65, `67`:67, `77`:77, `344`:344, `350`:350, `92`:92, `338`:338, `337`:337, `340`:340, `55`:55, `97`:97, `14`:14, `342`:342, `255`:255, `356`:356, `357`:357, `77`:77, `335`:335, `344`:344, `351`:351, `355`:355, `360`:360, `337`:337, `348`:348, `77`:77, `3`:3, `53`:53, `352`:352, `97`:97, `209`:209, `14`:14, `338`:338, `347`:347, `348`:348, `354`:354, `209`:209, `14`:14, `338`:338, `3`:3, `343`:343, `255`:255, `354`:354, `97`:97, `360`:360, `341`:341, `72`:72, `92`:92, `338`:338, `347`:347, `61`:61, `64`:64, `65`:65, `352`:352, `353`:353, `354`:354, `355`:355, `361`:361, `2`:2, `340`:340, `343`:343, `351`:351, `355`:355, `209`:209, `335`:335, `338`:338, `339`:339, `346`:346, `348`:348, `64`:64, `71`:71, `77`:77, `78`:78, `351`:351, `72`:72, `359`:359, `337`:337, `217`:217, `53`:53, `55`:55, `351`:351, `355`:355, `356`:356, `77`:77, `217`:217, `339`:339, `348`:348, `349`:349, `53`:53, `351`:351, `67`:67, `357`:357, `77`:77, `3`:3, `335`:335, `351`:351, `352`:352, `336`:336, `347`:347, `348`:348, `55`:55, `350`:350, `255`:255, `359`:359, `209`:209, `2`:2, `53`:53, `54`:54, `72`:72, `353`:353, `2`:2, `14`:14, `342`:342, `344`:344, `255`:255, `354`:354, `355`:355, `77`:77, `97`:97, `360`:360, `361`:361, `209`:209, `336`:336, `345`:345, `348`:348, `349`:349, `65`:65, `2`:2, `3`:3, `340`:340, `342`:342, `71`:71, `72`:72, `92`:92, `97`:97, `337`:337, `347`:347, `64`:64, `65`:65, `357`:357, `360`:360, `339`:339, `342`:342, `351`:351, `361`:361, `337`:337, `342`:342, `64`:64, `358`:358, `2`:2, `342`:342, `346`:346, `348`:348, `54`:54, `350`:350, `61`:61, `64`:64, `359`:359, `92`:92, `97`:97, `360`:360, `336`:336, `341`:341, `217`:217, `53`:53, `350`:350, `353`:353, `347`:347, `349`:349, `357`:357, `3`:3, `210`:210, `336`:336, `346`:346, `349`:349, `355`:355, `356`:356, `358`:358, `353`:353, `360`:360, `348`:348, `65`:65, `71`:71, `2`:2, `341`:341, `344`:344, `350`:350, `352`:352, `353`:353, `92`:92, `360`:360, `345`:345, `64`:64, `65`:65, `2`:2, `340`:340, `341`:341, `67`:67, `359`:359, `336`:336, `345`:345, `351`:351, `255`:255, `61`:61, `217`:217, `339`:339, `55`:55, `14`:14, `61`:61, `355`:355, `357`:357, `336`:336, `340`:340, `342`:342, `354`:354, `337`:337, `348`:348, `71`:71, `97`:97, `3`:3, `217`:217, `55`:55, `350`:350, `351`:351, `352`:352});


create(n:`1`{`77`:77, `127`:127, `337`:337, `338`:338, `346`:346, `348`:348, `356`:356, `209`:209, `335`:335, `337`:337, `348`:348, `53`:53, `67`:67, `335`:335, `346`:346, `67`:67, `77`:77, `53`:53, `67`:67, `2`:2, `339`:339, `346`:346, `348`:348, `53`:53, `55`:55, `71`:71, `353`:353});


create(n:`2`{`35`:35, `53`:53, `55`:55, `57`:57, `78`:78, `92`:92, `98`:98, `114`:114, `126`:126, `135`:135});


create(n:`3`{`7`:7, `14`:14, `34`:34, `50`:50, `53`:53, `55`:55, `59`:59, `65`:65, `78`:78, `92`:92, `127`:127, `137`:137, `168`:168, `170`:170, 
`137`:137, `164`:164, `202`:202});


create(n:`4`{`50`:50, `53`:53, `55`:55, `56`:56, `78`:78, `127`:127});

等等。 这是Cypher中Neo4j的常规创建语法。

我有4038个这样的节点。

Neo4j据说一次只接受一个查询。一次复制和粘贴一个查询是非常低效的。

此外,我还有另一个定义关系的文件,大小约为200000。一次复制和粘贴一个查询是完全不切实际的。

我需要一些帮助来自动化这个过程。

1 个答案:

答案 0 :(得分:1)

您可以使用WITH传递虚拟变量,以便能够一次执行多个查询:

create(n:`0`{`78`:78, `127`:127, `336`:336, `346`:346, `349`:349, `55`:55, `255`:255, `61`:61, `354`:354, `356`:356, `97`:97, `361`:361, `338`:338, `217`:217, `348`:348, `53`:53, `61`:61, `92`:92, `3`:3, `210`:210, `54`:54, `352`:352, `354`:354, `209`:209, `3`:3, `210`:210, `345`:345, `72`:72, `360`:360, `361`:361, `209`:209, `340`:340, `72`:72, `352`:352, `359`:359, `97`:97, `2`:2, `344`:344, `345`:345, `255`:255, `65`:65, `67`:67, `77`:77, `344`:344, `350`:350, `92`:92, `338`:338, `337`:337, `340`:340, `55`:55, `97`:97, `14`:14, `342`:342, `255`:255, `356`:356, `357`:357, `77`:77, `335`:335, `344`:344, `351`:351, `355`:355, `360`:360, `337`:337, `348`:348, `77`:77, `3`:3, `53`:53, `352`:352, `97`:97, `209`:209, `14`:14, `338`:338, `347`:347, `348`:348, `354`:354, `209`:209, `14`:14, `338`:338, `3`:3, `343`:343, `255`:255, `354`:354, `97`:97, `360`:360, `341`:341, `72`:72, `92`:92, `338`:338, `347`:347, `61`:61, `64`:64, `65`:65, `352`:352, `353`:353, `354`:354, `355`:355, `361`:361, `2`:2, `340`:340, `343`:343, `351`:351, `355`:355, `209`:209, `335`:335, `338`:338, `339`:339, `346`:346, `348`:348, `64`:64, `71`:71, `77`:77, `78`:78, `351`:351, `72`:72, `359`:359, `337`:337, `217`:217, `53`:53, `55`:55, `351`:351, `355`:355, `356`:356, `77`:77, `217`:217, `339`:339, `348`:348, `349`:349, `53`:53, `351`:351, `67`:67, `357`:357, `77`:77, `3`:3, `335`:335, `351`:351, `352`:352, `336`:336, `347`:347, `348`:348, `55`:55, `350`:350, `255`:255, `359`:359, `209`:209, `2`:2, `53`:53, `54`:54, `72`:72, `353`:353, `2`:2, `14`:14, `342`:342, `344`:344, `255`:255, `354`:354, `355`:355, `77`:77, `97`:97, `360`:360, `361`:361, `209`:209, `336`:336, `345`:345, `348`:348, `349`:349, `65`:65, `2`:2, `3`:3, `340`:340, `342`:342, `71`:71, `72`:72, `92`:92, `97`:97, `337`:337, `347`:347, `64`:64, `65`:65, `357`:357, `360`:360, `339`:339, `342`:342, `351`:351, `361`:361, `337`:337, `342`:342, `64`:64, `358`:358, `2`:2, `342`:342, `346`:346, `348`:348, `54`:54, `350`:350, `61`:61, `64`:64, `359`:359, `92`:92, `97`:97, `360`:360, `336`:336, `341`:341, `217`:217, `53`:53, `350`:350, `353`:353, `347`:347, `349`:349, `357`:357, `3`:3, `210`:210, `336`:336, `346`:346, `349`:349, `355`:355, `356`:356, `358`:358, `353`:353, `360`:360, `348`:348, `65`:65, `71`:71, `2`:2, `341`:341, `344`:344, `350`:350, `352`:352, `353`:353, `92`:92, `360`:360, `345`:345, `64`:64, `65`:65, `2`:2, `340`:340, `341`:341, `67`:67, `359`:359, `336`:336, `345`:345, `351`:351, `255`:255, `61`:61, `217`:217, `339`:339, `55`:55, `14`:14, `61`:61, `355`:355, `357`:357, `336`:336, `340`:340, `342`:342, `354`:354, `337`:337, `348`:348, `71`:71, `97`:97, `3`:3, `217`:217, `55`:55, `350`:350, `351`:351, `352`:352})

WITH 1 as whatever

create(n:`1`{`77`:77, `127`:127, `337`:337, `338`:338, `346`:346, `348`:348, `356`:356, `209`:209, `335`:335, `337`:337, `348`:348, `53`:53, `67`:67, `335`:335, `346`:346, `67`:67, `77`:77, `53`:53, `67`:67, `2`:2, `339`:339, `346`:346, `348`:348, `53`:53, `55`:55, `71`:71, `353`:353})

依旧......

您可以查看此示例以获取更多信息:Multiple unrelated queries in Neo4j Cypher?

您需要做的就是在每个请求之间有C / P WITH 1 as dummy,那么您有一个很大的请求。