py2neo 使用手册

根据末端节点 ID 获取图关系

from py2neo import Graph

class Neo4jConnection:
    """Neo4j 连接类"""

    def __init__(self, host, user, password, port):
        self.graph = Graph(host=host, user=user,
                           password=password, port=port)
                           
    def get_relationship_by_end_node_id(self, end_node_id):
        """根据末端节点 ID 获取图关系

        :param end_node_id: 末端节点 ID
        :return: 图关系
        """

        end_node = self.graph.nodes.get(end_node_id)
        relationship = self.graph.match_one(nodes=(None, end_node), r_type=r_type)

        return relationship

OGM(对象-图映射)

创建一个节点/关系

from py2neo import Graph
from py2neo.ogm import GraphObject, Property

class Person(GraphObject):
    """节点对象"""

    field_1 = Property()
    field_2 = Property()
    field_3 = Property()

    transfer = RelatedTo('Person', 'TRANSFER')

def create_person_node(field_1, field_2, field_3):
    """创建 person 节点

    :param field_1: 属性 1
    :param field_2: 属性 2
    :param field_3: 属性 3
    :return:
    """

    graph = Graph(
        host=host,
        user=user,
        password=password,
        port=port
    )

    new_person_node = Person()
    new_person_node.field_1 = field_1
    new_person_node.field_2 = field_2
    new_person_node.field_3 = field_3

    graph.push(new_person_node)

def create_transfer_relationship(field_1, field_2, field_3):
    """创建 transfer 关系

    :param field_1: 属性 1
    :param field_2: 属性 2
    :param field_3: 属性 3
    :return:
    """

    graph = Graph(
        host=host,
        user=user,
        password=password,
        port=port
    )

    new_person_node = Person()
    new_person_node.field_1 = field_1
    new_person_node.field_2 = field_2
    new_person_node.field_3 = field_3
	
	new_person_node_1 = Person()
    new_person_node_1.field_1 = field_1
    new_person_node_1.field_2 = field_2
    new_person_node_1.field_3 = field_3
    new_person_node_1.transfer.add(
		new_person_node,
		field_1=field_1
	)
    graph.push(new_person_node_1)

图存储

from py2neo import Graph, Node

# 用子图创建的方式进行图存储
person_1 = Node('Person', name="Kangkang")
person_2 = Node('Person', name="Jane")
friend_relationship_1 = (person_1, "Friend", person_2)
person_3 = Node('Person', name="Maria")
friend_relationship_2 = (person_1, "Friend", person_3)
sub_graph = friend_relationship_1 | friend_relationship_2

graph_instance = Graph(host=host, port=port, user=user, password=password)
graph_instance.create(sub_graph)

# 合并图数据,merge 的最后一个参数需要是 unique key
person_4 = Node('Person', name="Michael")
friend_relationship_3 = (person_3, "Friend", person_4)
graph_instance.merge(friend_relationship_3, "Person", "name")

参考

neo4j︱与python结合的py2neo使用教程(四)
Neo4j 简介及 Py2Neo 的用法
py2neo操作图数据库neo4j
py2neo操作-官方样例

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值