一、连接数据库
from py2neo import Graph “”” host:服务器ip地址,默认为localhost http_port:http协议——服务器监听端口,默认7474 https_port:https协议——服务器监听端口,默认7473 user:登录用户名,默认neo4j password:登录密码,无默认值,故若数据库其他参数都为默认值,则可直接通过密码登录 name:指定为数据库的名称,如果不指定为neo4j的话,会出现以下报错: py2neo.errors.P我爱线报网rotocolError: Cannot decode response content as JSON “”” g = Graph(http://localhost:7474, auth=(“neo4j”, “88888888”), name=“neo4j”) print(“创建成功”)二、创建结点和边
2.1 两个结点+一条边创建
from py2neo import Graph, Node, Relationship g = Graph(http://localhost:7474, auth=(“neo4j”, “88888888”), name=“neo4j”) pr我爱线报网int(“连接成功”) a = Node(Person, name=Alice) b = Node(Person, name=Bob) ab = Relationship(a, KNOWS, b) g.create(ab) print(“创建成功”)2.2 多节点/边创建
# 新版 from py2neo import Graph, Node, Relationship, Subgraph g = Graph(http://localhost:7474, auth=(neo4j, 88888888), name=“neo4j”) print(“连接成功我爱线报网“) # 开始进行图数据库的操作 tx = g.begin() # 结点创建(人) li_elder_woman = Node(“Person”, name=“李奶奶”, age=66) li_elder_man = Node(“Person”, name=“李爷爷”, age=67) li_woman = Node(“Person”, name=“李妈”) li_child = Node(“Person”, name=“小李”) # 结点创建(动物) cat = Node(“Animal”, name=cat) dog = Node(“Animal”, name=dog)我爱线报网 wife = Relationship(li_elder_man,“妻子”, li_elder_woman) husband = Relationship(li_elder_woman, “丈夫”, li_elder_man) father = Relationship(li_woman, “父亲”, li_elder_man) mother = Relationship(li_woman, “母亲”, li_elder_woman) mother2 = Relationship(li_child, “母亲”, li_woman) child = R我爱线报网elationship(li_woman,“孩子”, li_child) child2 = Relationship(li_elder_woman, “孩子”, li_woman) child3 = Relationship(li_elder_man, “孩子”, li_woman) grandson = Relationship(li_elder_woman, “孙子”, li_child) grandson2 = Relationship(li_elder_man, “丈夫”, li_child) relation_list = Subgraph(rela我爱线报网tionships=[wife, husband, father, mother, child, grandson])# 创建 tx.create(relation_list) print(“创建成功”) # 执行 try: tx.commit() print(“执行成功”) except: print(“执行失败”)效果图
三、匹配节点
3.1 匹配所有节点
import connect g = connect.connect() # 匹配所有的结点 nodes = g.nodes.match() for node in nodes: # node是一个类,表示一个结点 print(node) # 展示我爱线报网他的属性 for item in node.items(): # 这里是键值对,[key, value] print(“key=” + item[0] + “, value=” + str(item[1])) print(“label=” + str(node.labels)) print(“=” * 10 + “结点属性” + “=” * 10) # 展示所有节点的属性 for attr in dir(node): print(attr)3.2 匹配指定的节点
可以用匹配器来匹配:matcher = NodeMatcher(g)
from py2neo import NodeMatcher importconnect g = connect.co我爱线报网nnect()# 用来查找节点的对象 matcher = NodeMatcher(g) # first 返回第一个符合条件的节点 print(“=” * 20 + “测试1” + “=” * 20) 用法一 nodes = matcher.match(label, attr1=value1, attr2=value2) 用法二 nodes = matcher.match(label).where(“_.attr1=value1, _.attr2=value2”) node1 = matcher.match(Person, name=小李).first() # node1 =我爱线报网 matcher.match(Person).where(“_.name=小李”).first() print(“返回第一个符合要求的节点”) print(node1) print(“=” * 20 + “测试2” + “=” * 20) node2 = matcher.match(Person, age=67).first() print(“返回第一个符合要求的节点”) print(node2) # all 返回所有符合条件的节点 nodes = matcher.match(Person) for node in nodes: print(姓名:%s \t 年龄:%s % (我爱线报网node[name], node[age]))注意:matcher.match返回的是一个对象,并不是节点数组,其中
first方法:返回第一个匹配的节点all方法:返回所有匹配的节点,返回类型是个数组四、更新节点
4.1 更新单个节点
import connect from py2neo import NodeMatcher, Subgraph g = connect.connect() tx = g.begin() # 找到你要找的Nodes matcher = NodeMatcher(g) # 修改单个节点 init_node = matcher.match(“Person”, name=“李爷我爱线报网爷”).first() # init_node = matcher.match(“Person”).where(_.name=”徐福贵”) init_node[name] = “李爷” sub = Subgraph(nodes=[init_node]) tx.push(sub) tx.commit() print(“执行成功”)4.2 更新多个节点
# 修改多个节点 from py2neo import NodeMatcher, Subgraph import connect g = connect.connect() tx = g.begin() # 找到你要找的Nodesmatch我爱线报网er = NodeMatcher(g) init_node = matcher.match(“Person”) new_nodes = [] for node in init_node.all(): node[name] = “里” + node[name][1:] new_nodes.append(node) # 制作图 sub = Subgraph(nodes=new_nodes) # 将子图push进tx当中 tx.push(sub) # 执行命令 tx.commit() print(“执行成功”)五、删除边
描述:一般都是删除边,没有说删除节点的,因为删掉节点的话,相应的边就会凭我爱线报网空出来。所以一般都是删除边。
5.1 当节点没有了相连接的边的时候,就会自动删掉该节点
from py2neo import NodeMatcher, RelationshipMatcher import connect g = connect.connect() matcher = NodeMatcher(g) r_matcher = RelationshipMatcher(g) nodes = matcher.match(Person).all() length = len(nodes) for i in range(length): node = nodes[i] # nodes=[None,b] 表示所有以b为终点的关系 # nodes=我爱线报网[a,None] 表示所有以a为起点的关系 # nodes=[None,None] 表示所有节点的关系 relation1 = r_matcher.match(nodes=[node, None]).all() relation2 = r_matcher.match(nodes=[None, node]).all() for edge in relation1: g.delete(edge) for edge in relation2: g.delete(edge) print(“第%i个节点删除成功” % i) # print(relation) # g.delete(relation) # print(“删除成功”)5.2 我爱线报网只删除边不删除节点
from py2neo import NodeMatcher, RelationshipMatcher import connect g = connect.connect() matcher = NodeMatcher(g) r_matcher = RelationshipMatcher(g) li_1 = matcher.match(Person, name=里ob).first() li_2 = matcher.match(Person, name=里lice).first() relation = r_matcher.matc我爱线报网h(nodes=[li_2, li_1]).first()print(relation) g.separate(relation) print(“删除完成”)六、给两个节点新增关系
from py2neo import Relationship, NodeMatcher import connect g = connect.connect() matcher = NodeMatcher(g) li = matcher.match(Person, name=里李).first() alice = matcher.match(Person, name我爱线报网=里lice).first() relation = Relationship(li, 朋友, alice) g.create(relation) relation = Relationship(alice, 朋友, li) g.create(relation) print(“添加关系完成”)七、命令形式执行代码
查看:Cypher语言学习




































































