欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2022年02月11日 12:17:00   作者:呆萌的代Ma  
這篇文章主要介紹了python使用py2neo創(chuàng)建neo4j的節(jié)點(diǎn)和關(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é)點(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)文章

  • 教你如何在Pytorch中使用TensorBoard

    教你如何在Pytorch中使用TensorBoard

    TensorBoard是TensorFlow中強(qiáng)大的可視化工具,今天通過本文給大家介紹如何在Pytorch中使用TensorBoard,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友一起看看吧
    2021-08-08
  • 詳解Python3中的Sequence type的使用

    詳解Python3中的Sequence type的使用

    這篇文章主要介紹了詳解Python3中的Sequence type的使用,是Python入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-08-08
  • python實(shí)現(xiàn)圖片彩色轉(zhuǎn)化為素描

    python實(shí)現(xiàn)圖片彩色轉(zhuǎn)化為素描

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)圖片彩色轉(zhuǎn)化為素描,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • Python?list?append方法之給列表追加元素

    Python?list?append方法之給列表追加元素

    這篇文章主要介紹了Python?list?append方法如何給列表追加元素,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • Python解壓可迭代對象賦值給多個變量詳解

    Python解壓可迭代對象賦值給多個變量詳解

    這篇文章主要為大家介紹了Python賦值多個變量,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-12-12
  • 將python打包后的exe還原成py

    將python打包后的exe還原成py

    這篇文章主要介紹了將python打包后的exe還原成py,利用pyinstxtractor.py?拆包(解壓)工具,將exe文件解壓成一個文件夾<BR>uncompyle6?pyc反編譯工具,需要的朋友可以參考一下
    2022-01-01
  • 使用python 3實(shí)現(xiàn)發(fā)送郵件功能

    使用python 3實(shí)現(xiàn)發(fā)送郵件功能

    本文通過實(shí)例代碼給大家介紹了使用python 3實(shí)現(xiàn)發(fā)送郵件功能,代碼簡單易懂非常不錯,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-06-06
  • python2 與python3的print區(qū)別小結(jié)

    python2 與python3的print區(qū)別小結(jié)

    這篇文章主要介紹了python2 與python3的print區(qū)別小結(jié),需要的朋友可以參考下
    2018-01-01
  • Python讀取mat(matlab數(shù)據(jù)文件)并實(shí)現(xiàn)畫圖

    Python讀取mat(matlab數(shù)據(jù)文件)并實(shí)現(xiàn)畫圖

    這篇文章主要介紹了Python讀取mat(matlab數(shù)據(jù)文件)并實(shí)現(xiàn)畫圖問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • numpy存取數(shù)據(jù)(tofile/fromfile)的實(shí)現(xiàn)

    numpy存取數(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

最新評論