Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
04-02-2020 10:28 AM
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
Solved! Go to Solution.
04-02-2020 12:56 PM
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)
04-02-2020 12:56 PM
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)
04-02-2020 01:19 PM
When i try the outflow one it say no change
04-02-2020 01:23 PM
Did you try same as I had mentioned?
04-02-2020 01:24 PM
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
04-02-2020 01:27 PM
I hope you have two distinct labels Country and Density?
04-02-2020 01:28 PM
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)
04-02-2020 01:35 PM
BTW if i want to conect when ori !=des should be before the last merge like WHERE ori<>des or before the first one?
04-02-2020 01:35 PM
I guess in your input file it is outflow rather than OUTFLOW
04-02-2020 01:36 PM
i check my csv i forgot that the 2 types include _TO
let me check
04-02-2020 01:48 PM
Almost done now i need to fix something i forgot
i need to include the name of the coutry to the relationship
04-02-2020 01:52 PM
try
match(c:Country)-[rel]-(d:Density) Set rel.country_name=c.name return rel
04-02-2020 01:52 PM
that at the end in replace of the last merge?
ok
04-02-2020 01:59 PM
or must be below the merge?
04-02-2020 02:02 PM
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
All the sessions of the conference are now available online