python使用py2neo創(chuàng)建neo4j的節(jié)點(diǎn)和關(guān)系
1.核心代碼
使用py2neo連接neo4j的方法:
from py2neo import Graph
graph = Graph("http://localhost:7474", auth=("neo4j", "neo4j"))
graph.delete_all() ?# 刪除已有的所有內(nèi)容根據(jù)dict創(chuàng)建Node:
from py2neo import Node
node = Node(**{"key":"value"})
graph.create(node)創(chuàng)建關(guān)系:
from py2neo import Relationship relation = Relationship(node1, relation, node2) graph.create(relation)
用到的工具函數(shù)是:
def create_relation(graph, match_node1: dict, match_node2: dict, relation: str, node1_label=None, node2_label=None): ? ? """自動(dòng)創(chuàng)建節(jié)點(diǎn)與關(guān)系 ? ? :param graph: 圖 ? ? :param match_node1: 節(jié)點(diǎn)1屬性 ? ? :param match_node2: 節(jié)點(diǎn)2屬性 ? ? :param relation: 關(guān)系 ? ? :param node1_label: 節(jié)點(diǎn)1的標(biāo)簽 ? ? :param node2_label: 節(jié)點(diǎn)2的標(biāo)簽 ? ? """ ? ? from py2neo import Node, Relationship ? ? from py2neo import NodeMatcher ? ? node_matcher = NodeMatcher(graph) ? ? node1 = node_matcher.match(**match_node1).first() ? ? # 自動(dòng)創(chuàng)建node ? ? if not node1: ? ? ? ? if node1_label: ? ? ? ? ? ? node1 = Node(node1_label, **match_node1) ? ? ? ? else: ? ? ? ? ? ? node1 = Node(**match_node1) ? ? node2 = node_matcher.match(**match_node2).first() ? ? if not node2: ? ? ? ? if node2_label: ? ? ? ? ? ? node2 = Node(node2_label, **match_node2) ? ? ? ? else: ? ? ? ? ? ? node2 = Node(**match_node2) ? ? # 創(chuàng)建關(guān)系 ? ? relation = Relationship(node1, relation, node2) ? ? graph.create(relation)
2.完整示例代碼
def create_relation(graph, match_node1: dict, match_node2: dict, relation: str, node1_label=None, node2_label=None):
? ? """自動(dòng)創(chuàng)建節(jié)點(diǎn)與關(guān)系
? ? :param graph: 圖
? ? :param match_node1: 節(jié)點(diǎn)1屬性
? ? :param match_node2: 節(jié)點(diǎn)2屬性
? ? :param relation: 關(guān)系
? ? :param node1_label: 節(jié)點(diǎn)1的標(biāo)簽
? ? :param node2_label: 節(jié)點(diǎn)2的標(biāo)簽
? ? """
? ? from py2neo import Node, Relationship
? ? from py2neo import NodeMatcher
? ? node_matcher = NodeMatcher(graph)
? ? node1 = node_matcher.match(**match_node1).first()
? ? # 自動(dòng)創(chuàng)建node
? ? if not node1:
? ? ? ? if node1_label:
? ? ? ? ? ? node1 = Node(node1_label, **match_node1)
? ? ? ? else:
? ? ? ? ? ? node1 = Node(**match_node1)
? ? node2 = node_matcher.match(**match_node2).first()
? ? if not node2:
? ? ? ? if node2_label:
? ? ? ? ? ? node2 = Node(node2_label, **match_node2)
? ? ? ? else:
? ? ? ? ? ? node2 = Node(**match_node2)
? ? # 創(chuàng)建關(guān)系
? ? relation = Relationship(node1, relation, node2)
? ? graph.create(relation)
def main():
? ? from py2neo import Graph
? ? graph = Graph("http://localhost:7474", auth=("neo4j", "neo4j"))
? ? graph.delete_all() ?# 刪除已有的所有內(nèi)容
? ? create_relation(graph, {"name": "小a", "age": 12}, {"name": "小b", "age": 22}, "relation1", )
? ? create_relation(graph, {"name": "小a", "age": 12}, {"name": "小c", "age": 32}, "relation2", "people", "people")
? ? create_relation(graph, {"name": "小c", "age": 32}, {"name": "小d", "age": 42}, "relation1", "people", "people")
if __name__ == '__main__':
? ? main()效果圖:

到此這篇關(guān)于python使用py2neo創(chuàng)建neo4j的節(jié)點(diǎn)和關(guān)系的文章就介紹到這了,更多相關(guān)python使用py2neo創(chuàng)建neo4j的節(jié)點(diǎn)和關(guān)系內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Python爬蟲(chóng)爬取博客園問(wèn)題列表所有的問(wèn)題
這篇文章主要介紹了詳解Python爬蟲(chóng)爬取博客園問(wèn)題列表所有的問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
Python中defaultdict與lambda表達(dá)式用法實(shí)例小結(jié)
這篇文章主要介紹了Python中defaultdict與lambda表達(dá)式用法,結(jié)合實(shí)例形式分析了Python中defaultdict與lambda表達(dá)式的功能、使用方法及相關(guān)注意事項(xiàng),需要的朋友可以參考下2018-04-04
對(duì)Python中plt的畫(huà)圖函數(shù)詳解
今天小編就為大家分享一篇對(duì)Python中plt的畫(huà)圖函數(shù)詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-11-11
給Python中的MySQLdb模塊添加超時(shí)功能的教程
這篇文章主要介紹了給Python中的MySQLdb模塊添加超時(shí)功能的教程,timeout功能在服務(wù)器的運(yùn)維當(dāng)中非常有用,需要的朋友可以參考下2015-05-05
Python使用Rich實(shí)現(xiàn)美化終端顯示效果
Rich庫(kù)的功能就像它的名字一樣,使Python編程更加豐富(rich),用來(lái)幫助開(kāi)發(fā)者在控制臺(tái)(命令行)輸出中創(chuàng)建豐富、多彩和具有格式化的文本,下面我們就來(lái)了解下它的具體使用吧2024-02-02
pandas將list數(shù)據(jù)拆分成行或列的實(shí)現(xiàn)
這篇文章主要介紹了pandas將list數(shù)據(jù)拆分成行或列的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12

