cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

LOAD CSV WITH HEADERS Relationship

Hello Im trying to make a relationship for a nodes that came from the same CSV file but i dont manage to create the relationship, how could i do it?

The csv for country looks something like thishas:

id_country, country
1 , a
2 , b
3 , c
4 , d
6 , e
. , .
and the csv for relation:

id_country, coun_id_country, id_type
1 , 2, inflow
1 , 3 , inflow
1 , 3 , outflow
2 , 1 , .
2 , 3,
2 , 4,
. , .,
id_country is origin coun_id_country is destiny
The points means more data, the country.csv generate 210 nodes

The relation i want to generate for each country is if id_type=INFLOW p2<-[INFLOW_TO]-p1 and id id_type is outflow p1-[:OUTFLOW]->p1

edit: I tried as some of the answers given in a post but it say nothing change

1 ACCEPTED SOLUTION

intouch_vivek
Graph Steward

Hi @TheDante500

Welcome to the community
I tried with below set
**country_density.csv **

id1 id2 direction
1 100 inflow
1 200 inflow
1 100 outflow
2 200 outflow
2 300 inflow
2 400 outflow

country.csv

id name
1 a
2 b
3 c
4 d
5 e
6 f

**Insert queries **
load csv with headers from 'file:///country.csv' as line
Merge (n:Country{id: line.id})
On Create set n.name = line.name

load csv with headers from 'file:///country_density.csv' as line
with line where line.direction="outflow"
Merge (m:Density{id: line.id2})
Merge (n:Country{id :line.id1})
Merge (n)<-[:OUTFLOW]-(m)

load csv with headers from 'file:///country_density.csv' as line
with line where line.direction="inflow"
Merge (m:Density{id: line.id2})
Merge (n:Country{id :line.id1})
Merge (n)-[:INFLOW]->(m)

View solution in original post

14 REPLIES 14

intouch_vivek
Graph Steward

Hi @TheDante500

Welcome to the community
I tried with below set
**country_density.csv **

id1 id2 direction
1 100 inflow
1 200 inflow
1 100 outflow
2 200 outflow
2 300 inflow
2 400 outflow

country.csv

id name
1 a
2 b
3 c
4 d
5 e
6 f

**Insert queries **
load csv with headers from 'file:///country.csv' as line
Merge (n:Country{id: line.id})
On Create set n.name = line.name

load csv with headers from 'file:///country_density.csv' as line
with line where line.direction="outflow"
Merge (m:Density{id: line.id2})
Merge (n:Country{id :line.id1})
Merge (n)<-[:OUTFLOW]-(m)

load csv with headers from 'file:///country_density.csv' as line
with line where line.direction="inflow"
Merge (m:Density{id: line.id2})
Merge (n:Country{id :line.id1})
Merge (n)-[:INFLOW]->(m)

When i try the outflow one it say no change

Did you try same as I had mentioned?

I try the same way as showed, with the difference of using the value (id1,id2,etc) thats of my csv, im trying to see what step i miss

I hope you have two distinct labels Country and Density?

well i put them as Destino(Density) Origen(Country)
this is the query i try
LOAD CSV WITH HEADERS FROM "file:///relacion.csv" as line with line where line.id_tipo="OUTFLOW"
MERGE(des:Destino{id: line.pai_id_pais})
MERGE(ori:Origen{id: line.id_pais})
MERGE (ori)<-[:OUTFLOW]-(des)

BTW if i want to conect when ori !=des should be before the last merge like WHERE ori<>des or before the first one?

I guess in your input file it is outflow rather than OUTFLOW

i check my csv i forgot that the 2 types include _TO
let me check

Almost done now i need to fix something i forgot
i need to include the name of the coutry to the relationship

try
match(c:Country)-[rel]-(d:Density) Set rel.country_name=c.name return rel

that at the end in replace of the last merge?
ok

or must be below the merge?

this is to set property of relationship.
and will run as fourth query
try
match(c:Country)-[rel]-(d:Density) Set rel.country_name=c.name return rel