PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/hcatalog/core/src/test/java/org/apache/hive/hcatalog/mapreduce/TestHCatHiveCompatibility.java

http://github.com/apache/hive
Java | 129 lines | 82 code | 24 blank | 23 comment | 3 complexity | f907244966168b861e3d5c31b4377d0d MD5 | raw file
Possible License(s): Apache-2.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,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package org.apache.hive.hcatalog.mapreduce;
  20. import java.io.File;
  21. import java.io.FileWriter;
  22. import java.util.Arrays;
  23. import java.util.Iterator;
  24. import junit.framework.Assert;
  25. import org.apache.hadoop.hive.metastore.api.Partition;
  26. import org.apache.hadoop.hive.metastore.api.Table;
  27. import org.apache.hive.hcatalog.common.HCatConstants;
  28. import org.apache.pig.ExecType;
  29. import org.apache.pig.PigServer;
  30. import org.apache.pig.data.Tuple;
  31. import org.junit.BeforeClass;
  32. import org.junit.Test;
  33. public class TestHCatHiveCompatibility extends HCatBaseTest {
  34. private static final String INPUT_FILE_NAME = TEST_DATA_DIR + "/input.data";
  35. @BeforeClass
  36. public static void createInputData() throws Exception {
  37. int LOOP_SIZE = 11;
  38. File file = new File(INPUT_FILE_NAME);
  39. file.deleteOnExit();
  40. FileWriter writer = new FileWriter(file);
  41. for (int i = 0; i < LOOP_SIZE; i++) {
  42. writer.write(i + "\t1\n");
  43. }
  44. writer.close();
  45. }
  46. @Test
  47. public void testUnpartedReadWrite() throws Exception {
  48. driver.run("drop table if exists junit_unparted_noisd");
  49. String createTable = "create table junit_unparted_noisd(a int) stored as RCFILE";
  50. Assert.assertEquals(0, driver.run(createTable).getResponseCode());
  51. // assert that the table created has no hcat instrumentation, and that we're still able to read it.
  52. Table table = client.getTable("default", "junit_unparted_noisd");
  53. Assert.assertTrue(table.getSd().getInputFormat().equals(HCatConstants.HIVE_RCFILE_IF_CLASS));
  54. PigServer server = new PigServer(ExecType.LOCAL);
  55. logAndRegister(server, "A = load '" + INPUT_FILE_NAME + "' as (a:int);");
  56. logAndRegister(server, "store A into 'default.junit_unparted_noisd' using org.apache.hive.hcatalog.pig.HCatStorer();");
  57. logAndRegister(server, "B = load 'default.junit_unparted_noisd' using org.apache.hive.hcatalog.pig.HCatLoader();");
  58. Iterator<Tuple> itr = server.openIterator("B");
  59. int i = 0;
  60. while (itr.hasNext()) {
  61. Tuple t = itr.next();
  62. Assert.assertEquals(1, t.size());
  63. Assert.assertEquals(t.get(0), i);
  64. i++;
  65. }
  66. Assert.assertFalse(itr.hasNext());
  67. Assert.assertEquals(11, i);
  68. // assert that the table created still has no hcat instrumentation
  69. Table table2 = client.getTable("default", "junit_unparted_noisd");
  70. Assert.assertTrue(table2.getSd().getInputFormat().equals(HCatConstants.HIVE_RCFILE_IF_CLASS));
  71. driver.run("drop table junit_unparted_noisd");
  72. }
  73. @Test
  74. public void testPartedRead() throws Exception {
  75. driver.run("drop table if exists junit_parted_noisd");
  76. String createTable = "create table junit_parted_noisd(a int) partitioned by (b string) stored as RCFILE";
  77. Assert.assertEquals(0, driver.run(createTable).getResponseCode());
  78. // assert that the table created has no hcat instrumentation, and that we're still able to read it.
  79. Table table = client.getTable("default", "junit_parted_noisd");
  80. Assert.assertTrue(table.getSd().getInputFormat().equals(HCatConstants.HIVE_RCFILE_IF_CLASS));
  81. PigServer server = new PigServer(ExecType.LOCAL);
  82. logAndRegister(server, "A = load '" + INPUT_FILE_NAME + "' as (a:int);");
  83. logAndRegister(server, "store A into 'default.junit_parted_noisd' using org.apache.hive.hcatalog.pig.HCatStorer('b=42');");
  84. logAndRegister(server, "B = load 'default.junit_parted_noisd' using org.apache.hive.hcatalog.pig.HCatLoader();");
  85. Iterator<Tuple> itr = server.openIterator("B");
  86. int i = 0;
  87. while (itr.hasNext()) {
  88. Tuple t = itr.next();
  89. Assert.assertEquals(2, t.size()); // Contains explicit field "a" and partition "b".
  90. Assert.assertEquals(t.get(0), i);
  91. Assert.assertEquals(t.get(1), "42");
  92. i++;
  93. }
  94. Assert.assertFalse(itr.hasNext());
  95. Assert.assertEquals(11, i);
  96. // assert that the table created still has no hcat instrumentation
  97. Table table2 = client.getTable("default", "junit_parted_noisd");
  98. Assert.assertTrue(table2.getSd().getInputFormat().equals(HCatConstants.HIVE_RCFILE_IF_CLASS));
  99. // assert that there is one partition present, and it had hcat instrumentation inserted when it was created.
  100. Partition ptn = client.getPartition("default", "junit_parted_noisd", Arrays.asList("42"));
  101. Assert.assertNotNull(ptn);
  102. Assert.assertTrue(ptn.getSd().getInputFormat().equals(HCatConstants.HIVE_RCFILE_IF_CLASS));
  103. driver.run("drop table junit_unparted_noisd");
  104. }
  105. }