Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
02-07-2023 05:27 AM
Dear community, I come here as a beginner to ask for questions regarding where to look for answers.
Here are the 2 questions.
I have 1 database as follow
id ; name ; surname ; friendwith
1; A ; aaaaa ; B,C,D ;
2; B; bbbbb ; C,D ;
3; D; ddddd ; null ;
how can I import a csv file with these infos so that :
- I create a node for each name, with surname as property ?
could it be ? :
- for the friendship field, how to test if null and i not, for each value I create a node, ?
- for each node created, I create a relationship between each of the values within the friendwith field of my database ?
thanks a lot in advance
02-07-2023 09:27 AM
There are several ways to approach this. Here is one example:
load csv with headers from "file:///results1.csv" as row
merge(n:Person{name:row.name})
set n.surname = row.surname
with n, split(row.friendwith, ",") as friends
foreach(friend in friends |
merge(m:Person{name:friend})
merge(n)-[:HAS_FRIEND]->(m)
)
02-08-2023 12:29 AM
that's really helpful thanks
02-08-2023 01:54 AM
Thanks, that's super helpful !
New question arises :
here is the code :
LOAD CSV WITH HEADERS from 'file:///result2.csv' as row with row where row.id is not null
MERGE (n:Juggler {Name: row.nom})
SET n.Surname = row.prenom, n.Age = row.age, n.Email = row.mail
with n, split(row.aei, ";") as aeis
foreach (aei in aeis | merge (m:Juggler{Name:aei})
merge(n)-[:AEI]->(m)
)
with n, split(row.paig,";") as paigs
foreach (paig in paigs | merge (m:Juggler{Name:paig})
merge(n)-[:PAIG]->(m)
)
returns
Variable `row` not defined (line 5, column 15 (offset: 214))
"with n, split(row.paig,";") as paigs"
what's the matter ? Where does row stop being defined ?
greetings
O
02-08-2023 03:40 AM - edited 02-08-2023 03:43 AM
The ‘with’ statement pass variables from one phase of the query to the next, so any variables from before a ‘with’ call are out of scope after the ‘with’ clause if they are included in the ‘with.’ As such, ‘row’ goes out if scope after the first ‘with’. Since you need ‘row’ for your second ‘with’ in the split function, add ‘row’ to the first ‘with’ clause.
All the sessions of the conference are now available online