簡體   English   中英

py2neo-匹配並合並來自兩個不同csv的兩個節點,並創建關系

[英]py2neo - Match and Merge two nodes coming from two different csv, and create relationship

我有一個關系數據庫,並將表轉換為csv文件。 我導入了其中的2個,並通過指定要選擇的列來創建節點,如以下代碼所示:

import csv
from py2neo import neo4j, authenticate, Graph, Node, cypher, rel, Relationship
authenticate("localhost:7474", "neo4j", "my_password")
graph_db = Graph()
graph_db.delete_all()

"""import all rows and columns of csv files"""

with open('File1.csv', "rb") as abc_file, open('File2.csv', "rb") as efg_file:
data1 = csv.reader(abc_file, delimiter=';')
data2 = csv.reader(efg_file, delimiter=';')
data1.next()
data2.next()

"""Create the nodes for the all the rows of "Contact Email" column of abc_file"""
rownum = 0
for row in abc_file:
    nodes1 = Node("Contact_Email", email=row[0])
    contact_graph = graph_db.create(nodes1)

"""Create the nodes for the all the rows of "Building_Name" and "Person_Created" 
   columns of efg_file"""
rownum = 0
for row in efg_file:
    nodes2 = Node("Building_Name", name=row[0])
    nodes3 = Node("Person_Created", name=row[1])
    building_graph = graph_db.create(nodes2, nodes3)

假設在“ File1.csv”的“ Contact_Email”列下有60封電子郵件,這是Primary_Key。 它在“ Person_Created”列下的“ File2.csv”中用作Foreign_Key。 在“建築物名稱”下指定了14座建築物,並在“ Person_Created”列中指定了相應的電子郵件。 我的問題是:

1)如何將File2.csv“ Person_Created”列中的14封電子郵件與File1.csv“聯系電子郵件”列中的電子郵件進行匹配,以避免重復

2)以及如何在“建築物名稱”(在File2.csv中)和“ Person_Created”(在File1.csv中)之間建立沒有重復的關系。例如“ Building1234 is DESIGNED_BY abc@xyz.com”

如何在py2neo中使用/不使用密碼?

為聯系人電子郵件創建索引或唯一約束。

命名節點的屬性(例如電子郵件)可能是一個好主意。

遍歷Person_Created時,使用電子郵件外鍵值創建具有屬性email的Contact Email節點。

由於索引/約束到位,將有條件地創建節點

在此迭代中,還要創建“創建的人”和“聯系人電子郵件”之間的關系。

Py2neo為此提供了許多唯一性功能。 查看此頁面以查看merge_one和朋友。 然后可以存儲由此返回的節點值,並將其用作唯一的關系和路徑。

請注意,為了獲得更高的性能,您可能需要查看Cypher事務或批處理。 沒有這些,每個動作都將需要調用服務器,並且規模很大,這很慢。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM