user6342908
user6342908

Reputation:

unable to import a csv file which contains empty spaces in its records into Neo4j graph databases

I am trying to create a graph by importing csv file to neo4j,but my records contains some empty spaces ,so neo4j not allowing to create a node

I am unable to replace empty space with anything other in graph databases my code

LOAD CSV WITH HEADERS FROM "https://s3.amazonaws.com/xyz/test.csv" AS db

merge (dbn:dbname {name: db.origin })
merge (dbs:dbschema {name: db.dest})
merge (dbo:dbobj {name: db.via})


create (dbn)-[:via]->(dbs)-[:to]->(dbo)
return dbn,dbs,dbo

the csv file data is something like this,

origin dest via

ny     hk      la
ny     moscow  ft
ny     london
ny     mumbai  dubai

now from ny to london is direct so no "via"

but when I create nodes I need a node for empty node let it be with name null

Upvotes: 0

Views: 513

Answers (2)

William Lyon
William Lyon

Reputation: 8546

You can use the coalesce() function to deal with null values in the csv. Coalesce evaluates to the first non-null argument passed to it, so if you want to use "emptyData" for the name property of the DBObj node when it does not appear in the csv file just do something like this:

LOAD CSV WITH HEADERS FROM "http://some.url.com/file.csv" AS row
MERGE (dbn:DBName {name: row.origin })
MERGE (dbs:DBSchema {name: row.dest})
MERGE (dbo:DBObj {name: coalesce(row.via, "emptyData")})
CREATE (dbn)-[:via]->(dbs)-[:to]->(dbo)
return dbn,dbs,dbo

Upvotes: 0

jvilledieu
jvilledieu

Reputation: 535

Perhaps you could ignore the lines where the via column has not value?

LOAD CSV WITH HEADERS FROM "https://s3.amazonaws.com/xyz/test.csv" AS db
WITH db
WHERE db.via IS NOT NULL
merge (dbn:dbname {name: db.referencing_DB_Name })
merge (dbs:dbschema {name: db.referencing_schema})
merge (dbo:dbobj {name: db.referencing_object_name})
merge (dbd:dbrdb {name: db.referenced_database})
merge (dbrs:dbrdsch {name: db.referenced_schema})
merge (dbro:dbrdobj {name: db.referenced_object_name})

create (dbn)-[:referencing_schema]->(dbs)-[:referencing_object_name]->(dbo)-[:referenced_database]->(dbd)-[:referenced_schema]->(dbrs)-[:referenced_object_name]->(dbro)
return dbn,dbs,dbo,dbd,dbrs,dbro

Upvotes: 0

Related Questions