How to import relationships between nodes of the s

2019-08-09 07:26发布

问题:

I have two separate csv files that I need to import into my neo4j database. The first file contains all the nodes that I wish to import. The information is classified as follows:

                             id, Name 

                              1, Earth science

To import it, I successfully used the following code:

LOAD CSV WITH HEADERS FROM 'file:///Node_test.csv' AS line

CREATE (:Discipline { id: toInt(line.id), name: line.Name})

Now, I want to import my relationship file and create all the relationship between the nodes I just imported. The information in the relationship.csv file is classified as follows:

                 RelationshipID, parentID, relationship_type, childID

                              1, 2, IS_A_PARENT_DISCIPLINE_OF, 5

To import it, I used the following code, without success :

USING PERIODIC COMMIT 500 LOAD CSV WITH HEADERS FROM "file:///relationship_test.csv" AS csvLine

MATCH (DParent:Discipline { id: toInt(csvLine.parentID)}),(DChild:Discipline { id: toInt(csvLine.childID)})

CREATE (DParent)-[:IS_A_PARENT_DISCIPLINE_OF { id:toInt(csvLine.RelationshipID) } ]->(DChild)

Note: The result doesn't show any errors, it just returned no changes, no rows.

Please see the links below for other documentations I found regarding the subject; I have not found any documentation describing how to import csv files in order to create relationships between nodes of the same Label.

http://neo4j.com/docs/2.3.0-M01/cypherdoc-importing-csv-files-with-cypher.html

How do i create relationships for existing nodes by importing csv file in neo4j?

Spring Data Neo4j 4.0.0: Can't Create Relationship Between Nodes with the Same Label

回答1:

The following is the content of the import.cypher file that you need to create and run in the neo4j-shell to import the data...

Important thing is when matching and trying to create relationships you need to use the MERGE command

USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM "file:data/node.csv" AS row
CREATE (:Discipline{disciplineId: toInt(row.id), name: row.Name});

USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM "file:data/rels.csv" AS row
MATCH (parent:Discipline{disciplineId: toInt(row.parentID)})
MATCH (child:Discipline {disciplineId: toInt(row.childID)})
MERGE (parent)-[:IS_PARENT_DISCIPLINE_OF]->(child);