2015-07-04 105 views
2

我正在使用csv批量導入py2neo(在here中描述)將rdb轉換爲neo4j。從另一個「people.csv」文件中的「電子郵件」節點是之前創建與已有節點創建關係

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........ 

,並有「電子郵件」節點的唯一約束:我組織這樣的代碼。在以上幾行中,我想通過關係將「created_by」,「delivered_to」和「delivered_by」節點與之前創建的「email」節點合併。當我運行代碼時,它給出了錯誤email already declared,並沒有從csv創建任何節點。如何組織密碼查詢以防止此錯誤?謝謝。

回答

3

您有3個節點,您試圖在查詢中調用email。您需要重命名其中兩個,以便沒有任何重複項。

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

MATCH ING或MERGE ING,如果你的:(如email和其他人在你上面的例子)之前提供的名字,你結合匹配或合併的節點,以這個名字。您無法爲多個節點提供相同的名稱。

如果您稍後在查詢中不打算使用這些名稱,我建議不要將它們命名。例如:

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

將合併您的Email節點而不保留對其的引用。從我的代碼中可以看出,我認爲你可以排除這些名字。

+0

運行良好。謝謝 – yalcinm1