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

/hcatalog/core/src/test/java/org/apache/hive/hcatalog/data/schema/TestHCatSchemaUtils.java

http://github.com/apache/hive
Java | 118 lines | 77 code | 19 blank | 22 comment | 11 complexity | 19fb6eaace773ae6bddeaabc8c903ce8 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.data.schema;
  20. import java.io.PrintStream;
  21. import org.apache.hadoop.hive.metastore.api.FieldSchema;
  22. import org.apache.hadoop.hive.serde.serdeConstants;
  23. import org.apache.hadoop.hive.serde2.typeinfo.TypeInfo;
  24. import org.apache.hadoop.hive.serde2.typeinfo.TypeInfoUtils;
  25. import org.apache.hive.hcatalog.common.HCatException;
  26. import org.apache.hive.hcatalog.data.schema.HCatFieldSchema.Category;
  27. import org.slf4j.Logger;
  28. import org.slf4j.LoggerFactory;
  29. import static org.junit.Assert.assertEquals;
  30. import org.junit.Test;
  31. /**
  32. * TestHCatSchemaUtils.
  33. */
  34. public class TestHCatSchemaUtils {
  35. private static final Logger LOG = LoggerFactory.getLogger(TestHCatSchemaUtils.class);
  36. @Test
  37. public void testSimpleOperation() throws Exception {
  38. String typeString = "struct<name:string,studentid:int,"
  39. + "contact:struct<phNo:string,email:string>,"
  40. + "currently_registered_courses:array<string>,"
  41. + "current_grades:map<string,string>,"
  42. + "phNos:array<struct<phNo:string,type:string>>,blah:array<int>>";
  43. TypeInfo ti = TypeInfoUtils.getTypeInfoFromTypeString(typeString);
  44. HCatSchema hsch = HCatSchemaUtils.getHCatSchemaFromTypeString(typeString);
  45. LOG.info("Type name : {}", ti.getTypeName());
  46. LOG.info("HCatSchema : {}", hsch);
  47. assertEquals(hsch.size(), 1);
  48. // Looks like HCatFieldSchema.getTypeString() lower-cases its results
  49. assertEquals(ti.getTypeName().toLowerCase(), hsch.get(0).getTypeString());
  50. assertEquals(hsch.get(0).getTypeString(), typeString.toLowerCase());
  51. }
  52. @Test
  53. public void testHCatFieldSchemaConversion() throws Exception {
  54. FieldSchema stringFieldSchema = new FieldSchema("name1", serdeConstants.STRING_TYPE_NAME, "comment1");
  55. HCatFieldSchema stringHCatFieldSchema = HCatSchemaUtils.getHCatFieldSchema(stringFieldSchema);
  56. assertEquals(stringHCatFieldSchema.getName(), "name1");
  57. assertEquals(stringHCatFieldSchema.getCategory(), Category.PRIMITIVE);
  58. assertEquals(stringHCatFieldSchema.getComment(), "comment1");
  59. FieldSchema listFieldSchema = new FieldSchema("name1", "array<tinyint>", "comment1");
  60. HCatFieldSchema listHCatFieldSchema = HCatSchemaUtils.getHCatFieldSchema(listFieldSchema);
  61. assertEquals(listHCatFieldSchema.getName(), "name1");
  62. assertEquals(listHCatFieldSchema.getCategory(), Category.ARRAY);
  63. assertEquals(listHCatFieldSchema.getComment(), "comment1");
  64. FieldSchema mapFieldSchema = new FieldSchema("name1", "map<string,int>", "comment1");
  65. HCatFieldSchema mapHCatFieldSchema = HCatSchemaUtils.getHCatFieldSchema(mapFieldSchema);
  66. assertEquals(mapHCatFieldSchema.getName(), "name1");
  67. assertEquals(mapHCatFieldSchema.getCategory(), Category.MAP);
  68. assertEquals(mapHCatFieldSchema.getComment(), "comment1");
  69. FieldSchema structFieldSchema = new FieldSchema("name1", "struct<s:string,i:tinyint>", "comment1");
  70. HCatFieldSchema structHCatFieldSchema = HCatSchemaUtils.getHCatFieldSchema(structFieldSchema);
  71. assertEquals(structHCatFieldSchema.getName(), "name1");
  72. assertEquals(structHCatFieldSchema.getCategory(), Category.STRUCT);
  73. assertEquals(structHCatFieldSchema.getComment(), "comment1");
  74. }
  75. @SuppressWarnings("unused")
  76. private void pretty_print(PrintStream pout, HCatSchema hsch) throws HCatException {
  77. pretty_print(pout, hsch, "");
  78. }
  79. private void pretty_print(PrintStream pout, HCatSchema hsch, String prefix) throws HCatException {
  80. int i = 0;
  81. for (HCatFieldSchema field : hsch.getFields()) {
  82. pretty_print(pout, field, prefix + "." + (field.getName() == null ? i : field.getName()));
  83. i++;
  84. }
  85. }
  86. private void pretty_print(PrintStream pout, HCatFieldSchema hfsch, String prefix) throws HCatException {
  87. Category tcat = hfsch.getCategory();
  88. if (Category.STRUCT == tcat) {
  89. pretty_print(pout, hfsch.getStructSubSchema(), prefix);
  90. } else if (Category.ARRAY == tcat) {
  91. pretty_print(pout, hfsch.getArrayElementSchema(), prefix);
  92. } else if (Category.MAP == tcat) {
  93. pout.println(prefix + ".mapkey:\t" + hfsch.getMapKeyType().toString());
  94. pretty_print(pout, hfsch.getMapValueSchema(), prefix + ".mapvalue:");
  95. } else {
  96. pout.println(prefix + "\t" + hfsch.getType().toString());
  97. }
  98. }
  99. }