将CSV数据中的关系加载到neo4j db中 [英] Loading relationships from CSV data into neo4j db

查看:119
本文介绍了将CSV数据中的关系加载到neo4j db中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Neo4j 2.1.7

Neo4j 2.1.7

尝试通过我在CSV中收到的信息大规模连接一堆节点,如下所示:

Attempting to mass-connect a bunch of nodes via information I've received in a CSV, which looks like:

person_id,book_id,relationship
111,AAA,OWNS
222,BBB,BORROWS
333,AAA,BORROWS

节点:Person 和<$通过 LOAD CSV CREATE :Book >语句,并且已经存在于数据库中。现在,我想在:Person :Book 之间的关系的CSV上方加载此文件。关系是在CSV本身中定义的。

The nodes :Person and :Book used in this CSV were successfully loaded via LOAD CSV and CREATE statements, and already exist in the database. Now, I'd like to load this above CSV of relationships between :Person and :Book. The relationships are defined in the CSV itself.

LOAD CSV WITH HEADERS FROM "file:data.csv" AS row
MATCH (person:Person { personID: row.person_id })
MATCH (book:Book { bookID: row.book_id })

当然,如果我提供了一个特定的名称,则下一个 MERGE 命令有效( [:OWNS] [:BORROWS] 等),但是如您所见,我的关系由传入数据提供。

Sure, the next MERGE command works if I supply a specific name ([:OWNS], [:BORROWS], etc.) but as you can see, my relationships are supplied by the incoming data.

但是,我希望在 MERGE 中定义的关系不是硬编码字符串,而是来自第3列的数据我的CSV代替。类似于:

However, I'd like the relationship defined in MERGE to not be a "hard-coded" string, but come as data from the 3rd column of my CSV instead. Something along the lines of:

MERGE(人)-[row.relationship]->(书)

这可能吗?

PS:我已经尝试过语法以及-[:row.relationship]-> 都无济于事(语法错误)

PS: I've tried the syntax above, and also -[:row.relationship]->, both to no avail (syntax errors)

推荐答案

我认为LOAD CSV是不可能的。您需要对输入数据和集合进行一些技巧。如果输入csv中的关系包含 OWNS ,则创建一个其中包含一个的集合,否则创建一个空集合。对 BORROWS 关系值执行相同的操作。就像这样...

I don't think it is possible with LOAD CSV. You need to do a little trickery with the input data and collections. If the relationship in the input csv contains OWNS create a collection with a one in it otherwise create an empty collection. Do the same for the BORROWS relationship value. It will be something like this...

...
case when row.relationship = "OWNS" then [1] else [] end as owns
case when row.relationship = "BORROWS" then [1] else [] end as borrows
foreach(x in owns | MERGE (person)-[:OWNS]->(book))
foreach(x in borrows | MERGE (person)-[:BORROWS]->(book))
...

这篇关于将CSV数据中的关系加载到neo4j db中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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