使用CSV文件创建Neo4j数据库 [英] Create Neo4j database using CSV files

查看:1413
本文介绍了使用CSV文件创建Neo4j数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个 CSV 文件,我要转换为 Neo4j 数据库。它们如下所示:

I have 2 CSV files which I want to convert into a Neo4j database. They look like this:

第一个文件:

name,enzyme
Aminomonas paucivorans,M1.Apa12260I
Aminomonas paucivorans,M2.Apa12260I
Bacillus cellulosilyticus,M1.BceNI
Bacillus cellulosilyticus,M2.BceNI

second file 

name,motif
Aminomonas paucivorans,GGAGNNNNNGGC
Aminomonas paucivorans,GGAGNNNNNGGC
Bacillus cellulosilyticus,CCCNNNNNCTC

正如你可以看到的共同的因素是名称的有机体和。每个有机体将有几个,每个 Enzyme have 1 Motif Motifs 可以在酶之间相同。我使用以下语句创建我的数据库:

As you can see the common factor is the Name of the organism and the. Each Organism will have a few Enzymes and each Enzyme will have 1 Motif. Motifs can be same between enzymes . I used the following statement to create my database:

USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM "file1.csv" AS csvLine
MATCH (o:Organism { name: csvLine.name}),(e:Enzyme { name: csvLine.enzyme})
CREATE (o)-[:has_enzyme]->(e) //or maybe CREATE UNIQUE?

USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM "file2.csv" AS csvLine
MATCH (o:Organism { name: csvLine.name}),(m:Motif { name: csvLine.motif})
CREATE (o)-[:has_motif]->(m) //or maybe CREATE UNIQUE?

这会给我在第一行的错误。USING PERIODIC COMMIT 表示无效输入'S':expected 。如果我摆脱ti,我得到的下一个错误是 WITH是必需的CREATE和LOAD CSV(第6行,第1列)
MATCH(o:Organism {name:csvLine.name }),(m:Motif {name:csvLine.motif})
。我googled这个问题,导致我这个 answer < a>。我试过答案给予(刷新浏览器缓存),但问题仍然存在。我在这里做错了吗?查询是否正确?这个问题有另一个解决方案吗?任何帮助将非常感谢。

This gives me errors on the very first line at USING PERIODIC COMMIT which says Invalid input 'S': expected. If I get rid of ti, the next error I get is WITH is required between CREATE and LOAD CSV (line 6, column 1) "MATCH (o:Organism { name: csvLine.name}),(m:Motif { name: csvLine.motif})" . I googled this issue which led me to this answer . I tried the answer given ther (refreshing the browser cache) but the problem persists. WHat am I doing wrong here? Is the query correct? Is there an another solution to this issue? Any help will be greatly appreciated

推荐答案

您的查询有两个问题:


  1. 您不能使用file1.csv来引用本地文件,因为neo4j需要一个URL

  2. code> MATCH 在数据可能不是最初存在的情况下;您需要使用 MERGE ,这基本上就像您添加的创建唯一评论。

  1. You can't refer to a local file just with "file1.csv", because neo4j is expecting a URL
  2. You're using MATCH in cases where the data may not originally exist; you need to use MERGE there instead, which basically acts like the create unique comment you added.

我不知道你的具体错误消息的来源是什么,但是写作看起来不像这些查询可能工作。这是您的查询重新制定,以便他们将工作(我测试它在我的机器与您的CSV示例)

I don't know what the source of your specific error message is, but as written it doesn't look like these queries could possibly work. Here are your queries reformulated, so that they will work (I tested it on my machine with your CSV samples)

USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM "file:/home/myuser/tmp/file1.csv" AS csvLine
MERGE (o:Organism { name: coalesce(csvLine.name, "No Name")})
MERGE (e:Enzyme { name: csvLine.enzyme})
MERGE (o)-[:has_enzyme]->(e);

注意这里有3个合并语句( MERGE MATCH + CREATE 如果它不存在),以及我使用 file: URL。

Notice here 3 merge statements (MERGE basically does MATCH + CREATE if it doesn't already exist), and the fact that I've used a file: URL.

第二个查询的建立方式基本相同:

The second query gets formulated basically the same way:

USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM "file:/home/myuser/tmp/file2.csv" AS csvLine
MERGE (o:Organism { name:  coalesce(csvLine.name, "No Name")})
MERGE (m:Motif { name: csvLine.motif})
MERGE (o)-[:has_motif]->(m);

EDIT 我添加了 coalesce 在有机体的名称属性。如果CSV中的 name 有空值,那么查询将失败。 Coalesce保证,如果 csvLine.name 为null,那么你将返回No Name。

EDIT I added coalesce in the Organism's name property. If you have null values for name in the CSV, then the query would otherwise fail. Coalesce guarantees that if csvLine.name is null, then you'll get back "No Name" instead.

这篇关于使用CSV文件创建Neo4j数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