Simon Redding
Simon Redding

Reputation: 21

Loading a tree structure into Neo4J from a CSV file

I'm wanting to import a big CSV file which contains a tree structure. Removing some of the complexity, we could say there are three columns in my CSV: - name - id - parent_id

id is a unique alphanumeric code (e.g. GB234231AB) and parent_id references it from many nodes.

I've managed to load the CSV in find and index the two columns, but can't work out the Cypher for the load of the relationships.

How do I easily create the tree in Neo4J?

Upvotes: 2

Views: 688

Answers (1)

Michael Hunger
Michael Hunger

Reputation: 41676

you'd do it like this, assuming you have an index on :Label(id) (where label is your node label for your tree elements).

Using Neo4j 2.1.3

if you loaded the parents and children

create index on :Label(id);

USING PERIODIC COMMIT 1000
LOAD CSV FROM "....tree.csv" as line
MERGE (n:Label {id:line.id}) ON CREATE SET n.name = line.name
;

then loading the relationships for the tree should be something like:

USING PERIODIC COMMIT 1000
LOAD CSV FROM "....tree.csv" as line
MATCH (parent:Label {id:line.parent_id})
MATCH (child:Label {id:line.id})
CREATE (child)-[:PARENT]->(parent)
;

Upvotes: 3

Related Questions