I have a file with relationships in this format:
!comment
!comment
nodeID nodeName edgeType nodeID
nodeID nodeName edgeType nodeID
nodeID nodeName edgeType nodeID
I want to import the nodes and edges of that file into my neo4j database.
I tried the following steps:
- Create a unique constraint on node ids
- Read file, skip comment rows, create unique nodes from each row (skip row if node already exists)
- Read file, skip comment rows, create edges from each row
// Each node id is unique
CREATE CONSTRAINT ON (n:Node) ASSERT n.id IS UNIQUE
// For each row not starting by "!", create node if it doesn't exist
LOAD CSV FROM "file:///relationships.tsv" AS row
FIELDTERMINATOR '\t'
WITH row
WHERE NOT row =~ '^!.*'
CREATE (:Node {id: row[0], name: row[1]})
// For each row not starting by "!", create edge
LOAD CSV FROM "file:///relationships.tsv" AS row
FIELDTERMINATOR '\t'
WITH row
WHERE NOT row =~ '^!.*'
MATCH (n:Node), (m:Node)
WHERE n.id = row[0] AND m.id = row[3]
WITH n, m, row
CASE row[2]
WHEN 'F' THEN
CREATE UNIQUE (m)-[:Edge {type: 'friend'}]->(n)
WHEN 'P' THEN
CREATE UNIQUE (m)-[:Edge {type: 'partner'}]->(n)
END
The code above doesn't work. Being new to cypher I'm not sure what I am doing wrong. I would like ultimately to merge steps 2 and 3 to read the file once and be done with it. How can I import this data efficiently?