1
votes

I use Neo4J Community Edition version 3.2.1.

Consider this CSV-file with edges:

node1,relation,node2,type
1,RELATED_TO,2,Married
2,RELATED_TO,1,Married
1,RELATED_TO,3,Child
2,RELATED_TO,3,Child
3,RELATED_TO,4,Sibling
3,RELATED_TO,5,Sibling
4,RELATED_TO,5,Sibling

I have allready created the nodes for this. I then run the following csv load command:

load csv with headers from
"file:///test_dataset/edges.csv" as line
match (person1:Person {pid:line.node1}),
    (person2:Person {pid:line.node2})
create (person1)-[:line.relation {type:line.type}]->(person2)

But this returns the following error:

Invalid input '.': expected an identifier character, whitespace, '|', a length specification, a property map or ']' (line 5, column 24 (offset: 167))
"create (person1)-[:line.relation {type:line.type}]->(person2)"

It seems that I cannot use "line.relation" like this. How can I use the relation from the csv-file (second column) using csv load?

I have seen this answer, but I would like to do this using native query language.

To verify that the rest of the query is correct I have managed to create the edges correctly by hardcoding the relation like this:

load csv with headers from
"file:///test_dataset/edges.csv" as line
match (person1:Person {pid:line.node1}),
    (person2:Person {pid:line.node2})
create (person1)-[:RELATED_TO {type:line.type}]->(person2)
1

1 Answers

4
votes

Natively it's not possible to create a node with a dynamic label and a relationship with a dynamic type.

That's why there is a procedure for that.

If you want to do it natively and you know all the distinct value of your relation column, you can create many cypher script like that (one per value):

LOAD CSV WITH HEADERS FROM "file:///test_dataset/edges.csv" AS line
WITH line WHERE line.relation ='RELATED_TO'
MATCH (person1:Person {pid:line.node1})
MATCH (person2:Person {pid:line.node2})
CREATE (person1)-[:RELATED_TO {type:line.type}]->(person2)