neo4j之py2neo模块的使用
py2neo模块
一、连接neo4j图数据空
from py2neo import Graph
graph = Graph("http://192.168.101.52:9696", auth=("neo4j", "neo4j")) # auth元组中为账号和密码
二、创建节点
from py2neo import Graph, Node
def test():
graph = Graph("http://192.168.101.52:9696", auth=("neo4j", "neo4j1"))
node = Node("Person", name="小明", age=18, school="清华大学")
graph.create(node)
if __name__ == '__main__':
test()
结果:
三、查询节点
1、查询一个节点
from py2neo import Graph, Node, NodeMatcher
def test():
graph = Graph("http://192.168.101.52:9696", auth=("neo4j", "neo4j1"))
node_matcher = NodeMatcher(graph)
res = node_matcher.match("Person").where(name="小红").first()
print("1:", res)
print("2:", type(res))
print("3:", dict(res))
if __name__ == '__main__':
test()
结果:
2、查询多个节点
from py2neo import Graph, NodeMatcher
def test():
graph = Graph("http://192.168.101.52:9696", auth=("neo4j", "neo4j1"))
node_matcher = NodeMatcher(graph)
res = node_matcher.match("Person").where(age=18).all()
print("1:", res)
print("2:", type(res))
print("")
for i in res:
print("1:", i)
print("2:", type(i))
print("3:", dict(i))
print("")
if __name__ == '__main__':
test()
结果:
3、查询所有节点
from py2neo import Graph, Node, NodeMatcher, Relationship, RelationshipMatcher
def test():
graph = Graph("http://192.168.101.52:9696", auth=("neo4j", "neo4j1"))
node_matcher = NodeMatcher(graph)
nodes = node_matcher.match().all()
for i in nodes:
if i["age"] == 18:
print(dict(i))
if __name__ == '__main__':
test()
结果:
4、查询关系
from py2neo import Graph, NodeMatcher, RelationshipMatcher
def test():
graph = Graph("http://192.168.101.52:9696", auth=("neo4j", "neo4j1"))
node_matcher = NodeMatcher(graph)
relationship_matcher = RelationshipMatcher(graph)
node1 = node_matcher.match("Person").where(name="小明").first()
node2 = node_matcher.match("Person").where(name="小红").first()
res = relationship_matcher.match((node1, node2), r_type="KNOWS").all()
print(res)
print(dict(res[0]))
if __name__ == '__main__':
test()
结果:
四、创建关系
from py2neo import Graph, NodeMatcher, Relationship
def test():
graph = Graph("http://192.168.101.52:9696", auth=("neo4j", "neo4j1"))
node_matcher = NodeMatcher(graph)
node1 = node_matcher.match("Person").where(name="小明").first()
node2 = node_matcher.match("Person").where(name="小红").first()
graph.create(Relationship(node1, "KNOWS", node2, since="2000", others="备注:无"))
pro = {"since": "2000", "others": "test"}
graph.create(Relationship(node2, "KNOWS", node1, **pro))
if __name__ == '__main__':
test()
结果:
五、修改
1、修改(或新增)节点属性
from py2neo import Graph, NodeMatcher
def test():
graph = Graph("http://192.168.101.52:9696", auth=("neo4j", "neo4j1"))
node_matcher = NodeMatcher(graph)
node1 = node_matcher.match("Person").where(name="小明").first()
node1["age"] = 28
graph.push(node1)
if __name__ == '__main__':
test()
结果:
2、修改(或新增)关系属性
from py2neo import Graph, NodeMatcher, RelationshipMatcher
def test():
graph = Graph("http://192.168.101.52:9696", auth=("neo4j", "neo4j1"))
node_matcher = NodeMatcher(graph)
relation_matcher = RelationshipMatcher(graph)
node1 = node_matcher.match("Person").where(name="小明").first()
node2 = node_matcher.match("Person").where(name="小红").first()
relation = relation_matcher.match((node1, node2), r_type="KNOWS").first()
relation["since"] = "2001"
graph.push(relation)
if __name__ == '__main__':
test()
结果:
3、删除节点属性
from py2neo import Graph, NodeMatcher
def test():
graph = Graph("http://192.168.101.52:9696", auth=("neo4j", "neo4j1"))
node_matcher = NodeMatcher(graph)
node1 = node_matcher.match("Person").where(name="小明").first()
del node1["age"]
graph.push(node1)
if __name__ == '__main__':
test()
结果:
4、删除关系属性
from py2neo import Graph, NodeMatcher, RelationshipMatcher
def test():
graph = Graph("http://192.168.101.52:9696", auth=("neo4j", "neo4j1"))
node_matcher = NodeMatcher(graph)
relation_matcher = RelationshipMatcher(graph)
node1 = node_matcher.match("Person").where(name="小明").first()
node2 = node_matcher.match("Person").where(name="小红").first()
relation = relation_matcher.match((node1, node2), r_type="KNOWS").first()
del relation["since"]
graph.push(relation)
if __name__ == '__main__':
test()
结果:
5、节点新增标签
from py2neo import Graph, NodeMatcher
def test():
graph = Graph("http://192.168.101.52:9696", auth=("neo4j", "neo4j1"))
node_matcher = NodeMatcher(graph)
node1 = node_matcher.match("Person").where(name="小明").first()
node1.add_label("Student")
graph.push(node1)
if __name__ == '__main__':
test()
结果:
6、节点删除标签
from py2neo import Graph, NodeMatcher
def test():
graph = Graph("http://192.168.101.52:9696", auth=("neo4j", "neo4j1"))
node_matcher = NodeMatcher(graph)
node1 = node_matcher.match("Person").where(name="小明").first()
node1.remove_label("Student")
graph.push(node1)
if __name__ == '__main__':
test()
结果:
7、批量增加标签
node1.update_labels(["Student", "Male"])
graph.push(node1)
结果:
8、删除关系
from py2neo import Graph, NodeMatcher, RelationshipMatcher
def test():
graph = Graph("http://192.168.101.52:9696", auth=("neo4j", "neo4j1"))
node_matcher = NodeMatcher(graph)
relation_matcher = RelationshipMatcher(graph)
node1 = node_matcher.match("Person").where(name="小明").first()
node2 = node_matcher.match("Person").where(name="小红").first()
relations = relation_matcher.match((node1, node2), r_type="KNOWS").all()
for i in relations:
graph.separate(i)
if __name__ == '__main__':
test()
结果:
9、同时删除节点和节点间的关系
from py2neo import Graph, NodeMatcher, RelationshipMatcher
def test():
graph = Graph("http://192.168.101.52:9696", auth=("neo4j", "neo4j1"))
node_matcher = NodeMatcher(graph)
relation_matcher = RelationshipMatcher(graph)
node1 = node_matcher.match("Person").where(name="小明").first()
node2 = node_matcher.match("Person").where(name="小红").first()
relation = relation_matcher.match((node2, node1), r_type="KNOWS").first()
graph.delete(relation)
if __name__ == '__main__':
test()