import time
from py2neo import *
from py2neo.matching import *
class GraphRepository(object):
def __init__(self):
self.graph = Graph("bolt://xxx.xxx.xxx.xxx:xxx", auth=("root", "password"))
self.nodes = NodeMatcher(self.graph)
self.relations = RelationshipMatcher(self.graph)
def get_node(self, node_label, pri_properties):
"""
根据primary_key获取图中节点
"""
return self.nodes.match(node_label, **pri_properties).first()
def get_relation(self, node1, relation_label, node2, pri_properties):
"""
根据primary_key获取图中两节点之间的关系
"""
return self.relations.match(nodes=[node1, node2], r_type=relation_label, **pri_properties).first()
def node_create(self, node_label, properties):
"""
在图中查询节点是否存在,不存在则创建节点
primary_key作为点的主键,取能唯一标识的业务字段对其赋值
"""
pri_properties = {}
primary_key = str(properties['primary_key'])
pri_properties[primary_key] = properties[primary_key]
res = self.get_node(node_label, pri_properties)
if res is None:
properties['creat_time'] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
res = Node(node_label, **properties)
self.graph.create(res)
print('创建节点成功')
else:
print('节点已存在')
return res
def relation_create(self, node1, relation_label, node2, properties):
"""
在图中查询边关系是否存在,不存在则创建边
primary_key作为边的主键
"""
pri_properties = {}
primary_key = properties['primary_key']
pri_properties[primary_key] = properties[primary_key]
res = self.get_relation(node1, relation_label, node2, pri_properties)
if res is None:
properties['creat_time'] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
res = Relationship(node1, relation_label, node2, **properties)
self.graph.create(res)
print('创建关系成功')
else:
print('关系已存在')
def graph_create(self, node_label, node1_properties, node2_properties, relation_label, relation_properties):
node1 = self.node_create(node_label, node1_properties)
node2 = self.node_create(node_label, node2_properties)
self.relation_create(node1, relation_label, node2, relation_properties)
graphRepository = GraphRepository()
def test1():
node_label = 'TEST'
node1_properties = {
'primary_key': 'id',
'id': 'test12345678',
'name': '12345678test',
'aTime': 1,
'b_time': 2,
}
node2_properties = {
'primary_key': 'id', # 标识主键
'id': 'test123456789',
'name': '123456789test',
'aTime': 1,
'b_time': 2,
}
node1 = graphRepository.node_create(node_label, node1_properties)
node2 = graphRepository.node_create(node_label, node2_properties)
relation_label = 'testSend'
relation_properties = {
'primary_key': 'type',
'type': '关系类型test',
'aTime': 1,
'b_time': 2,
}
graphRepository.relation_create(node1, relation_label, node2, relation_properties)
def test2():
node_label = 'TEST'
nod1_pri_properties = {'id': 'test123'}
nod2_pri_properties = {'id': 'test1234'}
node1 = graphRepository.get_node(node_label, nod1_pri_properties)
node2 = graphRepository.get_node(node_label, nod2_pri_properties)
relation_label = 'testSend'
relation_properties = {
'primary_key': 'type',
'type': '关系类型test',
'aTime': 1,
'b_time': 2,
}
graphRepository.relation_create(node1, relation_label, node2, relation_properties)
def test3():
node_label = 'TEST'
node1_properties = {
'primary_key': 'id',
'id': 'test12345678',
'name': '12345678test',
'aTime': 1,
'b_time': 2,
}
node2_properties = {
'primary_key': 'id', # 标识主键
'id': 'test123456789',
'name': '123456789test',
'aTime': 1,
'b_time': 2,
}
relation_label = 'testSend'
relation_properties = {
'primary_key': 'type',
'type': '关系类型test',
'aTime': 1,
'b_time': 2,
}
graphRepository.graph_create(node_label,node1_properties,node2_properties,relation_label,relation_properties)
if __name__ == '__main__':
test2()
记得更改第一行ip和port以及user和password
效果图