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

/tags/release-0.0.0-rc0/hive/external/service/src/test/org/apache/hadoop/hive/service/TestHiveServer.java

#
Java | 335 lines | 245 code | 46 blank | 44 comment | 17 complexity | 6e20eccfa9a8a138149eb8e02071cc07 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.service;
  19. import java.util.List;
  20. import java.util.Properties;
  21. import junit.framework.TestCase;
  22. import org.apache.hadoop.conf.Configuration;
  23. import org.apache.hadoop.fs.Path;
  24. import org.apache.hadoop.hive.conf.HiveConf;
  25. import org.apache.hadoop.hive.metastore.api.FieldSchema;
  26. import org.apache.hadoop.hive.metastore.api.Schema;
  27. import org.apache.hadoop.hive.serde.Constants;
  28. import org.apache.hadoop.hive.serde2.dynamic_type.DynamicSerDe;
  29. import org.apache.hadoop.io.BytesWritable;
  30. import org.apache.thrift.protocol.TBinaryProtocol;
  31. import org.apache.thrift.protocol.TProtocol;
  32. import org.apache.thrift.transport.TSocket;
  33. import org.apache.thrift.transport.TTransport;
  34. /**
  35. * TestHiveServer.
  36. *
  37. */
  38. public class TestHiveServer extends TestCase {
  39. private HiveInterface client;
  40. private static final String host = "localhost";
  41. private static final int port = 10000;
  42. private final Path dataFilePath;
  43. private static String tableName = "testhivedrivertable";
  44. private final HiveConf conf;
  45. private boolean standAloneServer = false;
  46. private TTransport transport;
  47. public TestHiveServer(String name) {
  48. super(name);
  49. conf = new HiveConf(TestHiveServer.class);
  50. String dataFileDir = conf.get("test.data.files").replace('\\', '/')
  51. .replace("c:", "");
  52. dataFilePath = new Path(dataFileDir, "kv1.txt");
  53. // See data/conf/hive-site.xml
  54. String paramStr = System.getProperty("test.service.standalone.server");
  55. if (paramStr != null && paramStr.equals("true")) {
  56. standAloneServer = true;
  57. }
  58. }
  59. @Override
  60. protected void setUp() throws Exception {
  61. super.setUp();
  62. if (standAloneServer) {
  63. try {
  64. transport = new TSocket(host, port);
  65. TProtocol protocol = new TBinaryProtocol(transport);
  66. client = new HiveClient(protocol);
  67. transport.open();
  68. } catch (Throwable e) {
  69. e.printStackTrace();
  70. }
  71. } else {
  72. client = new HiveServer.HiveServerHandler();
  73. }
  74. }
  75. @Override
  76. protected void tearDown() throws Exception {
  77. super.tearDown();
  78. if (standAloneServer) {
  79. transport.close();
  80. }
  81. }
  82. public void testExecute() throws Exception {
  83. try {
  84. client.execute("set hive.support.concurrency = false");
  85. client.execute("drop table " + tableName);
  86. } catch (Exception ex) {
  87. }
  88. try {
  89. client.execute("create table " + tableName + " (num int)");
  90. client.execute("load data local inpath '" + dataFilePath.toString()
  91. + "' into table " + tableName);
  92. client.execute("select count(1) as cnt from " + tableName);
  93. String row = client.fetchOne();
  94. assertEquals(row, "500");
  95. Schema hiveSchema = client.getSchema();
  96. List<FieldSchema> listFields = hiveSchema.getFieldSchemas();
  97. assertEquals(listFields.size(), 1);
  98. assertEquals(listFields.get(0).getName(), "cnt");
  99. assertEquals(listFields.get(0).getType(), "bigint");
  100. Schema thriftSchema = client.getThriftSchema();
  101. List<FieldSchema> listThriftFields = thriftSchema.getFieldSchemas();
  102. assertEquals(listThriftFields.size(), 1);
  103. assertEquals(listThriftFields.get(0).getName(), "cnt");
  104. assertEquals(listThriftFields.get(0).getType(), "i64");
  105. client.execute("drop table " + tableName);
  106. } catch (Throwable t) {
  107. t.printStackTrace();
  108. }
  109. }
  110. public void notestExecute() throws Exception {
  111. try {
  112. client.execute("set hive.support.concurrency = false");
  113. client.execute("drop table " + tableName);
  114. } catch (Exception ex) {
  115. }
  116. client.execute("create table " + tableName + " (num int)");
  117. client.execute("load data local inpath '" + dataFilePath.toString()
  118. + "' into table " + tableName);
  119. client.execute("select count(1) from " + tableName);
  120. String row = client.fetchOne();
  121. assertEquals(row, "500");
  122. client.execute("drop table " + tableName);
  123. transport.close();
  124. }
  125. public void testNonHiveCommand() throws Exception {
  126. try {
  127. client.execute("set hive.support.concurrency = false");
  128. client.execute("drop table " + tableName);
  129. } catch (Exception ex) {
  130. }
  131. client.execute("create table " + tableName + " (num int)");
  132. client.execute("load data local inpath '" + dataFilePath.toString()
  133. + "' into table " + tableName);
  134. // Command not part of HiveQL - verify no results
  135. client.execute("SET hive.mapred.mode = nonstrict");
  136. Schema schema = client.getSchema();
  137. assertEquals(schema.getFieldSchemasSize(), 0);
  138. assertEquals(schema.getPropertiesSize(), 0);
  139. Schema thriftschema = client.getThriftSchema();
  140. assertEquals(thriftschema.getFieldSchemasSize(), 0);
  141. assertEquals(thriftschema.getPropertiesSize(), 0);
  142. assertEquals(client.fetchOne(), "");
  143. assertEquals(client.fetchN(10).size(), 0);
  144. assertEquals(client.fetchAll().size(), 0);
  145. // Execute Hive query and fetch
  146. client.execute("select * from " + tableName + " limit 10");
  147. client.fetchOne();
  148. // Re-execute command not part of HiveQL - verify still no results
  149. client.execute("SET hive.mapred.mode = nonstrict");
  150. schema = client.getSchema();
  151. assertEquals(schema.getFieldSchemasSize(), 0);
  152. assertEquals(schema.getPropertiesSize(), 0);
  153. thriftschema = client.getThriftSchema();
  154. assertEquals(thriftschema.getFieldSchemasSize(), 0);
  155. assertEquals(thriftschema.getPropertiesSize(), 0);
  156. assertEquals(client.fetchOne(), "");
  157. assertEquals(client.fetchN(10).size(), 0);
  158. assertEquals(client.fetchAll().size(), 0);
  159. // Cleanup
  160. client.execute("drop table " + tableName);
  161. }
  162. /**
  163. * Test metastore call.
  164. */
  165. public void testMetastore() throws Exception {
  166. try {
  167. client.execute("set hive.support.concurrency = false");
  168. client.execute("drop table " + tableName);
  169. } catch (Exception ex) {
  170. }
  171. client.execute("create table " + tableName + " (num int)");
  172. List<String> tabs = client.get_tables("default", tableName);
  173. assertEquals(tabs.get(0), tableName);
  174. client.execute("drop table " + tableName);
  175. }
  176. /**
  177. * Test cluster status retrieval.
  178. */
  179. public void testGetClusterStatus() throws Exception {
  180. HiveClusterStatus clusterStatus = client.getClusterStatus();
  181. assertNotNull(clusterStatus);
  182. assertTrue(clusterStatus.getTaskTrackers() >= 0);
  183. assertTrue(clusterStatus.getMapTasks() >= 0);
  184. assertTrue(clusterStatus.getReduceTasks() >= 0);
  185. assertTrue(clusterStatus.getMaxMapTasks() >= 0);
  186. assertTrue(clusterStatus.getMaxReduceTasks() >= 0);
  187. assertTrue(clusterStatus.getState() == JobTrackerState.INITIALIZING
  188. || clusterStatus.getState() == JobTrackerState.RUNNING);
  189. }
  190. /**
  191. *
  192. */
  193. public void testFetch() throws Exception {
  194. // create and populate a table with 500 rows.
  195. try {
  196. client.execute("set hive.support.concurrency = false");
  197. client.execute("drop table " + tableName);
  198. } catch (Exception ex) {
  199. }
  200. client.execute("create table " + tableName + " (key int, value string)");
  201. client.execute("load data local inpath '" + dataFilePath.toString()
  202. + "' into table " + tableName);
  203. try {
  204. // fetchAll test
  205. client.execute("select key, value from " + tableName);
  206. assertEquals(client.fetchAll().size(), 500);
  207. assertEquals(client.fetchAll().size(), 0);
  208. // fetchOne test
  209. client.execute("select key, value from " + tableName);
  210. for (int i = 0; i < 500; i++) {
  211. String str = client.fetchOne();
  212. if (str.equals("")) {
  213. assertTrue(false);
  214. }
  215. }
  216. assertEquals(client.fetchOne(), "");
  217. // fetchN test
  218. client.execute("select key, value from " + tableName);
  219. assertEquals(client.fetchN(499).size(), 499);
  220. assertEquals(client.fetchN(499).size(), 1);
  221. assertEquals(client.fetchN(499).size(), 0);
  222. } catch (Throwable e) {
  223. e.printStackTrace();
  224. }
  225. }
  226. public void testDynamicSerde() throws Exception {
  227. try {
  228. client.execute("set hive.support.concurrency = false");
  229. client.execute("drop table " + tableName);
  230. } catch (Exception ex) {
  231. }
  232. client.execute("create table " + tableName + " (key int, value string)");
  233. client.execute("load data local inpath '" + dataFilePath.toString()
  234. + "' into table " + tableName);
  235. // client.execute("select key, count(1) from " + tableName +
  236. // " where key > 10 group by key");
  237. String sql = "select key, value from " + tableName + " where key > 10";
  238. client.execute(sql);
  239. // Instantiate DynamicSerDe
  240. DynamicSerDe ds = new DynamicSerDe();
  241. Properties dsp = new Properties();
  242. dsp.setProperty(Constants.SERIALIZATION_FORMAT,
  243. org.apache.hadoop.hive.serde2.thrift.TCTLSeparatedProtocol.class
  244. .getName());
  245. dsp.setProperty(
  246. org.apache.hadoop.hive.metastore.api.Constants.META_TABLE_NAME,
  247. "result");
  248. String serDDL = new String("struct result { ");
  249. List<FieldSchema> schema = client.getThriftSchema().getFieldSchemas();
  250. for (int pos = 0; pos < schema.size(); pos++) {
  251. if (pos != 0) {
  252. serDDL = serDDL.concat(",");
  253. }
  254. serDDL = serDDL.concat(schema.get(pos).getType());
  255. serDDL = serDDL.concat(" ");
  256. serDDL = serDDL.concat(schema.get(pos).getName());
  257. }
  258. serDDL = serDDL.concat("}");
  259. dsp.setProperty(Constants.SERIALIZATION_DDL, serDDL);
  260. dsp.setProperty(Constants.SERIALIZATION_LIB, ds.getClass().toString());
  261. dsp.setProperty(Constants.FIELD_DELIM, "9");
  262. ds.initialize(new Configuration(), dsp);
  263. String row = client.fetchOne();
  264. Object o = ds.deserialize(new BytesWritable(row.getBytes()));
  265. assertEquals(o.getClass().toString(), "class java.util.ArrayList");
  266. List<?> lst = (List<?>) o;
  267. assertEquals(lst.get(0), 238);
  268. // TODO: serde doesn't like underscore -- struct result { string _c0}
  269. sql = "select count(1) as c from " + tableName;
  270. client.execute(sql);
  271. row = client.fetchOne();
  272. serDDL = new String("struct result { ");
  273. schema = client.getThriftSchema().getFieldSchemas();
  274. for (int pos = 0; pos < schema.size(); pos++) {
  275. if (pos != 0) {
  276. serDDL = serDDL.concat(",");
  277. }
  278. serDDL = serDDL.concat(schema.get(pos).getType());
  279. serDDL = serDDL.concat(" ");
  280. serDDL = serDDL.concat(schema.get(pos).getName());
  281. }
  282. serDDL = serDDL.concat("}");
  283. dsp.setProperty(Constants.SERIALIZATION_DDL, serDDL);
  284. // Need a new DynamicSerDe instance - re-initialization is not supported.
  285. ds = new DynamicSerDe();
  286. ds.initialize(new Configuration(), dsp);
  287. o = ds.deserialize(new BytesWritable(row.getBytes()));
  288. }
  289. }