【hadoop】hadoop 血缘解析

128 篇文章 479 订阅 ¥29.90 ¥99.00
本文探讨了在Hadoop数据仓库中进行元数据血缘分析的方法,主要聚焦于通过解析HQL脚本来获取血缘信息。推荐使用Hadoop自带的`LineageInfo`类,尽管它在处理`create table xx as`语句时存在不足,但可以通过修改代码来完善。
摘要由CSDN通过智能技术生成

在这里插入图片描述

1.概述

转载:【数据仓库】元数据血缘分析

现在数据仓库基本上采用Hadoop平台了,那么数据仓库里面元数据的血缘分析的思路有哪些呢

基本上有下面这两种思路:

1、解析hql脚本,通过正则表达式去匹配每一行字符串

2、采用Hadoop自带的语法分析类解析

这里比较建议采用第二种,比较直接简单,因为第一种方式比较复杂,需要考虑场景比较多,容易出现遗漏

Hadoop 自带的类 org.apache.hadoop.hive.ql.tools.LineageInfo

将hql语句通过解析语法tree,获取hive表的源表和目标表,达到血缘分析的目的

但是这个类有一点缺陷就是对于create table xx as 这种hql语句无法解析

我们稍加修改代码就可以解决了

package com.neo.datamanager;
 
import org.apache.hadoop.hive.ql.lib.*;
import org.apache.hadoop.hive.ql.parse.*;
 
import java.io.IOException;
import java.util.*;
 
public class HiveLineageInfo implements NodeProcessor {
 
//    private static final Logger logger = LoggerFactory.getLogger(HiveLineageInfo.class);
 
    /**
     * Stores input tables in sql.
     */
    TreeSet inputTableList = new TreeSet();
    /**
     * Stores output tables in sql.
     */
    TreeSet OutputTableList = new TreeSet();
 
    /**
     * @return java.util.TreeSet
     */
    public TreeSet getInputTableList() {
        return inputTableList;
    }
 
    /**
     * @return java.util.TreeSet
     */
    public TreeSet getOutputTableList() {
        return OutputTableList;
    }
 
    /**
     * Implements the process method for the NodeProcessor interface.
     */
    public Object process(Node nd, Stack stack, NodeProcessorCtx procCtx,
                          Object... nodeOutputs) throws SemanticException {
        ASTNode pt = (ASTNode) nd;
 
        switch (pt.getToken().getType()) {
 
            case HiveParser.TOK_CREATETABLE:
                OutputTableList.add(BaseSemanticAnalyzer.getUnescapedName((ASTNode) pt.getChild(0)));
                break;
            case HiveParser.TOK_TAB:
                OutputTableList.add(BaseSemanticAnalyzer.getUnescapedName((ASTNode) pt.getChild(0)));
                break;
 
            case HiveParser.TOK_TABREF:
                ASTNode tabTree = (ASTNode) pt.getChild(0);
                String table_name = (tabTree.getChildCount() == 1) ?
                        BaseSemanticAnalyzer.getUnescapedName((ASTNode) tabTree.getChild(0)) :
                        BaseSemanticAnalyzer.getUnescapedName((ASTNode) tabTree.getChild(0)) + "." + tabTree.getChild(1);
                inputTableList.add(table_name);
                break;
        }
        return null;
    }
 
    /**
     * parses given query and gets the lineage info.
     *
     * @param query
     * @throws ParseException
     */
    public void getLineageInfo(String query) throws ParseException,
            SemanticException {
 
    /*
     * Get the AST tree
     */
        ParseDriver pd = new ParseDriver();
        ASTNode tree = pd.parse(query);
 
        while ((tree.getToken() == null) && (tree.getChildCount() > 0)) {
            tree = (ASTNode) tree.getChild(0);
        }
 
    /*
     * initialize Event Processor and dispatcher.
     */
        inputTableList.clear();
        OutputTableList.clear();
 
        // create a walker which walks the tree in a DFS manner while maintaining
        // the operator stack. The dispatcher
        // generates the plan from the operator tree
        Map<Rule, NodeProcessor> rules = new LinkedHashMap<Rule, NodeProcessor>();
 
        // The dispatcher fires the processor corresponding to the closest matching
        // rule and passes the context along
        Dispatcher disp = new DefaultRuleDispatcher(this, rules, null);
        GraphWalker ogw = new DefaultGraphWalker(disp);
 
        // Create a list of topop nodes
        ArrayList topNodes = new ArrayList();
        topNodes.add(tree);
        ogw.startWalking(topNodes, null);
    }
 
    public static void main(String[] args) throws IOException, ParseException, SemanticException {
        String query = "insert into table aa  select * from bb union all select * from cc";
        HiveLineageInfo lep = new HiveLineageInfo();
        lep.getLineageInfo(query);
        System.out.println("Input tables = " + lep.getInputTableList());
        System.out.println("Output tables = " + lep.getOutputTableList());
    }
}
 

运行之后结果如下:

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值