简体   繁体   中英

Create multiple relationships of same type with different properties between two nodes from csv

I am facing issue while creating multiple relationships of same type with different properties between two nodes in Neo4jDesktop.

Nodes dataset:
    File Name: 1.csv
    File Contents:
        Id,Desc
        A,Alpha
        B,Beta
        C,Charlie
        D,Doyce

Relationships Dataset:
    File Name: 2.csv
    File Contents:
        SeqNo,Date,Count,Weight,From,To
        0,2018-04-01,12,308,A,B
        1,2018-04-01,3,475,B,C
        2,2018-04-01,23,308,C,D
        3,2018-04-01,32,524,D,A
        4,2018-04-01,0,308,A,C
        5,2018-04-01,23,237,B,D
        6,2018-04-01,54,308,B,A
        7,2018-04-01,23,237,D,B
        8,2018-04-01,18,308,D,C
        9,2018-04-01,23,308,C,A
        10,2018-04-01,78,475,B,C
        11,2018-04-01,67,308,A,B
        12,2018-04-01,56,237,D,B
        13,2018-04-01,34,308,A,C
        14,2018-04-01,27,524,A,D
        15,2018-04-01,84,237,D,B


// Create Nodes
USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM "file:/1.csv" AS row
CREATE (:Node {Id: row.Id, Desc: row.Desc});

// Create Relationships
USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM "file:/2.csv" AS row
MERGE (from:Node {Id: row.From})
MERGE (to:Node {Id: row.To})
MERGE (from)-[rel:RELATED_AS]->(to)
ON CREATE SET rel.SeqNo  = toInt(row.SeqNo), 
              rel.Date   = row.flightDate, 
              rel.Count  = toInteger(row.Count), 
              rel.Weight = toFloat(row.Weight)



This syntax works and creates only 11 relationships, with incoming and outgoing relationships between two nodes.

It is ignoring the additional relationships between AB, BC, AC and DB (2 additional relationships).

How to create the graph with all the 16 relationships?

Thanks in advance.

Mel.

Your second query is MERGING the relationship (from)-[rel:RELATED_AS]->(to) so Cypher is matching that pattern if it exists. So the subsequent ones are matched but then the values are never updated because of the ON CREATE statement.

Since you want to create the relationships every time you could replace your statement with something like the following.

USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM "file:/2.csv" AS row
MERGE (from:Node {Id: row.From})
MERGE (to:Node {Id: row.To})
CREATE (from)-[rel:RELATED_AS {SeqNo: row.SeqNo, Date: row.flightDate, Count: toInteger(row.Count), Weight: toFloat(row.Weight)}]->(to)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM