simone di tanna
simone di tanna

Reputation: 11

Create relationship on Neo4j using CSV files

I want to create a simple DB using some CSV files, like this: attore.csv, film.csv, recita.csv.

I created successfully the nodes with the label Attore and Film, simple files like this:

attore.csv:

    nome
    nome1
    nome2
    nome3

film.csv

    titolo
    titolo1 
    titolo2 
    titolo3 

and I was trying to create the relationship between them using recita.csv, in which each row is:

attore, film

Obv my primary key should be Attore(nome) and Film(titolo). I've been looking for so much time, I found many codes but no one is working, every try I made just run for something like an hour.

This is what I did:

I created the film nodes:

USING PERIODIC COMMIT 
LOAD CSV WITH HEADERS FROM "file:///film.csv" AS row
CREATE (n:Film) 
SET n = row, n.titolo = (row.titolo), n.durata = (row.durata), 
n.genere = (row.genere), n.anno = (row.anno), n.descrizione = 
(row.descrizione), n.regista = (row.regista), 
n.studio_cinematografico = (row.studio_cinematografico)

Then I created the attore nodes:

USING PERIODIC COMMIT 
LOAD CSV WITH HEADERS FROM "file:///attore.csv" AS row
CREATE (n:Attore) 
SET n = row, n.nome = (row.nome)

And then, after so much try I thought this was the exact way to create relationship, but didn't work:

USING PERIODIC COMMIT 
LOAD CSV WITH HEADERS FROM "file:///recita.csv" AS row
MATCH (attore:Attore {nome: row.attore})
MATCH (film:Film {titolo: row.film})
MERGE (attore)-[:RECITA]-(film);

I hope that someone could tell me the right way to create relationship, thanks.

EDIT: Examples of how are structured my files attore.csv:

nome
Brendan Fraser
Bett Granstaff
Leslie Nielsen
Martina Gedeck
Martin Sheen

film.csv:

titolo   durata   genere   anno   descrizione   regista   studio_cin
Mortdecai    80    Action  2015   *something*   David Koepp  Liongate

recita.csv:

attore       film
Johnny Depp   Mortdecai
Jason Momoa   Braven

Upvotes: 0

Views: 63

Answers (1)

Artem Nazarenko
Artem Nazarenko

Reputation: 71

Instead of the approach you are using. I would recommend to use Merge instead of Create, in this way you can avoid repetitions:

USING PERIODIC COMMIT 
LOAD CSV WITH HEADERS FROM "file:///attore.csv" AS row
MERGE (a:Attore{nome: row.nome})
RETURN a

the same is applied for the film.csv just separate properties with comma.

Second considering your csv docs format, check again the .csv format documentation. From what you have explained and if you want to make your code working, you need to have just two columns in your recita.csv (attore, film) and not 6 as you have (attore, film attore, film attore, film), because they are identical, but the column identifier (name) should be unique you don't need to repeat attore and film 3 times.

Please check the headers of all your files or expand your question with examples of your csv's.

Try to change your recita.csv file according to csv format requirements.

Upvotes: 0

Related Questions