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:
- In this case, I changed the
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)
- I also tried a variation of the code for creating nodes:
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)))
- This time I used the APOC library to try generating dinamic relationships depending on the relashionship type:
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!