網站首頁 編程語言 正文
1.核心代碼
使用py2neo連接neo4j的方法:
from py2neo import Graph graph = Graph("http://localhost:7474", auth=("neo4j", "neo4j")) graph.delete_all() ?# 刪除已有的所有內容
根據dict創建Node:
from py2neo import Node node = Node(**{"key":"value"}) graph.create(node)
創建關系:
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): ? ? """自動創建節點與關系 ? ? :param graph: 圖 ? ? :param match_node1: 節點1屬性 ? ? :param match_node2: 節點2屬性 ? ? :param relation: 關系 ? ? :param node1_label: 節點1的標簽 ? ? :param node2_label: 節點2的標簽 ? ? """ ? ? from py2neo import Node, Relationship ? ? from py2neo import NodeMatcher ? ? node_matcher = NodeMatcher(graph) ? ? node1 = node_matcher.match(**match_node1).first() ? ? # 自動創建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) ? ? # 創建關系 ? ? 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): ? ? """自動創建節點與關系 ? ? :param graph: 圖 ? ? :param match_node1: 節點1屬性 ? ? :param match_node2: 節點2屬性 ? ? :param relation: 關系 ? ? :param node1_label: 節點1的標簽 ? ? :param node2_label: 節點2的標簽 ? ? """ ? ? from py2neo import Node, Relationship ? ? from py2neo import NodeMatcher ? ? node_matcher = NodeMatcher(graph) ? ? node1 = node_matcher.match(**match_node1).first() ? ? # 自動創建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) ? ? # 創建關系 ? ? 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()
效果圖:
原文鏈接:https://blog.csdn.net/weixin_35757704/article/details/122766877
相關推薦
- 2023-05-14 Python中數字(Number)數據類型常用操作_python
- 2022-12-06 python中的代碼運行時間獲取方式_python
- 2022-02-02 Maven命令安裝本地jar包到本地倉庫
- 2023-11-23 python獲取文件夾內所有文件并改名
- 2024-02-28 UNI-APP中,swiper和tabbar結合實現滑動翻頁效果
- 2022-11-23 Android開發Jetpack?Compose元素Modifier特性詳解_Android
- 2022-10-08 淺談數據庫日期類型字段設計應該如何選擇_MsSql
- 2022-02-11 安裝element UI (全局引入與按需引入)
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支