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.

Single CSV - problem creating nodes and relations

hi i'm new to Neo4j i have trouble generating model.

Database - it's a CSV -
Link a CSV

Model -
link a arrows model

The code I use to generate the model

// Create unique property constraint
// Replace:
//   'LabelName' with node label
//   'propertyKey' with property that should be unique
CREATE CONSTRAINT ON (h:CODUSU) ASSERT h.Id IS UNIQUE;
CREATE CONSTRAINT ON (r:REGION) ASSERT r.Id IS UNIQUE;
CREATE CONSTRAINT ON (c:AGLOMERADO) ASSERT c.Id IS UNIQUE;
CREATE CONSTRAINT ON (t:IV1) ASSERT t.Id IS UNIQUE;
 
//Cargar la DB
USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM 'file:///usu_hogar_T221.csv' AS row

//Crear Nodo CODUSU - Hogar
// el merge sirve para agrupar
CREATE (hogar:CODUSU {Id: row.CODUSU})
MERGE (region:REGION { region: row.REGION })
MERGE (aglomerado:AGLOMERADO { aglomerado: row.AGLOMERADO })
MERGE (Tipo_vivienda:IV1 { Tipo_vivienda: row.IV1 })
 
CREATE (hogar)-[:hogar_region]->(region)
CREATE (hogar)-[:hogar_aglomerado]->(aglomerado)
CREATE (hogar)-[:hogar_Tipo_vivienda]->(Tipo_vivienda)
 
//Crear Nodo Tipo_vivienda
 
CREATE (Tipo_vivienda:IV1 { Id: row.IV1, ambientes: row.IV2, Pisos_Interiores: row.IV3, Tiene_Agua: row.IV6, Tiene_Bano: row.IV8, Desague_de_bano: row.IV11, 3_cuadras_basural: row.IV12_1, zona_inundable: row.IV12_2, Villa_emergencia: row.IV12_3 })
MERGE (aglomerado:AGLOMERADO { name: (row.AGLOMERADO) })
CREATE (Tipo_vivienda)-[:Tipovivienda_aglomerado]->(aglomerado)
 
//Crear Nodo Aglomerado
 
CREATE (aglomerado:AGLOMERADO { Id: row.AGLOMERADO, MAS_500: row.MAS_500, PONDERA: row.PONDERA})
MERGE (region:REGION { region: row.REGION })
CREATE (aglomerado)-[:aglomerado_region]->(region)
 
//Crear Nodo Region
CREATE (region:REGION { Id: row.REGION})

problem - I can't generate the nodes and relationships

7 REPLIES 7

@nesfernandez2

can you run and return

USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM 'file:///usu_hogar_T221.csv' AS row return row limit 5;

which will simply report the 1st 5 rows from the CSV. Does it report the same column headers?

Dear Dana, thank you very much for your reply !!!! It is not possible to execute the query

Neo.ClientError.Statement.SemanticError

Executing queries that use periodic commit in an open transaction is not possible.

@nesfernandez2

the error message provides the detail as to move forward.

either
a. remove the USING PERIODIC COMMIT from this statement and thus

LOAD CSV WITH HEADERS FROM 'file:///usu_hogar_T221.csv' AS row return row limit 5;

or
b. preface the query with :auto

Dana, query runs OK

@nesfernandez2

Using the CSV provided in your initial problem descpription I copied to a local csv and only kept the 1st 5 lines and named the file dana5.csv.
If I run the cypher, via bin/cyphe-shell, to create the nodes/rels as-is then

  1. the cypher needs a final ; terminator.
  2. if I add the final ; teminator then this errors as
Invalid input '3_cuadras_basural': expected an identifier (line 13, column 158 (offset: 562))
"CREATE (Tipo_vivienda:IV1 { Id: row.IV1, ambientes: row.IV2, Pisos_Interiores: row.IV3, Tiene_Agua: row.IV6, Tiene_Bano: row.IV8, Desague_de_bano: row.IV11, 3_cuadras_basural: row.IV12_1, zona_inundable: row.IV12_2, Villa_emergencia: row.IV12_3 })"

