How to specify relationship type in CSV?

耗尽温柔 提交于 2019-12-20 03:23:13

问题


I have a CSV file with data like:

ID,Name,Role,Project
1,James,Owner,TST
2,Ed,Assistant,TST
3,Jack,Manager,TST

and want to create people whose relationships to the project are therein specified. I attempted to do it like this:

load csv from 'file:/../x.csv' as line 
match (p:Project {code: line[3]}) 
create (n:Individual {name: line[1]})-[r:line[2]]->(p);

but it barfs with:

Invalid input '[': expected an identifier character, whitespace, '|', a length specification, a property map or ']' (line 1, column 159 (offset: 158))

as it can't seem to dereference line in the relationship creation. if I hard-code that it works:

load csv from 'file:/../x.csv' as line 
match (p:Project {code: line[3]}) 
create (n:Individual {name: line[1]})-[r:WORKSFOR]->(p);

so how do I make the reference?


回答1:


Right now you can't as this is structural information.

Either use neo4j-import tool for that.

Or specify it manually as you did, or use this workaround:

load csv with headers from 'file:/../x.csv' as line 
match (p:Project {code: line.Project}) 
create (n:Individual {name: lineName})
foreach (x in case line.Role when "Owner" then [1] else [] end |
  create (n)-[r:Owner]->(p)
)
foreach (x in case line.Role when "Assistant" then [1] else [] end |
  create (n)-[Assistant]->(p)
)
foreach (x in case line.Role when "Manager" then [1] else [] end |
  create (n)-[r:Manager]->(p)
)



回答2:


Michael's answer stands, however, I've found that what I can do is specify an attribute to the relationship, like this:

load csv from 'file:/.../x.csv' as line 
match (p:Project {code: line[3]}) 
create (i:Individual {name: line[1]})-[r:Role { type: line[2] }]->(p)

and I can make Neo4j display the type attribute of the relationship instead of the label




回答3:


this question is old, but there is a post by Mark Needham

that provide a great and easy solution using APOC

as follow

load csv with headers from "file:///people.csv" AS row
MERGE (p1:Person {name: row.node1})
MERGE (p2:Person {name: row.node2})

WITH p1, p2, row
CALL apoc.create.relationship(p1, row.relationship, {}, p2) YIELD rel

RETURN rel

note: the "YIELD rel" is essential and so for the return part



来源:https://stackoverflow.com/questions/29312417/how-to-specify-relationship-type-in-csv

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!