从csv在两个节点之间创建具有不同属性的相同类型的多个关系

时间:2018-08-08 11:13:05

标签: neo4j relationship

在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)



此语法有效且仅创建11个关系,两个节点之间具有传入和传出关系。

它忽略了A-B,B-C,A-C和D-B之间的其他关系(另外2个关系)。

如何创建具有全部16种关系的图?

谢谢。

梅尔。

1 个答案:

答案 0 :(得分:0)

您的第二个查询正在合并关系(from)-[rel:RELATED_AS]->(to),因此Cypher会匹配该模式(如果存在)。因此,后续的匹配,但是由于ON CREATE语句,因此永远不会更新值。

由于您希望每次都可以使用以下内容替换语句时创建关系。

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)