Neo图数据库与python交互
程序员文章站
2022-03-03 10:20:17
...
# -*- coding: UTF-8 -*-
__author__ = 'zy'
__time__ = '2020/12/29 11:18'
# coding:utf-8
from py2neo import Graph, Node, Relationship
##连接neo4j数据库,输入地址、用户名、密码
graph = Graph('http://39.97.100.242:7474', username='neo4j', password='neo4j/mysql')
##创建结点
test_node_1 = Node(label='ru_yi_zhuan', name='皇帝')
test_node_2 = Node(label='ru_yi_zhuan', name='皇后')
test_node_3 = Node(label='ru_yi_zhuan', name='公主')
graph.create(test_node_1)
graph.create(test_node_2)
graph.create(test_node_3)
##创建关系
# 分别建立了test_node_1指向test_node_2和test_node_2指向test_node_1两条关系,关系的类型为"丈夫、妻子",两条关系都有属性count,且值为1。
node_1_zhangfu_node_1 = Relationship(test_node_1, '丈夫', test_node_2)
node_1_zhangfu_node_1['count'] = 3
node_2_qizi_node_1 = Relationship(test_node_2, '妻子', test_node_1)
node_2_munv_node_1 = Relationship(test_node_2, '母女', test_node_3)
node_2_qizi_node_1['count'] = 1
graph.create(node_1_zhangfu_node_1)
graph.create(node_2_qizi_node_1)
graph.create(node_2_munv_node_1)
print(graph)
print(test_node_1)
print(test_node_2)
print(node_1_zhangfu_node_1)
print(node_2_qizi_node_1)
print(node_2_munv_node_1)
#CREATE (emp:Employee{id:123,name:"Lokesh",sal:35000,deptno:10})
# CREATE (
# <node-name>:<label-name>
# {
# <Property1-name>:<Property1-Value>
# ........
# <Propertyn-name>:<Propertyn-Value>
# }
# )
# MATCH (<node-name>:<label-name>)
# MATCH (p:Employee {name:"Lokesh"}) RETURN p
# MATCH (e:Customer)
# RETURN e.id,e.name,e.dob
# MATCH (name:Dept)
# RETURN name.deptno
# CREATE (<node1-name>:<label1-name>)-
# [(<relationship-name>:<relationship-label-name>)]
# ->(<node2-name>:<label2-name>)
# CREATE (dept:Dept { deptno:10,dname:"Accounting",location:"Hyderabad" })
# CREATE (dept:Dept)-[r1:LIKES]->(dept2:Dept)
#CREATE (dept:Dept { deptno:10,dname:"Accounting",location:"Hyderabad" })
# MATCH (emp:Employee)
# WHERE emp.name = 'Abc'
# RETURN emp
# MATCH (e: Employee) DELETE e
# DELETE <node1-name>,<node2-name>,<relationship-name>
# MATCH (cc: CreditCard)-[rel]-(c:Customer)
# DELETE cc,c,rel
# MATCH (e: Employee) DELETE e
#全部删除
#WHERE emp.name = 'Abc'
#部分删除
# MATCH (e: Employee) RETURN e
#
# MATCH (e:wuhan)
# WHERE e.happy ='4'
# delete e
上一篇: 根据电话号码获取归属地
下一篇: 获取时间 判断有效期的工具类