python使用py2neo创建neo4j的节点和关系

作者:呆萌的代Ma 时间:2021-09-25 01:03:28 

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()

效果图:

python使用py2neo创建neo4j的节点和关系

来源:https://blog.csdn.net/weixin_35757704/article/details/122766877

标签:python,py2neo,neo4j,节点,关系
0
投稿

猜你喜欢

  • 简单实现SQLServer转MYSQL的方法

    2024-01-21 20:12:13
  • python 操作sqlite数据库的方法

    2024-01-26 04:18:43
  • Python Matplotlib绘制动画的代码详解

    2022-08-19 20:21:03
  • PHP中文处理 中文字符串截取(mb_substr)和获取中文字符串字数

    2023-07-11 03:55:49
  • 仿google的asp分页代码

    2009-03-08 18:27:00
  • Python使用sqlite3第三方库读写SQLite数据库的方法步骤

    2024-01-23 06:31:39
  • PyTorch中apex安装方式和避免踩坑

    2023-06-20 15:19:57
  • 关于vuex状态刷新网页时数据被清空问题及解决

    2024-04-30 10:22:18
  • python 一维二维插值实例

    2022-07-17 10:09:08
  • python使用openpyxl库读写Excel表格的方法(增删改查操作)

    2021-11-29 01:22:43
  • 总结归纳python os库常用方法

    2023-05-23 19:34:05
  • Windows下安装MySQL5.5.19图文教程

    2024-01-20 09:00:26
  • canvas时钟效果

    2024-05-05 09:14:22
  • 详解用python实现基本的学生管理系统(文件存储版)(python3)

    2021-10-08 18:26:29
  • python实现飞船大战

    2022-09-19 02:17:26
  • Java通过MySQL的加解密函数实现敏感字段存储

    2024-01-27 12:08:25
  • Ethnique公司logo设计过程和思路

    2009-09-19 17:04:00
  • TensorFlow和Numpy矩阵操作中axis理解及axis=-1的解释

    2021-02-14 05:59:09
  • Javascript浅拷贝与深拷贝实现

    2013-07-16 22:47:46
  • Golang检查变量类型的四种方式

    2024-04-27 15:26:11
  • asp之家 网络编程 m.aspxhome.com