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.

Where to start ? 2 questions

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 ? :

LOAD CSV WITH HEADERS from 'file:///result1.csv' as row with row where row.nom is not null
MERGE (n:name {Name: row.nom , Surname :row.surname})
 

- 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

4 REPLIES 4

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)
)

Screen Shot 2023-02-07 at 12.23.02 PM.png

Screen Shot 2023-02-07 at 12.22.49 PM.png 

that's really helpful thanks

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

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. 

Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

All the sessions of the conference are now available online