最近要做数据血缘,发现了Hive源码的一个类可以用来分析Hive表的关系:
org.apache.hadoop.hive.ql.tools.LineageInfo
这个的源代码如下:
/**
*
* This class prints out the lineage info. It takes sql as input and prints
* lineage info. Currently this prints only input and output tables for a given
* sql. Later we can expand to add join tables etc.
*
*/
public class LineageInfo implements NodeProcessor {
/**
* Stores input tables in sql.
*/
TreeSet<String> inputTableList = new TreeSet<String>();
/**
* Stores output tables in sql.
*/
TreeSet<String> OutputTableList = new TreeSet<String>();
/**
*
* @return java.util.TreeSet
*/
public TreeSet<String> getInputTableList() {
return inputTableList;
}
/**
* @return java.util.TreeSet
*/
public TreeSet<String> getOutputTableList() {
return OutputTableList;
}
/**
* Implements the process method for the NodeProcessor interface.
*/
public Object process(Node nd, Stack<Node> stack, NodeProcessorCtx procCtx,
Object... nodeOutputs) throws SemanticException {
ASTNode pt = (ASTNode) nd;
switch (pt.getToken().getType()) {
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<Node> topNodes = new ArrayList<Node>();
topNodes.add(tree);
ogw.startWalking(topNodes, null);
}
public static void main(String[] args) throws IOException, ParseException,
SemanticException {
String query = args[0];
LineageInfo lep = new LineageInfo();
lep.getLineageInfo(query);
for (String tab : lep.getInputTableList()) {
System.out.println("InputTable=" + tab);
}
for (String tab : lep.getOutputTableList()) {
System.out.println("OutputTable=" + tab);
}
}
}
main 方法已经很清楚的写明白了用法,我们来测试一下:
#/bin/bash
java -cp .:/home/hadoop/hive-test/lib/*:/home/hadoop/exec/lib/* org.apache.hadoop.hive.ql.tools.LineageInfo "$1"
如上在classpath中加入各种依赖的jar包,然后咱们就开始执行之:
hadoop@hzbxs-bigdata16:~/hive-test$ ./test.sh "insert into tableA (id,name) select id,name from tableB"
InputTable=tableB
OutputTable=tableA
hadoop@hzbxs-bigdata16:~/hive-test$ ./test.sh "select * from tableA join (select * from tableB where id like '%helloworld%') temp on tableA.id = temp.id"
InputTable=tableA
InputTable=tableB
但是发现一个问题,就是Create table as select 语句不能正常分析出血缘关系:
hadoop@hzbxs-bigdata16:~/hive-test$ ./test.sh "create table tableA as select * from tableB"
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
InputTable=tableB