与现有节点建立关系

时间:2015-07-04 12:06:54

标签: neo4j cypher

我正在使用here中描述的csv批量导入py2neo将rdb转换为neo4j。我组织了这样的代码:

delivery = "Delivery.csv"
graph = Graph("http://localhost:7474/db/data/")
graph.cypher.execute("CREATE CONSTRAINT ON (delivery:Delivery) ASSERT delivery.name IS UNIQUE")
with open(delivery, 'r+') as in_file:

    reader = csv.reader(in_file, delimiter=';')
    next(reader, None)
    batch = graph.cypher.begin()

    try:
        i = 0;
        j = 0;
        for row in reader:
            if row:
                name = strip(row[0])
                created_by = strip(row[1])
                created_on = strip(row[2])
                description = strip(row[3])
                delivered_to = strip(row[4])
                delivered_by = strip(row[5])
                barcode = strip(row[6])

                query = """
                    merge (delivery:Delivery {name:{a}})
                    merge (email:Email {email:{b}})
                    merge (created_on:Created_On {created_on:{c}})
                    merge (description:Description {describe:{d}})
                    merge (email:Email {email:{e}})
                    merge (email:Email {email:{f}})
                    merge (barcode:Barcode {code:{g}})
                  ##and there are some relationships##
                   """
                   batch.append(query, {"a": delivery, "b": created_by, "c": created_on "d": description, "e": delivered_to,
                                     "f": delivered_by, "g": barcode})
                  .........................................................
                  .........code goes as described in the hypherlink........

"电子邮件"节点是从另一个节点创建的#34; people.csv"文件和"电子邮件"有唯一性限制节点。在上面这些行中,我想合并" created_by"," deliver_to"和" deliver_by"带有"电子邮件"的节点之前通过关系创建的节点。当我运行代码时,它给出了错误email already declared并且没有从csv创建任何节点。如何组织密码查询以防止此错误?感谢。

1 个答案:

答案 0 :(得分:3)

您尝试在查询中调用email 3个节点。您需要重命名其中两个,以便没有重复项。

query = """
     merge (delivery:Delivery {name:{a}})
     merge (email:Email {email:{b}})
     merge (created_on:Created_On {created_on:{c}})
     merge (description:Description {describe:{d}})
     merge (email:Email {email:{e}})                # <- pick a different name
     merge (email:Email {email:{f}})                # <- pick a different name
     merge (barcode:Barcode {code:{g}})

MATCHMERGE时,如果您在:之前提供了一个名称(如上面示例中的email和其他人),那么您就是匹配的或将节点合并到该名称。您不能为多个节点提供相同的名称。

如果您稍后不在查询中使用这些名称,我建议不要命名它们。例如:

merge (:Email {email:{b}})

将合并您的Email节点而不保留对它的引用。从我的代码中可以看出,我认为你可以排除名称。