python使用py2neo創(chuàng)建neo4j的節(jié)點和關(guān)系
更新時間:2022年02月11日 12:17:00 作者:呆萌的代Ma
這篇文章主要介紹了python使用py2neo創(chuàng)建neo4j的節(jié)點和關(guān)系,第一步使用py2neo連接neo4j的方法然后根據(jù)dict創(chuàng)建Node,更多相關(guān)資料需要的朋友參考下面文章內(nèi)容
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): ? ? """自動創(chuàng)建節(jié)點與關(guān)系 ? ? :param graph: 圖 ? ? :param match_node1: 節(jié)點1屬性 ? ? :param match_node2: 節(jié)點2屬性 ? ? :param relation: 關(guān)系 ? ? :param node1_label: 節(jié)點1的標(biāo)簽 ? ? :param node2_label: 節(jié)點2的標(biāo)簽 ? ? """ ? ? from py2neo import Node, Relationship ? ? from py2neo import NodeMatcher ? ? node_matcher = NodeMatcher(graph) ? ? node1 = node_matcher.match(**match_node1).first() ? ? # 自動創(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):
? ? """自動創(chuàng)建節(jié)點與關(guān)系
? ? :param graph: 圖
? ? :param match_node1: 節(jié)點1屬性
? ? :param match_node2: 節(jié)點2屬性
? ? :param relation: 關(guān)系
? ? :param node1_label: 節(jié)點1的標(biāo)簽
? ? :param node2_label: 節(jié)點2的標(biāo)簽
? ? """
? ? from py2neo import Node, Relationship
? ? from py2neo import NodeMatcher
? ? node_matcher = NodeMatcher(graph)
? ? node1 = node_matcher.match(**match_node1).first()
? ? # 自動創(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é)點和關(guān)系的文章就介紹到這了,更多相關(guān)python使用py2neo創(chuàng)建neo4j的節(jié)點和關(guān)系內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python中defaultdict與lambda表達式用法實例小結(jié)
這篇文章主要介紹了Python中defaultdict與lambda表達式用法,結(jié)合實例形式分析了Python中defaultdict與lambda表達式的功能、使用方法及相關(guān)注意事項,需要的朋友可以參考下2018-04-04
pandas將list數(shù)據(jù)拆分成行或列的實現(xiàn)
這篇文章主要介紹了pandas將list數(shù)據(jù)拆分成行或列的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12

