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.

Importing a thesaurus from CSVs

Hello! Thanks for your patience and help. It's end of the day Friday, and I'm just tired, so I'm going to ask for help. I'm trying to import a relatively straightforward thesaurus into a graph. I've organized the information into two CSVs (pipe delimited), one for the nodes and one for the relationships.

nodes.csv has a structure like this:
:ID | concept:string
1 | foo
2 | bar
3 | spam
4 | ham

relationships.csv has this structure like this:
:START_ID | :TYPE | :END_ID
1 | is_not | 2
1 | is_not |3
3 | is_like | 4

Now, when I try to import this, I'm using these queries:

CREATE CONSTRAINT ON (concept:Concept) ASSERT concept.id IS UNIQUE
CREATE INDEX ON :Concept(name)

LOAD CSV WITH HEADERS FROM "file:///nodes.csv" AS csvLine fieldterminator '|'
CREATE (c:Concept {id: toInteger(csvLine.id), name:csvLine.name})

USING PERIODIC COMMIT 500
LOAD CSV WITH HEADERS FROM "file:///relationships.csv" AS csvLine FIELDTERMINATOR "|"
MATCH (concept:Concept {id: toInteger(csvLine.conceptID)}),(concept:Concept {id:toInteger(csvLine.conceptID)})
CREATE (concept)-[:RELATIONSHIP {type: csvLine.type}]->(concept)

I end up with a lot of blank nodes, and no relationships. What am I doing wrong?

1 ACCEPTED SOLUTION

In your CSV file your header is called :ID and :START_ID, and in your cypher you're referring to it as csvLine.id and csvLine.conceptID which don't exist in your input CSV. So they are null. So you get blank nodes. 😉

You need instead to do something like this:

LOAD CSV WITH HEADERS FROM "file:///nodes.csv" AS csvLine fieldterminator '|'
CREATE (c:Concept {id: toInteger(csvLine.`:ID`), name:csvLine.`concept:string`})

In other words, match up your column metadata. Notice the backticks I'm using around the column names, this is so that you can include characters like : in a property name in Neo4j.

View solution in original post

2 REPLIES 2

In your CSV file your header is called :ID and :START_ID, and in your cypher you're referring to it as csvLine.id and csvLine.conceptID which don't exist in your input CSV. So they are null. So you get blank nodes. 😉

You need instead to do something like this:

LOAD CSV WITH HEADERS FROM "file:///nodes.csv" AS csvLine fieldterminator '|'
CREATE (c:Concept {id: toInteger(csvLine.`:ID`), name:csvLine.`concept:string`})

In other words, match up your column metadata. Notice the backticks I'm using around the column names, this is so that you can include characters like : in a property name in Neo4j.

Hi David, thank you very much, this worked for me!