Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
06-25-2020 12:00 PM
I am trying to import a csv into Neo4j that contains relationships between people, organizations, banks, assets, etc., where there is only one relationship per row. The column names are FROM, A.Type, TO, B.type, and then different properties. Here, the from and to labels have the name, and A-B.type say if it belongs to a person, org., etc. respectibly.
I managed to create the nodes depending on type with FOREACH
, like so:
FOREACH (_ IN CASE WHEN line.`A.type` = 'ASSET' THEN [1] ELSE [] END | MERGE (asset:Asset {Name:line.FROM}))
FOREACH (_ IN CASE WHEN line.`A.type` = 'BANK' THEN [1] ELSE [] END | MERGE (bank:Bank {Name: line.FROM}))
.
.
.
FOREACH (_ IN CASE WHEN line.`B.type` = 'ACTIVO' THEN [1] ELSE [] END | MERGE (asset:Asset {Name:line.TO}))
FOREACH (_ IN CASE WHEN line.`B.type` = 'BANCO' THEN [1] ELSE [] END | MERGE (bank:Bank {Name: line.TO}))
.
.
.
My problem now is creating the relationships per row, I've tried many different ways and nothing seems to work.
For Example:
FOREACH
to two different nodes depending on if they are on the FROM or TO column:WITH 'link' as line
LOAD CSV WITH HEADERS FROM url AS line
WITH line WHERE line.FROM = 'ASSET' AND line.TO = 'ORGANIZATION'
MERGE (a1:Asset {Name:line.FROM})
MERGE (o2:Organization {Name:line.TO})
CREATE (a1)-[con:PROPERTY_OF]->(o2)
FOREACH(n IN (CASE WHEN line.`A.type` = 'ASSET' THEN [1] ELSE [] END) |
FOREACH(t IN CASE WHEN line.`B.type` = 'ORGANIZATION' THEN [1] ELSE [] END |
MERGE (asset)-[ao:CONNECTED_WITH]->(organization)))
WITH asset, organization, line
CALL apoc.create.relationship(asset, line.RelationshipType, NULL, organization) YIELD rel
RETURN asset, rel, organization
And different variations of each, creating the nodes from scratch or matching them. Every time the query seems to work, it runs, but it creates no relationships or it creates a single relationship between new nodes that don't exist in the csv, with no name or label.
I am completely new to Cypher/Neo4j and am at my wits end, if someone could point out my mistakes, it would be HIGHLY appreciated.
Thank you in advance!
06-25-2020 12:22 PM
Goodevening,
I would create steps to complete the task.
There could also be a better and prefererd way to accomplish your import.
These steps I would take and involves reading the CSV file multiple times:
And maybe look at this tutorial:
Yours Kindly
Omer
02-19-2021 05:49 AM
Is there maybe a link to a more detailed minimal example?
09-16-2020 10:45 PM
Hey Omer,
Can you help me with following:
https://community.neo4j.com/t/need-help-with-correct-queries/25485/2
All the sessions of the conference are now available online