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): ? ? """自動創(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() ? ? # 自動創(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é)點(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() ? ? # 自動創(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python實(shí)現(xiàn)圖片彩色轉(zhuǎn)化為素描
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)圖片彩色轉(zhuǎn)化為素描,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01使用python 3實(shí)現(xiàn)發(fā)送郵件功能
本文通過實(shí)例代碼給大家介紹了使用python 3實(shí)現(xiàn)發(fā)送郵件功能,代碼簡單易懂非常不錯,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-06-06python2 與python3的print區(qū)別小結(jié)
這篇文章主要介紹了python2 與python3的print區(qū)別小結(jié),需要的朋友可以參考下2018-01-01Python讀取mat(matlab數(shù)據(jù)文件)并實(shí)現(xiàn)畫圖
這篇文章主要介紹了Python讀取mat(matlab數(shù)據(jù)文件)并實(shí)現(xiàn)畫圖問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12numpy存取數(shù)據(jù)(tofile/fromfile)的實(shí)現(xiàn)
本文主要介紹了numpy存取數(shù)據(jù)(tofile/fromfile)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02