PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/apache/hive
Java | 132 lines | 82 code | 24 blank | 26 comment | 3 complexity | 9a83ff2290febeeee6fe0ee25c2887aa 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.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.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. /**
  34. * @deprecated Use/modify {@link org.apache.hive.hcatalog.mapreduce.TestHCatHiveCompatibility} instead
  35. */
  36. public class TestHCatHiveCompatibility extends HCatBaseTest {
  37. private static final String INPUT_FILE_NAME = TEST_DATA_DIR + "/input.data";
  38. @BeforeClass
  39. public static void createInputData() throws Exception {
  40. int LOOP_SIZE = 11;
  41. File file = new File(INPUT_FILE_NAME);
  42. file.deleteOnExit();
  43. FileWriter writer = new FileWriter(file);
  44. for (int i = 0; i < LOOP_SIZE; i++) {
  45. writer.write(i + "\t1\n");
  46. }
  47. writer.close();
  48. }
  49. @Test
  50. public void testUnpartedReadWrite() throws Exception {
  51. driver.run("drop table if exists junit_unparted_noisd");
  52. String createTable = "create table junit_unparted_noisd(a int) stored as RCFILE";
  53. Assert.assertEquals(0, driver.run(createTable).getResponseCode());
  54. // assert that the table created has no hcat instrumentation, and that we're still able to read it.
  55. Table table = client.getTable("default", "junit_unparted_noisd");
  56. Assert.assertTrue(table.getSd().getInputFormat().equals(HCatConstants.HIVE_RCFILE_IF_CLASS));
  57. PigServer server = new PigServer(ExecType.LOCAL);
  58. logAndRegister(server, "A = load '" + INPUT_FILE_NAME + "' as (a:int);");
  59. logAndRegister(server, "store A into 'default.junit_unparted_noisd' using org.apache.hcatalog.pig.HCatStorer();");
  60. logAndRegister(server, "B = load 'default.junit_unparted_noisd' using org.apache.hcatalog.pig.HCatLoader();");
  61. Iterator<Tuple> itr = server.openIterator("B");
  62. int i = 0;
  63. while (itr.hasNext()) {
  64. Tuple t = itr.next();
  65. Assert.assertEquals(1, t.size());
  66. Assert.assertEquals(t.get(0), i);
  67. i++;
  68. }
  69. Assert.assertFalse(itr.hasNext());
  70. Assert.assertEquals(11, i);
  71. // assert that the table created still has no hcat instrumentation
  72. Table table2 = client.getTable("default", "junit_unparted_noisd");
  73. Assert.assertTrue(table2.getSd().getInputFormat().equals(HCatConstants.HIVE_RCFILE_IF_CLASS));
  74. driver.run("drop table junit_unparted_noisd");
  75. }
  76. @Test
  77. public void testPartedRead() throws Exception {
  78. driver.run("drop table if exists junit_parted_noisd");
  79. String createTable = "create table junit_parted_noisd(a int) partitioned by (b string) stored as RCFILE";
  80. Assert.assertEquals(0, driver.run(createTable).getResponseCode());
  81. // assert that the table created has no hcat instrumentation, and that we're still able to read it.
  82. Table table = client.getTable("default", "junit_parted_noisd");
  83. Assert.assertTrue(table.getSd().getInputFormat().equals(HCatConstants.HIVE_RCFILE_IF_CLASS));
  84. PigServer server = new PigServer(ExecType.LOCAL);
  85. logAndRegister(server, "A = load '" + INPUT_FILE_NAME + "' as (a:int);");
  86. logAndRegister(server, "store A into 'default.junit_parted_noisd' using org.apache.hcatalog.pig.HCatStorer('b=42');");
  87. logAndRegister(server, "B = load 'default.junit_parted_noisd' using org.apache.hcatalog.pig.HCatLoader();");
  88. Iterator<Tuple> itr = server.openIterator("B");
  89. int i = 0;
  90. while (itr.hasNext()) {
  91. Tuple t = itr.next();
  92. Assert.assertEquals(2, t.size()); // Contains explicit field "a" and partition "b".
  93. Assert.assertEquals(t.get(0), i);
  94. Assert.assertEquals(t.get(1), "42");
  95. i++;
  96. }
  97. Assert.assertFalse(itr.hasNext());
  98. Assert.assertEquals(11, i);
  99. // assert that the table created still has no hcat instrumentation
  100. Table table2 = client.getTable("default", "junit_parted_noisd");
  101. Assert.assertTrue(table2.getSd().getInputFormat().equals(HCatConstants.HIVE_RCFILE_IF_CLASS));
  102. // assert that there is one partition present, and it had hcat instrumentation inserted when it was created.
  103. Partition ptn = client.getPartition("default", "junit_parted_noisd", Arrays.asList("42"));
  104. Assert.assertNotNull(ptn);
  105. Assert.assertTrue(ptn.getSd().getInputFormat().equals(HCatConstants.HIVE_RCFILE_IF_CLASS));
  106. driver.run("drop table junit_unparted_noisd");
  107. }
  108. }