- 导入并连接:
from py2neo import *
graph = Graph("http://127.0.0.1:7474",auth=("账号","密码"))
- 新建节点与关系:
head = Node("head_label", name='head_name')
tail = Node("tail_label", name='tail_name')
relation = Relationship(head, "relation_name", tail)
graph.create(relation)
- 创建时合并相同节点:
node_list = list(matcher.match("node_label",name='node_name'))
if len(node_list) > 0:
node = node_list[0]
else:
node = Node("node_label",name='node_name')
graph.create(node)
- 查询节点与关系:
data_list = list(graph.match(r_type="relation_name")
matcher = NodeMatcher(graph)
node_list = list(matcher.match("node_label",name='node_name'))
r_matcher = RelationshipMatcher(g)
relation_list = list(r_matcher.match(nodes=[fugui, youqian]))
node = matcher[node_id]
node = graph.nodes[node_id]
graph.nodes
graph.relationships
graph.nodes.match("node_label",name='node_name')
- 追加属性与删除属性:
node_list = list(matcher.match("node_label",name='node_name'))
node.setdefault('age',default='30')
node_list[0]['home'] = 'jiangxi'
node_list[0].remove_label('age')
graph.push(node_list[0])
atrributes = { 'name': 'Amy', 'age': 23 }
node.update(atrributes)
- 事务与批处理:
tx = graph.begin()
relation_list = Subgraph(relationship=[relation_1,relation_2,...])
tx.create(relation_list)
graph.commit(tx)
- 删除节点和关系:
graph.delete_all()
graph.delete(node)
graph.delete(relation)
graph.separate(relation)
- 其余操作:
node.idendity
node[key]
node[key] = value
del node[key]
len(node)
labels = node.labels
node.labels.remove(labelname)
dict(node)
graph.schema.node_labels
graph.schema.relationship_types
node_list.to_data_frame()
node_list.data()
node_list.to_ndarray()
- 使用
cyper(CQL)
语句:
graph.run('MATCH (p:Person) return p')
graph.run('MATCH (n) DETACH DELETE n')