简体   繁体   English

Neo4j Cypher - 使用 LOAD CSV 创建节点和设置标签

[英]Neo4j Cypher - creating nodes and setting labels with LOAD CSV

I'm trying to use LOAD CSV to create nodes with the labels being set to values from the CSV.我正在尝试使用 LOAD CSV 创建节点,并将标签设置为 CSV 中的值。 Is that possible?那可能吗? I'm trying something like:我正在尝试类似的东西:

LOAD CSV WITH HEADERS FROM 'file:///testfile.csv' AS line
CREATE (x:line.label)

...but I get an invalid syntax error. ...但我得到一个无效的语法错误。 Is there any way to do this?有没有办法做到这一点?

bicpence,二分之一,

First off, this is pretty easy to do with a Java batch import application, and they aren't hard to write.首先,使用 Java 批量导入应用程序很容易做到这一点,而且编写起来也不难。 See this batch inserter example .请参阅 此批处理插入器示例 You can use opencsv to read your CSV file.您可以使用opencsv读取您的 CSV 文件。

If you would rather stick with Cypher, and if you have a finite set of labels to work with, then you could do something like this:如果您更愿意使用 Cypher,并且您有一组有限的标签可供使用,那么您可以执行以下操作:

USING PERIODIC COMMIT 1000
LOAD CSV WITH HEADERS FROM 'file:///testfile.csv' AS LINE
CREATE (n:load {lab:line.label, prop:line.prop});

CREATE INDEX ON :load(lab);

MATCH (n:load {lab:'label1'})
SET n:label1
REMOVE n:load
REMOVE n.lab;

MATCH (n:load {lab:'label2'})
SET n:label2
REMOVE n:load
REMOVE n.lab;

Grace and peace,恩典与平安,

Jim吉姆

Unfortunately not, parameterized labels are not supported不幸的是,不支持参数化标签

Chris克里斯

you can do a workaround - create all nodes and than filter on them and create the desired nodes, than remove those old nodes你可以做一个解决方法 - 创建所有节点,然后过滤它们并创建所需的节点,而不是删除那些旧节点

LOAD CSV WITH HEADERS FROM 'file:///testfile.csv' AS line
CREATE (tmp:line[1])
WITH tmp
CREATE (x:Person {name: labels(tmp)[0]})
WITH tmp
REMOVE tmp

paste this into http://console.neo4j.org to see example:将此粘贴到http://console.neo4j.org以查看示例:

LOAD CSV 
WITH HEADERS FROM "http://docs.neo4j.org/chunked/2.1.2/csv/import/persons.csv" AS csvLine
CREATE (p:tmp { id: toInt(csvLine.id), name: csvLine.name })
WITH p
CREATE (pp:Person { name: labels(p)[0]})
WITH p, pp
DELETE p
RETURN pp

I looked around at a few questions like this, and came to the conclusion that a nice concise way to handle these kinds of complex frustrations of not being able to easily add dynamic labels through 'LOAD CSV', is simply use your favorite programming language to read CSV lines, and produce a text output file of Cypher statements that will produce the Neo4j node/edge structure that you want.我环顾了几个这样的问题,得出的结论是,一种很好的简洁方法来处理无法通过“LOAD CSV”轻松添加动态标签的这些复杂挫败感,只需使用您最喜欢的编程语言读取 CSV 行,并生成 Cypher 语句的文本输出文件,该文件将生成所需的 Neo4j 节点/边缘结构。 Then you will also be able to edit the text file directly, to alter whatever you want to further customize your commands.然后,您还可以直接编辑文本文件,以更改您想要进一步自定义命令的任何内容。

I personally used Java given I am most comfortable with Java.鉴于我对 Java 最熟悉,我个人使用 Java。 I read each line of the CSV into a custom object that represents a row in my CSV file.我将 CSV 的每一行读入一个自定义对象,该对象代表我的 CSV 文件中的一行。 I then printed to a file a line that reflects the Cypher statement I wanted.然后,我将反映我想要的 Cypher 语句的行打印到文件中。 And then all I had to do was cut and paste those commands into Neo4j browser command line.然后我所要做的就是将这些命令剪切并粘贴到 Neo4j 浏览器命令行中。

This way you can build your commands however you want, and you can completely avoid the limitations of 'LOAD CSV' commands with Cypher这样,您可以根据需要构建命令,并且可以完全避免使用 Cypher 的“LOAD CSV”命令的限制

Jim Biard's answer works but uses PERIODIC COMMIT which is useful however deprecated. Jim Biard 的回答有效,但使用了PERIODIC COMMIT ,它很有用,但已被弃用。

I was able to write a query that:我能够编写一个查询:

  1. Loads from CSV从 CSV 加载
  2. Uses multiple transactions使用多个事务
  3. Creates nodes创建节点
  4. Appends labels附加标签
  5. Will work for 4.5 and onwards适用于 4.5 及更高版本
:auto LOAD CSV WITH HEADERS FROM 'file:///nodes_build_ont_small.csv' AS row
CALL { 
        with row
        call apoc.create.node([row.label], {id: row.id})
        yield node
        return null
} IN TRANSACTIONS of 100 rows 
return null

Seems that apoc procedures are more useful then the commands themselves since this is not possible (at least in my attempts) with CREATE .似乎 apoc 过程比命令本身更有用,因为使用CREATE这是不可能的(至少在我的尝试中)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM