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.

How to verify if a node exist and if not exist then a created during an import of .csv to neo4j

dairon
Ninja
Ninja

Hello, I am importing some .cvs files for my database in #neo4j, but I have the data of people in three different files, so when I import the data of the person from another file that has more data, I get an error when trying to import people nodes, because I already have other nodes with those dni (constraint) in my database.

So I want to create the new node or, if it exists, retrieve its pointer to create relationships with other nodes that I keep creating while I import. I have tried several things on the internet but I still can't find the solution.

 I have tried with apoc and "with" but I still can't find the solution. when this code finds another node with a person label and ID equal to the one entered, it gives me an error

 

LOAD CSV WITH HEADERS FROM 'file:/D:/ACCOUNT.csv' AS line FIELDTERMINATOR ';'

MERGE (persona :Persona { dni: line.DNI,
nombre: line.NOMBRE,
sexo: line.SEXO,
fechaNacimiento: line.FNACIMIENTO,
direccion: line.DIRECCION
})

 

I love neo4j because I feel connected!!!
1 ACCEPTED SOLUTION

I think that is the cause of your error.  There will be no issue for the scenario when the node with that 'dni' and other field values is created.  The issue occurs when you try to 'merge' with your code when the node with that 'dni' already exist and at least one of the other values in your 'merge' has changed. The merge will not find the node, so it tries to create it. You get an error because you have a unique constraint just on the value of 'dni.' You would write your more like this.

LOAD CSV WITH HEADERS FROM 'file:/D:/ACCOUNT.csv' AS line FIELDTERMINATOR ';'
MERGE (persona :Persona { dni: line.DNI})
set persona.nombre = line.NOMBRE,
persona.sexo = line.SEXO,
persona.fechaNacimiento = line.FNACIMIENTO,
persona.direccion = line.DIRECCION

 In your script, do you want to create the node if it does exist and then create the relationship, or do you want to skip the node and not create the relationships since the node did not exists?  If you want to create the non-existing node, you can refer to it afterward using the binding variable 'persona' when creating the relationship. Let's assume you want to related it to a node of type 'Role' with a role name of 'Read Only', you would do something like this:

LOAD CSV WITH HEADERS FROM 'file:/D:/ACCOUNT.csv' AS line FIELDTERMINATOR ';'
MERGE (persona :Persona { dni: line.DNI})
set persona.nombre = line.NOMBRE,
persona.sexo = line.SEXO,
persona.fechaNacimiento = line.FNACIMIENTO,
persona.direccion = line.DIRECCION
WITH persona
MATCH(r:Role{name: "Read Only"})
MERGE(persona)-[:HAS_ROLE]->(r)

View solution in original post

4 REPLIES 4

Try placing only the unique identifier in the merge clause as a property (I assume ‘dni’), and put the rest in a SET clause to set them. A match/merge clause will use all the properties when matching, so only places the one or ones that uniquely identify the node. All others are left to a SET 

But doing it that way in the MERGE will not give me an error? is that I have a CONSTRAINT in my node in the field ('dni').
What I would like is to verify if the node exists in my database and if it exists, keep a variable that points to said node and then in my script make relationships with other nodes to my existing node.
If it doesn't exist then create it new and keep a pointer variable, for the relationships to create.

I love neo4j because I feel connected!!!

I think that is the cause of your error.  There will be no issue for the scenario when the node with that 'dni' and other field values is created.  The issue occurs when you try to 'merge' with your code when the node with that 'dni' already exist and at least one of the other values in your 'merge' has changed. The merge will not find the node, so it tries to create it. You get an error because you have a unique constraint just on the value of 'dni.' You would write your more like this.

LOAD CSV WITH HEADERS FROM 'file:/D:/ACCOUNT.csv' AS line FIELDTERMINATOR ';'
MERGE (persona :Persona { dni: line.DNI})
set persona.nombre = line.NOMBRE,
persona.sexo = line.SEXO,
persona.fechaNacimiento = line.FNACIMIENTO,
persona.direccion = line.DIRECCION

 In your script, do you want to create the node if it does exist and then create the relationship, or do you want to skip the node and not create the relationships since the node did not exists?  If you want to create the non-existing node, you can refer to it afterward using the binding variable 'persona' when creating the relationship. Let's assume you want to related it to a node of type 'Role' with a role name of 'Read Only', you would do something like this:

LOAD CSV WITH HEADERS FROM 'file:/D:/ACCOUNT.csv' AS line FIELDTERMINATOR ';'
MERGE (persona :Persona { dni: line.DNI})
set persona.nombre = line.NOMBRE,
persona.sexo = line.SEXO,
persona.fechaNacimiento = line.FNACIMIENTO,
persona.direccion = line.DIRECCION
WITH persona
MATCH(r:Role{name: "Read Only"})
MERGE(persona)-[:HAS_ROLE]->(r)

I will try this solution right now, I was already wondering why it gave me an error, it is that the .csv files are from excel tables made by hand and they seem to have human errors in the duplicate data so, it is complicated when data is imported from non-linked data correctly.

That must be the error because when I ran my first script it never gave me an error, and the same data that existed in the database already entered was wrong.
Thank you

I love neo4j because I feel connected!!!