加载具有多种关系的 CSV 到 Neo4j [英] Load CSV with multiple types of relationships to Neo4j

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

问题描述

假设我们有一个包含具有各种关系类型的节点的 CSV.是否有一个选项可以在一个查询中加载 CSV,允许将每种关系类型显示为关系名称,而无需将 CSV 分解为单独的文件(每个关系类型一个)?(我们不想将关系类型作为属性添加到 Edge).

Id1 |id2 |关系类型1 |2 |类型11 |3 |类型22 |3 |类型1...

我们想稍后展示 &使用类似于以下的查询查询数据:

MATCH l=(p:Id1) - [:type1] - (p:Id2) RETURN l;MATCH l=(p:Id1) - [:type2] - (p:Id2) RETURN l;

解决方案

您可以使用

注意: 请记住根据您使用的 Neo4j 版本安装 APOC 程序.查看版本兼容性矩阵.

Assuming we have a CSV that has nodes with various relationship types. Is there an option to load the CSV in one query allowing each relationship type to be displayed as the relationship name without breaking up the CSV to separate files (one per relationship type)? (We don't want to add the relationship type as a property to the Edge).

Id1 | Id2 | RelationshipType   
1 | 2 | type1  
1 | 3 | type2   
2 | 3 | type1  
... 

We want to later on display & query the data with queries similar to below:

MATCH l=(p:Id1) - [:type1] - (p:Id2) RETURN l;  
MATCH l=(p:Id1) - [:type2] - (p:Id2) RETURN l;

解决方案

You can do it using the APOC Procedure apoc.create.relationship.

Considering this CSV file:

Id1|Id2|RelationshipType
1|2|type1
1|3|type2
2|3|type1

The LOAD CSV query will be:

LOAD CSV WITH HEADERS FROM 'file:///sample.csv' AS line FIELDTERMINATOR '|'
WITH line
MERGE(node0:Node {id : line.Id1})
MERGE(node1:Node {id : line.Id2})
WITH node0, node1, line
CALL apoc.create.relationship(node0, line.RelationshipType, {}, node1) YIELD rel
RETURN *

The resultant graph will be:

Note: Remember to install APOC procedures according to the version of Neo4j you are using. Take a look in the Version Compatibility Matrix.

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

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