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

/tags/release-0.1-rc2/hive/external/serde/src/test/org/apache/hadoop/hive/serde2/lazy/TestLazySimpleSerDe.java

#
Java | 192 lines | 118 code | 25 blank | 49 comment | 3 complexity | fae5dbec67ec125eae1f33c4c887534f 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.serde2.lazy;
  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.hive.serde.Constants;
  24. import org.apache.hadoop.hive.serde2.SerDeException;
  25. import org.apache.hadoop.hive.serde2.io.ByteWritable;
  26. import org.apache.hadoop.hive.serde2.io.DoubleWritable;
  27. import org.apache.hadoop.hive.serde2.io.ShortWritable;
  28. import org.apache.hadoop.hive.serde2.objectinspector.StructField;
  29. import org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector;
  30. import org.apache.hadoop.io.IntWritable;
  31. import org.apache.hadoop.io.LongWritable;
  32. import org.apache.hadoop.io.Text;
  33. /**
  34. * TestLazySimpleSerDe.
  35. *
  36. */
  37. public class TestLazySimpleSerDe extends TestCase {
  38. /**
  39. * Test the LazySimpleSerDe class.
  40. */
  41. public void testLazySimpleSerDe() throws Throwable {
  42. try {
  43. // Create the SerDe
  44. LazySimpleSerDe serDe = new LazySimpleSerDe();
  45. Configuration conf = new Configuration();
  46. Properties tbl = createProperties();
  47. serDe.initialize(conf, tbl);
  48. // Data
  49. Text t = new Text("123\t456\t789\t1000\t5.3\thive and hadoop\t1.\tNULL");
  50. String s = "123\t456\t789\t1000\t5.3\thive and hadoop\tNULL\tNULL";
  51. Object[] expectedFieldsData = {new ByteWritable((byte) 123),
  52. new ShortWritable((short) 456), new IntWritable(789),
  53. new LongWritable(1000), new DoubleWritable(5.3),
  54. new Text("hive and hadoop"), null, null};
  55. // Test
  56. deserializeAndSerialize(serDe, t, s, expectedFieldsData);
  57. } catch (Throwable e) {
  58. e.printStackTrace();
  59. throw e;
  60. }
  61. }
  62. private void deserializeAndSerialize(LazySimpleSerDe serDe, Text t, String s,
  63. Object[] expectedFieldsData) throws SerDeException {
  64. // Get the row structure
  65. StructObjectInspector oi = (StructObjectInspector) serDe
  66. .getObjectInspector();
  67. List<? extends StructField> fieldRefs = oi.getAllStructFieldRefs();
  68. assertEquals(8, fieldRefs.size());
  69. // Deserialize
  70. Object row = serDe.deserialize(t);
  71. for (int i = 0; i < fieldRefs.size(); i++) {
  72. Object fieldData = oi.getStructFieldData(row, fieldRefs.get(i));
  73. if (fieldData != null) {
  74. fieldData = ((LazyPrimitive) fieldData).getWritableObject();
  75. }
  76. assertEquals("Field " + i, expectedFieldsData[i], fieldData);
  77. }
  78. // Serialize
  79. assertEquals(Text.class, serDe.getSerializedClass());
  80. Text serializedText = (Text) serDe.serialize(row, oi);
  81. assertEquals("Serialized data", s, serializedText.toString());
  82. }
  83. private Properties createProperties() {
  84. Properties tbl = new Properties();
  85. // Set the configuration parameters
  86. tbl.setProperty(Constants.SERIALIZATION_FORMAT, "9");
  87. tbl.setProperty("columns",
  88. "abyte,ashort,aint,along,adouble,astring,anullint,anullstring");
  89. tbl.setProperty("columns.types",
  90. "tinyint:smallint:int:bigint:double:string:int:string");
  91. tbl.setProperty(Constants.SERIALIZATION_NULL_FORMAT, "NULL");
  92. return tbl;
  93. }
  94. /**
  95. * Test the LazySimpleSerDe class with LastColumnTakesRest option.
  96. */
  97. public void testLazySimpleSerDeLastColumnTakesRest() throws Throwable {
  98. try {
  99. // Create the SerDe
  100. LazySimpleSerDe serDe = new LazySimpleSerDe();
  101. Configuration conf = new Configuration();
  102. Properties tbl = createProperties();
  103. tbl.setProperty(Constants.SERIALIZATION_LAST_COLUMN_TAKES_REST, "true");
  104. serDe.initialize(conf, tbl);
  105. // Data
  106. Text t = new Text("123\t456\t789\t1000\t5.3\thive and hadoop\t1.\ta\tb\t");
  107. String s = "123\t456\t789\t1000\t5.3\thive and hadoop\tNULL\ta\tb\t";
  108. Object[] expectedFieldsData = {new ByteWritable((byte) 123),
  109. new ShortWritable((short) 456), new IntWritable(789),
  110. new LongWritable(1000), new DoubleWritable(5.3),
  111. new Text("hive and hadoop"), null, new Text("a\tb\t")};
  112. // Test
  113. deserializeAndSerialize(serDe, t, s, expectedFieldsData);
  114. } catch (Throwable e) {
  115. e.printStackTrace();
  116. throw e;
  117. }
  118. }
  119. /**
  120. * Test the LazySimpleSerDe class with extra columns.
  121. */
  122. public void testLazySimpleSerDeExtraColumns() throws Throwable {
  123. try {
  124. // Create the SerDe
  125. LazySimpleSerDe serDe = new LazySimpleSerDe();
  126. Configuration conf = new Configuration();
  127. Properties tbl = createProperties();
  128. serDe.initialize(conf, tbl);
  129. // Data
  130. Text t = new Text("123\t456\t789\t1000\t5.3\thive and hadoop\t1.\ta\tb\t");
  131. String s = "123\t456\t789\t1000\t5.3\thive and hadoop\tNULL\ta";
  132. Object[] expectedFieldsData = {new ByteWritable((byte) 123),
  133. new ShortWritable((short) 456), new IntWritable(789),
  134. new LongWritable(1000), new DoubleWritable(5.3),
  135. new Text("hive and hadoop"), null, new Text("a")};
  136. // Test
  137. deserializeAndSerialize(serDe, t, s, expectedFieldsData);
  138. } catch (Throwable e) {
  139. e.printStackTrace();
  140. throw e;
  141. }
  142. }
  143. /**
  144. * Test the LazySimpleSerDe class with missing columns.
  145. */
  146. public void testLazySimpleSerDeMissingColumns() throws Throwable {
  147. try {
  148. // Create the SerDe
  149. LazySimpleSerDe serDe = new LazySimpleSerDe();
  150. Configuration conf = new Configuration();
  151. Properties tbl = createProperties();
  152. serDe.initialize(conf, tbl);
  153. // Data
  154. Text t = new Text("123\t456\t789\t1000\t5.3\t");
  155. String s = "123\t456\t789\t1000\t5.3\t\tNULL\tNULL";
  156. Object[] expectedFieldsData = {new ByteWritable((byte) 123),
  157. new ShortWritable((short) 456), new IntWritable(789),
  158. new LongWritable(1000), new DoubleWritable(5.3), new Text(""), null,
  159. null};
  160. // Test
  161. deserializeAndSerialize(serDe, t, s, expectedFieldsData);
  162. } catch (Throwable e) {
  163. e.printStackTrace();
  164. throw e;
  165. }
  166. }
  167. }