python使用py2neo創(chuàng)建neo4j的節(jié)點和關系
更新時間:2022年02月11日 12:17:00 作者:呆萌的代Ma
這篇文章主要介紹了python使用py2neo創(chuàng)建neo4j的節(jié)點和關系,第一步使用py2neo連接neo4j的方法然后根據dict創(chuàng)建Node,更多相關資料需要的朋友參考下面文章內容
1.核心代碼
使用py2neo連接neo4j的方法:
from py2neo import Graph graph = Graph("http://localhost:7474", auth=("neo4j", "neo4j")) graph.delete_all() ?# 刪除已有的所有內容
根據dict創(chuàng)建Node:
from py2neo import Node node = Node(**{"key":"value"}) graph.create(node)
創(chuàng)建關系:
from py2neo import Relationship relation = Relationship(node1, relation, node2) graph.create(relation)
用到的工具函數是:
def create_relation(graph, match_node1: dict, match_node2: dict, relation: str, node1_label=None, node2_label=None): ? ? """自動創(chuàng)建節(jié)點與關系 ? ? :param graph: 圖 ? ? :param match_node1: 節(jié)點1屬性 ? ? :param match_node2: 節(jié)點2屬性 ? ? :param relation: 關系 ? ? :param node1_label: 節(jié)點1的標簽 ? ? :param node2_label: 節(jié)點2的標簽 ? ? """ ? ? 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)建關系 ? ? 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é)點與關系 ? ? :param graph: 圖 ? ? :param match_node1: 節(jié)點1屬性 ? ? :param match_node2: 節(jié)點2屬性 ? ? :param relation: 關系 ? ? :param node1_label: 節(jié)點1的標簽 ? ? :param node2_label: 節(jié)點2的標簽 ? ? """ ? ? 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)建關系 ? ? 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() ?# 刪除已有的所有內容 ? ? 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()
效果圖:
到此這篇關于python使用py2neo創(chuàng)建neo4j的節(jié)點和關系的文章就介紹到這了,更多相關python使用py2neo創(chuàng)建neo4j的節(jié)點和關系內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
python2 與python3的print區(qū)別小結
這篇文章主要介紹了python2 與python3的print區(qū)別小結,需要的朋友可以參考下2018-01-01