Home » NoSQL » Neo4j » Neo4j: creating nodes and relationship with CSV file

Neo4j: creating nodes and relationship with CSV file

I’m sure that you don’t want to get you hands dirty by writing cypher query to create hundreds of nodes and relationship. In this talk, I’ll show how to make node and relationship from CSV file. First, we’ll make several user nodes and then we’ll continue with creating their relationship. To do this, we need nodes representation in CSV file. Each row in the CSV file will represent a node. Don’t forget to write a unique id for it. For example, I’ll create friend relationship representation in Neo4j. In the CSV file, I place 5 person data as follows :

later, I save the CSV file with a name (nodes.csv). The file show that each person has a unique ID and also a name, and ready to be imported in Neo4j. Use this following cypher query to import that file :

After successfully imported, the next step is creating necessary relationship for each node. To create a relationship, we must first define the relationship itself by the ids in a new CSV file. In the second CSV file, we create relation for each user like this (name it with relation.csv) :

In this case, relation.csv shows that Alice has friend and his name is Bob, and so forth. We can then import to Neo4j by this following query :

So now you have your nodes are all connected.

Leave a comment

Your email address will not be published. Required fields are marked *