You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
2.0 KiB

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2023/4/15 11:05
# @Author : old tom
# @File : graph_helper.py
# @Project : futool-tiny-datahub
# @Desc : 血缘关系上图,关系查询
from py2neo import Node, Relationship, Graph, Subgraph
from datahub.metadata.constant.metadata_constant import MetaDataObjType
class MetaDataGraphBuilder(object):
def __init__(self, graph: Graph):
self.graph = graph
def add_view_relation(self, tables: list, view_name: str):
"""
添加视图关系
:param tables: 创建视图的数据名称及类型[(名称大写,类型)]
:param view_name: 视图名
:return:
"""
source_nodes = []
for t in tables:
# 只添加表名和属性
source_nodes.append(Node(t[1], name=t[0]))
source_nodes.append(Node(MetaDataObjType.View.value, name=view_name))
self._add_one_to_many_relation(source_nodes, 'from')
def add_table_relation(self):
pass
def add_procedure_relation(self, tables: list, procedure_name: str):
source_nodes = []
for t in tables:
# 只添加表名和属性
source_nodes.append(Node(t[1], name=t[0]))
source_nodes.append(Node(MetaDataObjType.View.value, name=procedure_name))
self._add_one_to_many_relation(source_nodes, 'into')
def _add_one_to_many_relation(self, nodes: [], relation):
"""
添加一对多关系
:param one_node: 单节点
:param other_nodes: 多节点
:param relation: 关系描述
:return:
"""
# 批量创建relation
relation_ls = []
for other in nodes[0:-1]:
relation_ls.append(Relationship(other, relation, nodes[-1]))
subgraph = Subgraph(nodes=nodes, relationships=relation_ls)
# 开启事务
tx = self.graph.begin()
try:
tx.create(subgraph)
self.graph.commit(tx)
except Exception as e:
self.graph.rollback(tx)
print('图创建失败', e)