How to specify relationship type in CSV?

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 you want to create people whose relationship with the project is indicated in it. I tried to do it as follows:

 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 these are barfs with:

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

since it cannot look like a dereferenced line in creating relationships. 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 can i make a link?

+1
source share
3 answers

You cannot right now, as this is structural information.

To do this, use the neo4j-import tool .

Either specify it manually, just like you, 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
source

Michael responds, however, I found that I can specify a relationship attribute, for example:

 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 a relationship type attribute instead of a label

0
source

this question is old, but there is a message from Mark Needham

which provide a great and easy solution using APOC

properly

 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: "YIELD rel" is important, so for the return part

0
source

Source: https://habr.com/ru/post/1276277/


All Articles