Glinty
Glinty

Reputation: 37

Error in importing data into neo4j via csv


I am new to neo4j.
I can't figure out what I am doing wrong?, please help.

LOAD CSV WITH HEADERS FROM "file:///C:\Users\Chandra Harsha\Downloads\neo4j_module_datasets\test.csv" as line
    MERGE (n:Node{name:line.Source})
    MERGE (m:Node{name:line.Target})
    MERGE (n)-[:TO{distance:line.dist}]->(m)

Error message:
Invalid input 's': expected four hexadecimal digits specifying a unicode character (line 1, column 41 (offset: 40)) "LOAD CSV WITH HEADERS FROM "file:///C:\Users\Chandra Harsha\Downloads\neo4j_module_datasets\test.csv" as line"

Upvotes: 0

Views: 390

Answers (1)

Pablissimo
Pablissimo

Reputation: 2905

You've got two problems here.

The immediate error is that the backslashes in the path are being seen as escape characters rather than folder separators - you either have to escape the backslashes (by just doubling them up) or use forward-slashes instead. For example:

LOAD CSV WITH HEADERS FROM "file:///C:\\Users\\Chandra Harsha\\Downloads\\neo4j_module_datasets\\test.csv" as line
    MERGE (n:Node{name:line.Source})
    MERGE (m:Node{name:line.Target})
    MERGE (n)-[:TO{distance:line.dist}]->(m)

However - Neo4j doesn't allow you to load files from arbitrary places on the filesystem anyway as a security precaution. Instead, you should move the file you want to import into the import folder under your database. If you're using Neo4j Desktop, you can find this by selecting your database project and clicking the little down-arrow icon on 'Open Folder', choosing 'Import' from the list.

enter image description here

Drop the CSV in there, then just use its filename in your file space. So for example:

LOAD CSV WITH HEADERS FROM "file:///test.csv" as line
    MERGE (n:Node{name:line.Source})
    MERGE (m:Node{name:line.Target})
    MERGE (n)-[:TO{distance:line.dist}]->(m)

You can still use folders under the import directory - anything starting file:/// will be resolved relative to that import folder, so things like the following are also fine:

LOAD CSV WITH HEADERS FROM "file:///neo4j_module_datasets/test.csv" as line
    MERGE (n:Node{name:line.Source})
    MERGE (m:Node{name:line.Target})
    MERGE (n)-[:TO{distance:line.dist}]->(m)

Upvotes: 0

Related Questions