if I then replay the cypher and only have it perform

 LOAD CSV WITH HEADERS FROM 'file:///dana5.csv' as row
                CREATE (hogar:CODUSU {Id: row.CODUSU})
                MERGE (region:REGION { region: row.REGION })
                MERGE (aglomerado:AGLOMERADO { aglomerado: row.AGLOMERADO })
                MERGE (Tipo_vivienda:IV1 { Tipo_vivienda: row.IV1 })

                CREATE (hogar)-[:hogar_region]->(region)
                CREATE (hogar)-[:hogar_aglomerado]->(aglomerado)
                CREATE (hogar)-[:hogar_Tipo_vivienda]->(Tipo_vivienda)
        ;

and thus the output indicates nodes/relationships were created, as evidence

0 rows
ready to start consuming query after 1787 ms, results consumed after another 0 ms
Added 10 nodes, Created 12 relationships, Set 10 properties, Added 10 labels
@neo4j>

as to the error of

Invalid input '3_cuadras_basural': expected an identifier (line 13, column 158 (offset: 562))
"CREATE (Tipo_vivienda:IV1 { Id: row.IV1, ambientes: row.IV2, Pisos_Interiores: row.IV3, Tiene_Agua: row.IV6, Tiene_Bano: row.IV8, Desague_de_bano: row.IV11, 3_cuadras_basural: row.IV12_1, zona_inundable: row.IV12_2, Villa_emergencia: row.IV12_3 })"

this failure is because a property name can not start with a numeric unless you enclose the property name in back-ticks, and thus

CREATE (Tipo_vivienda:IV1 { Id: row.IV1, ambientes: row.IV2, Pisos_Interiores: row.IV3, Tiene_Agua: row.IV6, Tiene_Bano: row.IV8, Desague_de_bano: row.IV11, `3_cuadras_basural`: row.IV12_1, zona_inundable: row.IV12_2, Villa_emergencia: row.IV12_3 })

but this then means every time you reference the property it must b wrapped in back ticks.

Further by right of your statement of

MERGE (Tipo_vivienda:IV1 { Tipo_vivienda: row.IV1 })

you define varaible Tipo_vivinda but then 5 statements later you use this variable again as

CREATE (Tipo_vivienda:IV1 { Id: row.IV1, ambientes: row.IV2, Pisos_Interiores: .... .... ....

and this will cause conflict as you cant redefine the same variable.

Thanks Dana!!!! So the query goes like this?

LOAD CSV WITH HEADERS FROM 'file:///usu_hogar_T221.csv' as row
                CREATE (hogar:CODUSU {Id: row.CODUSU})
                CREATE (Tipo_vivienda:IV1 { Id: row.IV1, ambientes: row.IV2, Pisos_Interiores: row.IV3, Tiene_Agua: row.IV6, Tiene_Bano: row.IV8, Desague_de_bano: row.IV11, cuadras_basural: row.IV12_1, zona_inundable: row.IV12_2, Villa_emergencia: row.IV12_3 })
                CREATE (aglomerado:AGLOMERADO { Id: row.AGLOMERADO, MAS_500: row.MAS_500, PONDERA: row.PONDERA})
                CREATE (region:REGION { Id: row.REGION})
                
                MERGE (region:REGION { region: row.REGION })
                MERGE (aglomerado:AGLOMERADO { aglomerado: row.AGLOMERADO })
                MERGE (Tipo_vivienda:IV1 { Tipo_vivienda: row.IV1 })

                CREATE (hogar)-[:hogar_region]->(region)
                CREATE (hogar)-[:hogar_aglomerado]->(aglomerado)
                CREATE (hogar)-[:hogar_Tipo_vivienda]->(Tipo_vivienda)
                CREATE (Tipo_vivienda)-[:Tipovivienda_aglomerado]->(aglomerado)
                CREATE (aglomerado)-[:aglomerado_region]->(region);

I have to delete de DB and doit from cero I sepouse

@nesfernandez2

so all good? problem solved?