0
votes

I would like to read in a csv file where the first two columns have node names, and the third column has the node relationship. Currently I use this in py2neo:

query2 = """

USING PERIODIC COMMIT

LOAD CSV WITH HEADERS FROM "file:///data.csv" AS line

MERGE (topic:Topic {name: line.Topic})

MERGE (result:Result {name: line.Result})

CREATE UNIQUE (topic)-[:DISCUSSES]->(result)

"""

How can I use the third column in the csv file to set the relationship, instead of having all relationships set as "DISCUSSES"?

I tried this, but it does not have a UNIQUE option:

query1 = """

USING PERIODIC COMMIT

LOAD CSV WITH HEADERS FROM "file:///data.csv" AS line

MERGE (topic:Topic {name: line.Topic})

MERGE (result:Result {name: line.Result})

MERGE (relation:Relation {name: line.Relation})

WITH topic,result,line

CALL apoc.merge.relationship(topic, line.Relation, {}, {}, result) YIELD rel as rel1

RETURN topic,result

"""

1
The latest neo4j versions no longer support CREATE UNIQUE. You should use MERGE instead.cybersam
Doesn't answer the question: Is there a way to set the relationships using a column in the csv file?e25548
Have you read the duplicate question and its accepted answer?: stackoverflow.com/questions/47808421/…cybersam
Thank you, yes, I read it and tried it on my problem set. APOC does not seem to have a UNIQUE option, which I need, as shown in the code above.e25548
OK, see my answer. You were actually close to having a good query.cybersam

1 Answers

0
votes

Actually, your second query is almost correct (except that it has an extraneous MERGE clause). Here is the corrected query:

USING PERIODIC COMMIT LOAD CSV WITH HEADERS FROM "file:///data.csv" AS line
MERGE (topic:Topic {name: line.Topic})
MERGE (result:Result {name: line.Result})
WITH topic, result, line
CALL apoc.merge.relationship(topic, line.Relation, {}, {}, result) YIELD rel
RETURN topic, result

The apoc.merge.relationship call is equivalent to doing a MERGE to create the relationship (with a dynamic label) if it does not already exist.