最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python使用py2neo創(chuàng)建neo4j的節(jié)點和關系

 更新時間:2022年02月11日 12:17:00   作者:呆萌的代Ma  
這篇文章主要介紹了python使用py2neo創(chuàng)建neo4j的節(jié)點和關系,第一步使用py2neo連接neo4j的方法然后根據(jù)dict創(chuàng)建Node,更多相關資料需要的朋友參考下面文章內(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)建關系:

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é)點與關系
? ? :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() ?# 刪除已有的所有內(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()

效果圖:

到此這篇關于python使用py2neo創(chuàng)建neo4j的節(jié)點和關系的文章就介紹到這了,更多相關python使用py2neo創(chuàng)建neo4j的節(jié)點和關系內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 詳解Python爬蟲爬取博客園問題列表所有的問題

    詳解Python爬蟲爬取博客園問題列表所有的問題

    這篇文章主要介紹了詳解Python爬蟲爬取博客園問題列表所有的問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-01-01
  • Python中defaultdict與lambda表達式用法實例小結(jié)

    Python中defaultdict與lambda表達式用法實例小結(jié)

    這篇文章主要介紹了Python中defaultdict與lambda表達式用法,結(jié)合實例形式分析了Python中defaultdict與lambda表達式的功能、使用方法及相關注意事項,需要的朋友可以參考下
    2018-04-04
  • 對Python中plt的畫圖函數(shù)詳解

    對Python中plt的畫圖函數(shù)詳解

    今天小編就為大家分享一篇對Python中plt的畫圖函數(shù)詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-11-11
  • 給Python中的MySQLdb模塊添加超時功能的教程

    給Python中的MySQLdb模塊添加超時功能的教程

    這篇文章主要介紹了給Python中的MySQLdb模塊添加超時功能的教程,timeout功能在服務器的運維當中非常有用,需要的朋友可以參考下
    2015-05-05
  • Python中打印出指定格式日期時間的12種方式

    Python中打印出指定格式日期時間的12種方式

    無論是為了數(shù)據(jù)分析、文件命名還是日志記錄,掌握多種日期格式化的方法都是很有幫助的,下面我們來探索一下如何以各種不同的方式使用Python打印出指定格式的日期吧
    2025-02-02
  • 利用python添加表格到PowerPoint中的代碼示例

    利用python添加表格到PowerPoint中的代碼示例

    有效的信息傳達是演示文稿中的重點,而PowerPoint演示文稿作為最廣泛使用的演示工具之一,提供了豐富的功能來幫助演講者實現(xiàn)這一目標,本文將介紹如何利用Python來添加表格到PowerPoint演示文稿中,需要的朋友可以參考下
    2024-08-08
  • PyTorch: 梯度下降及反向傳播的實例詳解

    PyTorch: 梯度下降及反向傳播的實例詳解

    今天小編就為大家分享一篇PyTorch: 梯度下降及反向傳播的實例詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-08-08
  • Python使用Rich實現(xiàn)美化終端顯示效果

    Python使用Rich實現(xiàn)美化終端顯示效果

    Rich庫的功能就像它的名字一樣,使Python編程更加豐富(rich),用來幫助開發(fā)者在控制臺(命令行)輸出中創(chuàng)建豐富、多彩和具有格式化的文本,下面我們就來了解下它的具體使用吧
    2024-02-02
  • pandas將list數(shù)據(jù)拆分成行或列的實現(xiàn)

    pandas將list數(shù)據(jù)拆分成行或列的實現(xiàn)

    這篇文章主要介紹了pandas將list數(shù)據(jù)拆分成行或列的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-12-12
  • Django model update的多種用法介紹

    Django model update的多種用法介紹

    Django開發(fā)過程中對表(model)的增刪改查是最常用的功能之一,本文介紹筆者在使用model update過程中遇到的那些事
    2019-07-07

最新評論

巴里| 乌拉特中旗| 奇台县| 马边| 方正县| 阳西县| 马龙县| 东乌| 呼玛县| 遂昌县| 郸城县| 汉寿县| 普陀区| 黔南| 成安县| 通州区| 麻栗坡县| 泽库县| 渭南市| 明光市| 大兴区| 江永县| 唐山市| 广安市| 阿尔山市| 射洪县| 鄂尔多斯市| 枞阳县| 拉孜县| 四平市| 济源市| 定结县| 辽宁省| 崇阳县| 拜城县| 古交市| 江西省| 东莞市| 会泽县| 黄陵县| 两当县|