PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/release-0.1-rc2/hive/external/ql/src/java/org/apache/hadoop/hive/ql/tools/LineageInfo.java

#
Java | 158 lines | 76 code | 24 blank | 58 comment | 7 complexity | ad37c9b110120f86b3396a0445746a00 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, JSON, CPL-1.0
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package org.apache.hadoop.hive.ql.tools;
  19. import java.io.IOException;
  20. import java.util.ArrayList;
  21. import java.util.LinkedHashMap;
  22. import java.util.Map;
  23. import java.util.Stack;
  24. import java.util.TreeSet;
  25. import org.apache.hadoop.hive.ql.lib.DefaultGraphWalker;
  26. import org.apache.hadoop.hive.ql.lib.DefaultRuleDispatcher;
  27. import org.apache.hadoop.hive.ql.lib.Dispatcher;
  28. import org.apache.hadoop.hive.ql.lib.GraphWalker;
  29. import org.apache.hadoop.hive.ql.lib.Node;
  30. import org.apache.hadoop.hive.ql.lib.NodeProcessor;
  31. import org.apache.hadoop.hive.ql.lib.NodeProcessorCtx;
  32. import org.apache.hadoop.hive.ql.lib.Rule;
  33. import org.apache.hadoop.hive.ql.parse.ASTNode;
  34. import org.apache.hadoop.hive.ql.parse.BaseSemanticAnalyzer;
  35. import org.apache.hadoop.hive.ql.parse.HiveParser;
  36. import org.apache.hadoop.hive.ql.parse.ParseDriver;
  37. import org.apache.hadoop.hive.ql.parse.ParseException;
  38. import org.apache.hadoop.hive.ql.parse.SemanticException;
  39. /**
  40. *
  41. * This class prints out the lineage info. It takes sql as input and prints
  42. * lineage info. Currently this prints only input and output tables for a given
  43. * sql. Later we can expand to add join tables etc.
  44. *
  45. */
  46. public class LineageInfo implements NodeProcessor {
  47. /**
  48. * Stores input tables in sql.
  49. */
  50. TreeSet<String> inputTableList = new TreeSet<String>();
  51. /**
  52. * Stores output tables in sql.
  53. */
  54. TreeSet<String> OutputTableList = new TreeSet<String>();
  55. /**
  56. *
  57. * @return java.util.TreeSet
  58. */
  59. public TreeSet<String> getInputTableList() {
  60. return inputTableList;
  61. }
  62. /**
  63. * @return java.util.TreeSet
  64. */
  65. public TreeSet<String> getOutputTableList() {
  66. return OutputTableList;
  67. }
  68. /**
  69. * Implements the process method for the NodeProcessor interface.
  70. */
  71. public Object process(Node nd, Stack<Node> stack, NodeProcessorCtx procCtx,
  72. Object... nodeOutputs) throws SemanticException {
  73. ASTNode pt = (ASTNode) nd;
  74. switch (pt.getToken().getType()) {
  75. case HiveParser.TOK_TAB:
  76. OutputTableList.add(BaseSemanticAnalyzer.getUnescapedName((ASTNode)pt.getChild(0)));
  77. break;
  78. case HiveParser.TOK_TABREF:
  79. ASTNode tabTree = (ASTNode) pt.getChild(0);
  80. String table_name = (tabTree.getChildCount() == 1) ?
  81. BaseSemanticAnalyzer.getUnescapedName((ASTNode)tabTree.getChild(0)) :
  82. BaseSemanticAnalyzer.getUnescapedName((ASTNode)tabTree.getChild(0)) + "." + tabTree.getChild(1);
  83. inputTableList.add(table_name);
  84. break;
  85. }
  86. return null;
  87. }
  88. /**
  89. * parses given query and gets the lineage info.
  90. *
  91. * @param query
  92. * @throws ParseException
  93. */
  94. public void getLineageInfo(String query) throws ParseException,
  95. SemanticException {
  96. /*
  97. * Get the AST tree
  98. */
  99. ParseDriver pd = new ParseDriver();
  100. ASTNode tree = pd.parse(query);
  101. while ((tree.getToken() == null) && (tree.getChildCount() > 0)) {
  102. tree = (ASTNode) tree.getChild(0);
  103. }
  104. /*
  105. * initialize Event Processor and dispatcher.
  106. */
  107. inputTableList.clear();
  108. OutputTableList.clear();
  109. // create a walker which walks the tree in a DFS manner while maintaining
  110. // the operator stack. The dispatcher
  111. // generates the plan from the operator tree
  112. Map<Rule, NodeProcessor> rules = new LinkedHashMap<Rule, NodeProcessor>();
  113. // The dispatcher fires the processor corresponding to the closest matching
  114. // rule and passes the context along
  115. Dispatcher disp = new DefaultRuleDispatcher(this, rules, null);
  116. GraphWalker ogw = new DefaultGraphWalker(disp);
  117. // Create a list of topop nodes
  118. ArrayList<Node> topNodes = new ArrayList<Node>();
  119. topNodes.add(tree);
  120. ogw.startWalking(topNodes, null);
  121. }
  122. public static void main(String[] args) throws IOException, ParseException,
  123. SemanticException {
  124. String query = args[0];
  125. LineageInfo lep = new LineageInfo();
  126. lep.getLineageInfo(query);
  127. for (String tab : lep.getInputTableList()) {
  128. System.out.println("InputTable=" + tab);
  129. }
  130. for (String tab : lep.getOutputTableList()) {
  131. System.out.println("OutputTable=" + tab);
  132. }
  133. }
  134. }