LineageInfoを使用したHiveテーブル関係の解析
4396 ワード
最近データの血縁をして、Hiveのソースコードの1つのクラスがHive表の関係を分析することができることを発見しました:org.apache.hadoop.hive.ql.tools.LineageInfo
このソースコードは次のとおりです.
mainの方法はすでにはっきり書いて使い方が分かりました.テストしてみましょう.
上記のようにclasspathに依存するjarパッケージを追加し、実行します.
しかし、一つの問題は、Create table as select文が血縁関係を正常に分析できないことです.
このソースコードは次のとおりです.
/**
*
* 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 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_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 rules = new LinkedHashMap();
// 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 = 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