|
|
|
@ -1,5 +1,7 @@
|
|
|
|
|
# pip install py2neo
|
|
|
|
|
from py2neo import Graph, Node, Relationship
|
|
|
|
|
# 参考文档
|
|
|
|
|
# https://cloud.tencent.com/developer/article/1693445
|
|
|
|
|
|
|
|
|
|
# 修改密码
|
|
|
|
|
# - 使用Neo4j Browser或命令行连接到系统数据库
|
|
|
|
@ -9,7 +11,7 @@ from py2neo import Graph, Node, Relationship
|
|
|
|
|
# 连接配置
|
|
|
|
|
graph = Graph(
|
|
|
|
|
"bolt://localhost:7687", # 建议使用bolt协议
|
|
|
|
|
auth=("neo4j", "DsideaL147258369") # 用户名和密码
|
|
|
|
|
auth=("neo4j", "DsideaL147258369") # 用户名和密码
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# 测试连接
|
|
|
|
@ -17,11 +19,117 @@ try:
|
|
|
|
|
# 执行一个简单查询测试连接
|
|
|
|
|
result = graph.run("RETURN 1").data()
|
|
|
|
|
print("成功连接到Neo4j数据库,测试查询结果:", result)
|
|
|
|
|
|
|
|
|
|
# 示例:创建一个节点
|
|
|
|
|
node = Node("Person", name="Alice")
|
|
|
|
|
graph.create(node)
|
|
|
|
|
print("成功创建节点:", node)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 清空数据库
|
|
|
|
|
graph.delete_all()
|
|
|
|
|
print("数据库已清空")
|
|
|
|
|
|
|
|
|
|
node_1 = Node("person", name="黄刚")
|
|
|
|
|
node_2 = Node("person", name="刘梅")
|
|
|
|
|
node_3 = Node("person", name="赵琬")
|
|
|
|
|
node_4 = Node("person", name="夏雪")
|
|
|
|
|
node_5 = Node("person", name="夏雨")
|
|
|
|
|
node_6 = Node("person", name="胡统一")
|
|
|
|
|
node_7 = Node("person", name="玛丽")
|
|
|
|
|
node_8 = Node("person", name="戴明明")
|
|
|
|
|
node_9 = Node("person", name="戴天高")
|
|
|
|
|
node_10 = Node("person", name="胖婶")
|
|
|
|
|
node_11 = Node("person", name="夏祥")
|
|
|
|
|
graph.create(node_1)
|
|
|
|
|
graph.create(node_2)
|
|
|
|
|
graph.create(node_3)
|
|
|
|
|
graph.create(node_4)
|
|
|
|
|
graph.create(node_5)
|
|
|
|
|
graph.create(node_6)
|
|
|
|
|
graph.create(node_7)
|
|
|
|
|
graph.create(node_8)
|
|
|
|
|
graph.create(node_9)
|
|
|
|
|
graph.create(node_10)
|
|
|
|
|
graph.create(node_11)
|
|
|
|
|
|
|
|
|
|
node_12 = Node("job", name="护士长")
|
|
|
|
|
node_13 = Node("job", name="学生")
|
|
|
|
|
node_14 = Node("job", name="编导")
|
|
|
|
|
node_15 = Node("job", name="无业游民")
|
|
|
|
|
node_16 = Node("job", name="社区工作人员")
|
|
|
|
|
node_17 = Node("job", name="无业游民")
|
|
|
|
|
node_18 = Node("location", country="中国", city='北京')
|
|
|
|
|
node_19 = Node("location", country="美国", city='纽约')
|
|
|
|
|
graph.create(node_12)
|
|
|
|
|
graph.create(node_13)
|
|
|
|
|
graph.create(node_14)
|
|
|
|
|
graph.create(node_15)
|
|
|
|
|
graph.create(node_16)
|
|
|
|
|
graph.create(node_17)
|
|
|
|
|
graph.create(node_18)
|
|
|
|
|
graph.create(node_19)
|
|
|
|
|
|
|
|
|
|
#node_1_call_node_2 = Relationship(node_1, '丈夫', node_2)
|
|
|
|
|
#graph.create(node_1_call_node_2)
|
|
|
|
|
|
|
|
|
|
r1 = Relationship(Node("person", name="刘梅"), '妈妈', Node("person", name="赵琬"))
|
|
|
|
|
graph.create(r1)
|
|
|
|
|
|
|
|
|
|
node_1 = Node("person", name="黄刚")
|
|
|
|
|
node_2 = Node("person", name="刘梅")
|
|
|
|
|
r1 = Relationship(node_1, '丈夫', node_2)
|
|
|
|
|
r2 = Relationship(node_2, '妻子', node_1)
|
|
|
|
|
graph.create(r1)
|
|
|
|
|
graph.create(r2)
|
|
|
|
|
|
|
|
|
|
r3 = graph.create(Relationship(node_1, '继父', node_3))
|
|
|
|
|
r4 = graph.create(Relationship(node_3, '继子', node_1))
|
|
|
|
|
r5 = graph.create(Relationship(node_1, '父亲', node_4))
|
|
|
|
|
r6 = graph.create(Relationship(node_4, '女儿', node_1))
|
|
|
|
|
r7 = graph.create(Relationship(node_1, '父亲', node_5))
|
|
|
|
|
r8 = graph.create(Relationship(node_5, '儿子', node_1))
|
|
|
|
|
r9 = graph.create(Relationship(node_1, '前夫', node_7))
|
|
|
|
|
r10 = graph.create(Relationship(node_7, '前妻', node_1))
|
|
|
|
|
r11 = graph.create(Relationship(node_1, '儿子', node_11))
|
|
|
|
|
r12 = graph.create(Relationship(node_11, '父亲', node_1))
|
|
|
|
|
r13 = graph.create(Relationship(node_2, '母亲', node_3))
|
|
|
|
|
r14 = graph.create(Relationship(node_3, '儿子', node_2))
|
|
|
|
|
r15 = graph.create(Relationship(node_2, '继母', node_4))
|
|
|
|
|
r16 = graph.create(Relationship(node_4, '继女', node_2))
|
|
|
|
|
r17 = graph.create(Relationship(node_2, '继母', node_5))
|
|
|
|
|
r18 = graph.create(Relationship(node_5, '继子', node_2))
|
|
|
|
|
r19 = graph.create(Relationship(node_2, '前妻', node_6))
|
|
|
|
|
r20 = graph.create(Relationship(node_6, '前夫', node_2))
|
|
|
|
|
r21 = graph.create(Relationship(node_2, '同学', node_9))
|
|
|
|
|
r22 = graph.create(Relationship(node_9, '同学', node_2))
|
|
|
|
|
r23 = graph.create(Relationship(node_2, '邻居', node_10))
|
|
|
|
|
r24 = graph.create(Relationship(node_10, '邻居', node_2))
|
|
|
|
|
r25 = graph.create(Relationship(node_3, '弟弟', node_4))
|
|
|
|
|
r26 = graph.create(Relationship(node_4, '姐姐', node_3))
|
|
|
|
|
r27 = graph.create(Relationship(node_3, '哥哥', node_5))
|
|
|
|
|
r28 = graph.create(Relationship(node_5, '弟弟', node_3))
|
|
|
|
|
r29 = graph.create(Relationship(node_3, '儿子', node_6))
|
|
|
|
|
r30 = graph.create(Relationship(node_6, '父亲', node_3))
|
|
|
|
|
r31 = graph.create(Relationship(node_4, '姐姐', node_5))
|
|
|
|
|
r32 = graph.create(Relationship(node_5, '弟弟', node_4))
|
|
|
|
|
r33 = graph.create(Relationship(node_4, '女儿', node_7))
|
|
|
|
|
r34 = graph.create(Relationship(node_7, '母亲', node_4))
|
|
|
|
|
r35 = graph.create(Relationship(node_4, '朋友', node_8))
|
|
|
|
|
r36 = graph.create(Relationship(node_8, '朋友', node_4))
|
|
|
|
|
r37 = graph.create(Relationship(node_5, '儿子', node_7))
|
|
|
|
|
r38 = graph.create(Relationship(node_7, '母亲', node_5))
|
|
|
|
|
r39 = graph.create(Relationship(node_8, '女儿', node_9))
|
|
|
|
|
r40 = graph.create(Relationship(node_9, '父亲', node_8))
|
|
|
|
|
|
|
|
|
|
r41 = graph.create(Relationship(node_1, '居住地', node_18))
|
|
|
|
|
r42 = graph.create(Relationship(node_2, '居住地', node_18))
|
|
|
|
|
r43 = graph.create(Relationship(node_3, '居住地', node_18))
|
|
|
|
|
r44 = graph.create(Relationship(node_4, '居住地', node_18))
|
|
|
|
|
r45 = graph.create(Relationship(node_5, '居住地', node_18))
|
|
|
|
|
r46 = graph.create(Relationship(node_7, '居住地', node_19))
|
|
|
|
|
|
|
|
|
|
r47 = graph.create(Relationship(node_1, '职业', node_14))
|
|
|
|
|
r48 = graph.create(Relationship(node_2, '职业', node_12))
|
|
|
|
|
r49 = graph.create(Relationship(node_3, '职业', node_13))
|
|
|
|
|
r50 = graph.create(Relationship(node_4, '职业', node_13))
|
|
|
|
|
r51 = graph.create(Relationship(node_5, '职业', node_13))
|
|
|
|
|
r52 = graph.create(Relationship(node_8, '职业', node_13))
|
|
|
|
|
r53 = graph.create(Relationship(node_6, '职业', node_15))
|
|
|
|
|
r54 = graph.create(Relationship(node_10, '职业', node_16))
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print("连接失败:", e)
|
|
|
|
|
print("连接失败:", e)
|
|
|
|
|