您当前的位置: 首页 >  阿里云云栖号

基于MaxCompute InformationSchema进行血缘关系分析

阿里云云栖号 发布时间:2020-03-12 14:48:18 ,浏览量:2

一、需求场景分析 在实际的数据平台运营管理过程中,数据表的规模往往随着更多业务数据的接入以及数据应用的建设而逐渐增长到非常大的规模,数据管理人员往往希望能够利用元数据的分析来更好地掌握不同数据表的血缘关系,从而分析出数据的上下游依赖关系。 本文将介绍如何去根据MaxCompute InformationSchema中作业ID的输入输出表来分析出某张表的血缘关系。 二、方案设计思路 MaxCompute Information_Schema提供了访问表的作业明细数据tasks_history,该表中有作业ID、input_tables、output_tables字段记录表的上下游依赖关系。根据这三个字段统计分析出表的血缘关系 1、根据某1天的作业历史,通过获取tasks_history表里的input_tables、output_tables、作业ID字段的详细信息,然后分析统计一定时间内的各个表的上下游依赖关系。 2、根据表上下游依赖推测出血缘关系。 三、方案实现方法 参考示例一: (1)根据作业ID查询某表上下游依赖SQL处理如下:

select
t2.input_table,
t1.inst_id,
replace(replace(t1.output_tables,"[",""),"]","") as output_table
from information_schema.tasks_history  t1
left join
(
    select
    ---去除表开始和结尾的[ ]
    trans_array(1,",",inst_id,
    replace(replace(input_tables,"[",""),"]","")) as (inst_id,input_table)
    from information_schema.tasks_history  where ds = 20190902 
)t2
on t1.inst_id = t2.inst_id
where (replace(replace(t1.output_tables,"[",""),"]",""))  ""
order by t2.input_table limit 1000;

结果如下图所示:image.png

(2)根据结果可以分析得出每张表张表的输入表输出表以及连接的作业ID,即每张表的血缘关系。 血缘关系位图如下图所示:image.png

中间连线为作业ID,连线起始为输入表,箭头所指方向为输出表。 参考示例二: 以下方式是通过设置分区,结合DataWorks去分析血缘关系: (1)设计存储结果表Schema

CREATE TABLE IF NOT EXISTS dim_meta_tasks_history_a
(
    stat_date         STRING COMMENT '统计日期',
    project_name      STRING COMMENT '项目名称',
    task_id           STRING COMMENT '作业ID',
    start_time        STRING COMMENT '开始时间',
    end_time          STRING COMMENT '结束时间',
    input_table       STRING COMMENT '输入表',
    output_table      STRING COMMENT '输出表',
    etl_date          STRING COMMENT 'ETL运行时间'
);

(2)关键解析sql

SELECT 
'${yesterday}'      AS stat_date
,'project_name'     AS project_name
,a.inst_id          AS task_id
,start_time         AS start_time
,end_time           AS end_time
,a.input_table      AS input_table
,a.output_table     AS output_table
,GETDATE()          AS etl_date
FROM (
    SELECT 
        t2.input_table    
        ,t1.inst_id
        ,replace(replace(t1.input_tables,"[",""),"]","") AS output_table
        ,start_time        
        ,end_time        
    FROM (
        SELECT
            *
            ,ROW_NUMBER() OVER(PARTITION BY output_tables ORDER BY end_time DESC) AS rows
        FROM information_schema.tasks_history
        WHERE operation_text LIKE 'INSERT OVERWRITE TABLE%'
        AND (
            start_time >= TO_DATE('${yesterday}','yyyy-mm-dd')
            and
            end_time             
关注
打赏
1688896170
查看更多评论
0.0868s