How to specify relationship type in CSV?

2019-03-02 19:34发布

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?

标签: csv neo4j cypher
3条回答
别忘想泡老子
2楼-- · 2019-03-02 19:47

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楼-- · 2019-03-02 19:47

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

查看更多
迷人小祖宗
4楼-- · 2019-03-02 19:50

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)
)
查看更多
登录 后发表回答