PageRenderTime 24ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/ql/src/test/org/apache/hadoop/hive/ql/history/TestHiveHistory.java

http://github.com/apache/hive
Java | 232 lines | 164 code | 29 blank | 39 comment | 15 complexity | b4e52437e754910c7c3341417f3213de MD5 | raw file
Possible License(s): Apache-2.0
  1. package org.apache.hadoop.hive.ql.history;
  2. /**
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. import java.io.PrintStream;
  20. import java.io.UnsupportedEncodingException;
  21. import java.lang.reflect.Proxy;
  22. import java.util.LinkedList;
  23. import java.util.Map;
  24. import junit.framework.TestCase;
  25. import org.apache.hadoop.fs.FileSystem;
  26. import org.apache.hadoop.fs.Path;
  27. import org.apache.hadoop.hive.cli.CliSessionState;
  28. import org.apache.hadoop.hive.common.LogUtils;
  29. import org.apache.hadoop.hive.common.LogUtils.LogInitializationException;
  30. import org.apache.hadoop.hive.conf.HiveConf;
  31. import org.apache.hadoop.hive.conf.HiveConf.ConfVars;
  32. import org.apache.hadoop.hive.metastore.MetaStoreUtils;
  33. import org.apache.hadoop.hive.ql.Driver;
  34. import org.apache.hadoop.hive.ql.history.HiveHistory.Keys;
  35. import org.apache.hadoop.hive.ql.history.HiveHistory.QueryInfo;
  36. import org.apache.hadoop.hive.ql.history.HiveHistory.TaskInfo;
  37. import org.apache.hadoop.hive.ql.io.IgnoreKeyTextOutputFormat;
  38. import org.apache.hadoop.hive.ql.metadata.Hive;
  39. import org.apache.hadoop.hive.ql.session.SessionState;
  40. import org.apache.hadoop.hive.ql.tools.LineageInfo;
  41. import org.apache.hadoop.mapred.TextInputFormat;
  42. /**
  43. * TestHiveHistory.
  44. *
  45. */
  46. public class TestHiveHistory extends TestCase {
  47. static HiveConf conf;
  48. private static String tmpdir = System.getProperty("test.tmp.dir");
  49. private static Path tmppath = new Path(tmpdir);
  50. private static Hive db;
  51. private static FileSystem fs;
  52. /*
  53. * intialize the tables
  54. */
  55. @Override
  56. protected void setUp() {
  57. try {
  58. conf = new HiveConf(HiveHistory.class);
  59. SessionState.start(conf);
  60. fs = FileSystem.get(conf);
  61. if (fs.exists(tmppath) && !fs.getFileStatus(tmppath).isDir()) {
  62. throw new RuntimeException(tmpdir + " exists but is not a directory");
  63. }
  64. if (!fs.exists(tmppath)) {
  65. if (!fs.mkdirs(tmppath)) {
  66. throw new RuntimeException("Could not make scratch directory "
  67. + tmpdir);
  68. }
  69. }
  70. conf.setBoolVar(HiveConf.ConfVars.HIVE_SUPPORT_CONCURRENCY, false);
  71. // copy the test files into hadoop if required.
  72. int i = 0;
  73. Path[] hadoopDataFile = new Path[2];
  74. String[] testFiles = {"kv1.txt", "kv2.txt"};
  75. String testFileDir = new Path(conf.get("test.data.files")).toUri().getPath();
  76. for (String oneFile : testFiles) {
  77. Path localDataFile = new Path(testFileDir, oneFile);
  78. hadoopDataFile[i] = new Path(tmppath, oneFile);
  79. fs.copyFromLocalFile(false, true, localDataFile, hadoopDataFile[i]);
  80. i++;
  81. }
  82. // load the test files into tables
  83. i = 0;
  84. db = Hive.get(conf);
  85. String[] srctables = {"src", "src2"};
  86. LinkedList<String> cols = new LinkedList<String>();
  87. cols.add("key");
  88. cols.add("value");
  89. for (String src : srctables) {
  90. db.dropTable(MetaStoreUtils.DEFAULT_DATABASE_NAME, src, true, true);
  91. db.createTable(src, cols, null, TextInputFormat.class,
  92. IgnoreKeyTextOutputFormat.class);
  93. db.loadTable(hadoopDataFile[i], src, false, false);
  94. i++;
  95. }
  96. } catch (Throwable e) {
  97. e.printStackTrace();
  98. throw new RuntimeException("Encountered throwable");
  99. }
  100. }
  101. /**
  102. * Check history file output for this query.
  103. */
  104. public void testSimpleQuery() {
  105. new LineageInfo();
  106. try {
  107. // NOTE: It is critical to do this here so that log4j is reinitialized
  108. // before any of the other core hive classes are loaded
  109. try {
  110. LogUtils.initHiveLog4j();
  111. } catch (LogInitializationException e) {
  112. }
  113. HiveConf hconf = new HiveConf(SessionState.class);
  114. hconf.setBoolVar(ConfVars.HIVE_SESSION_HISTORY_ENABLED, true);
  115. CliSessionState ss = new CliSessionState(hconf);
  116. ss.in = System.in;
  117. try {
  118. ss.out = new PrintStream(System.out, true, "UTF-8");
  119. ss.err = new PrintStream(System.err, true, "UTF-8");
  120. } catch (UnsupportedEncodingException e) {
  121. System.exit(3);
  122. }
  123. SessionState.start(ss);
  124. String cmd = "select a.key from src a";
  125. Driver d = new Driver(conf);
  126. int ret = d.run(cmd).getResponseCode();
  127. if (ret != 0) {
  128. fail("Failed");
  129. }
  130. HiveHistoryViewer hv = new HiveHistoryViewer(SessionState.get()
  131. .getHiveHistory().getHistFileName());
  132. Map<String, QueryInfo> jobInfoMap = hv.getJobInfoMap();
  133. Map<String, TaskInfo> taskInfoMap = hv.getTaskInfoMap();
  134. if (jobInfoMap.size() != 1) {
  135. fail("jobInfo Map size not 1");
  136. }
  137. if (taskInfoMap.size() != 1) {
  138. fail("jobInfo Map size not 1");
  139. }
  140. cmd = (String) jobInfoMap.keySet().toArray()[0];
  141. QueryInfo ji = jobInfoMap.get(cmd);
  142. if (!ji.hm.get(Keys.QUERY_NUM_TASKS.name()).equals("1")) {
  143. fail("Wrong number of tasks");
  144. }
  145. } catch (Exception e) {
  146. e.printStackTrace();
  147. fail("Failed");
  148. }
  149. }
  150. public void testQueryloglocParentDirNotExist() throws Exception {
  151. String parentTmpDir = tmpdir + "/HIVE2654";
  152. Path parentDirPath = new Path(parentTmpDir);
  153. try {
  154. fs.delete(parentDirPath, true);
  155. } catch (Exception e) {
  156. }
  157. try {
  158. String actualDir = parentTmpDir + "/test";
  159. HiveConf conf = new HiveConf(SessionState.class);
  160. conf.set(HiveConf.ConfVars.HIVEHISTORYFILELOC.toString(), actualDir);
  161. SessionState ss = new CliSessionState(conf);
  162. HiveHistory hiveHistory = new HiveHistoryImpl(ss);
  163. Path actualPath = new Path(actualDir);
  164. if (!fs.exists(actualPath)) {
  165. fail("Query location path is not exist :" + actualPath.toString());
  166. }
  167. } finally {
  168. try {
  169. fs.delete(parentDirPath, true);
  170. } catch (Exception e) {
  171. }
  172. }
  173. }
  174. /**
  175. * Check if HiveHistoryImpl class is returned when hive history is enabled
  176. * @throws Exception
  177. */
  178. public void testHiveHistoryConfigEnabled() throws Exception {
  179. HiveConf conf = new HiveConf(SessionState.class);
  180. conf.setBoolVar(ConfVars.HIVE_SESSION_HISTORY_ENABLED, true);
  181. SessionState ss = new CliSessionState(conf);
  182. SessionState.start(ss);
  183. HiveHistory hHistory = ss.getHiveHistory();
  184. assertEquals("checking hive history class when history is enabled",
  185. hHistory.getClass(), HiveHistoryImpl.class);
  186. }
  187. /**
  188. * Check if HiveHistory class is a Proxy class when hive history is disabled
  189. * @throws Exception
  190. */
  191. public void testHiveHistoryConfigDisabled() throws Exception {
  192. HiveConf conf = new HiveConf(SessionState.class);
  193. conf.setBoolVar(ConfVars.HIVE_SESSION_HISTORY_ENABLED, false);
  194. SessionState ss = new CliSessionState(conf);
  195. SessionState.start(ss);
  196. HiveHistory hHistory = ss.getHiveHistory();
  197. assertTrue("checking hive history class when history is disabled",
  198. hHistory.getClass() != HiveHistoryImpl.class);
  199. System.err.println("hHistory.getClass" + hHistory.getClass());
  200. assertTrue("verifying proxy class is used when history is disabled",
  201. Proxy.isProxyClass(hHistory.getClass()));
  202. }
  203. }