neo4j实例教程(python版)

本文是一篇关于使用Python与Neo4j数据库交互的教程,介绍了如何创建节点、建立关系并更新它们的属性。还讲解了如何通过find和find_one函数查找节点,以及使用match和match_one获取相关节点和关系。
# coding: utf-8 -*-
from py2neo import Graph,Node,Relationship
graph = Graph("http://localhost:7474", username="neo4j", password="neo4j")
test_node_1 = Node(label = "Person",name = "test_node_1")
test_node_2 = Node(label = "Person",name = "test_node_2")

graph.create(test_node_1)
graph.create(test_node_2)

"""分别建立了test_node_1指向test_node_2test_node_2指向test_node_1两条关系,
关系的类型为"CALL",两条关系都有属性count,且值为1"""
node_1_call_node_2 = Relationship(test_node_1,'CALL',test_node_2)
node_1_call_node_2['count'] = 1
node_2_call_node_1 = Relationship(test_node_2,'CALL',test_node_1)
node_2_call_node_1['count'] = 1
graph.create(node_1_call_node_2)
graph.create(node_2_call_node_1)


"""节点和关系的属性初始赋值在前面节点和关系的建立
的时候已经有了相应的代码,在这里主要讲述一下怎么更新一个节点/关系的属性值。"""

node_1_call_node_2['count']+=1
graph.push(node_1_call_node_2)

"""通过findfind_one函数,可以根据类型和属性、属性值来查找节点和关系。"""

"""findfind_one的区别在于:
find_one的返回结果是一个具体的节点/关系,可以直接查看它的属性和值。如果没有这个节点/关系,返回None
find查找的结果是一个游标,可以通过循环取到所找到的所有节点/关系。"""

find_code_1 = graph.find_one(
  label="Person",
  property_key="name",
  # property_value="test_node_1"
)
# print(find_code_1['name'])

find_code_3 = graph.find_one(
  label="Person",
  property_key="name",
  # property_value="test_node_2"
)

"""如果已经确定了一个节点或者关系,想找到和它相关的关系和节点,
就可以使用matchmatch_one"""
#
find_relationship = graph.match_one(start_node=find_code_1,end_node=find_code_3,bidirectional=False)
print(find_relationship)


# match_relation = graph.match(start_node=find_code_1,bidirectional=False) #True
# for i in match_relation:
#     print(i)
#     i['count']+=1
#     graph.push(i)


# print("1111111111111111")
# # print(graph)
# print(test_node_1)
# print(test_node_2)
# print(node_2_call_node_1)
# print(node_1_call_node_2)
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值