python使用py2neo根据关系自动创建neo4j的节点与关系

2022/2/2 1:29:26

本文主要是介绍python使用py2neo根据关系自动创建neo4j的节点与关系,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

文章目录

  • 核心代码
  • 完整示例代码

使用py2neo构建neo4j图模型小demo:https://blog.csdn.net/weixin_35757704/article/details/112525629

核心代码

  1. 使用py2neo连接neo4j的方法:
from py2neo import Graph

graph = Graph("http://localhost:7474", auth=("neo4j", "neo4j"))
graph.delete_all()  # 删除已有的所有内容
  1. 根据dict创建Node
from py2neo import Node

node = Node(**{"key":"value"})
graph.create(node)
  1. 创建关系:
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)

完整示例代码

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的节点与关系的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程