PageRenderTime 47ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://github.com/apache/hive
Java | 84 lines | 50 code | 13 blank | 21 comment | 11 complexity | f77f0a817931597ad7bb098a6633ab60 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.data.schema;
  20. import java.io.PrintStream;
  21. import junit.framework.TestCase;
  22. import org.apache.hadoop.hive.serde2.typeinfo.TypeInfo;
  23. import org.apache.hadoop.hive.serde2.typeinfo.TypeInfoUtils;
  24. import org.apache.hcatalog.common.HCatException;
  25. import org.apache.hcatalog.data.schema.HCatFieldSchema.Category;
  26. import org.slf4j.Logger;
  27. import org.slf4j.LoggerFactory;
  28. /**
  29. * @deprecated Use/modify {@link org.apache.hive.hcatalog.data.schema.TestHCatSchemaUtils} instead
  30. */
  31. public class TestHCatSchemaUtils extends TestCase {
  32. private static final Logger LOG = LoggerFactory.getLogger(TestHCatSchemaUtils.class);
  33. public void testSimpleOperation() throws Exception {
  34. String typeString = "struct<name:string,studentid:int,"
  35. + "contact:struct<phno:string,email:string>,"
  36. + "currently_registered_courses:array<string>,"
  37. + "current_grades:map<string,string>,"
  38. + "phnos:array<struct<phno:string,type:string>>,blah:array<int>>";
  39. TypeInfo ti = TypeInfoUtils.getTypeInfoFromTypeString(typeString);
  40. HCatSchema hsch = HCatSchemaUtils.getHCatSchemaFromTypeString(typeString);
  41. LOG.info("Type name : {}", ti.getTypeName());
  42. LOG.info("HCatSchema : {}", hsch);
  43. assertEquals(hsch.size(), 1);
  44. assertEquals(ti.getTypeName(), hsch.get(0).getTypeString());
  45. assertEquals(hsch.get(0).getTypeString(), typeString);
  46. }
  47. @SuppressWarnings("unused")
  48. private void pretty_print(PrintStream pout, HCatSchema hsch) throws HCatException {
  49. pretty_print(pout, hsch, "");
  50. }
  51. private void pretty_print(PrintStream pout, HCatSchema hsch, String prefix) throws HCatException {
  52. int i = 0;
  53. for (HCatFieldSchema field : hsch.getFields()) {
  54. pretty_print(pout, field, prefix + "." + (field.getName() == null ? i : field.getName()));
  55. i++;
  56. }
  57. }
  58. private void pretty_print(PrintStream pout, HCatFieldSchema hfsch, String prefix) throws HCatException {
  59. Category tcat = hfsch.getCategory();
  60. if (Category.STRUCT == tcat) {
  61. pretty_print(pout, hfsch.getStructSubSchema(), prefix);
  62. } else if (Category.ARRAY == tcat) {
  63. pretty_print(pout, hfsch.getArrayElementSchema(), prefix);
  64. } else if (Category.MAP == tcat) {
  65. pout.println(prefix + ".mapkey:\t" + hfsch.getMapKeyType().toString());
  66. pretty_print(pout, hfsch.getMapValueSchema(), prefix + ".mapvalue:");
  67. } else {
  68. pout.println(prefix + "\t" + hfsch.getType().toString());
  69. }
  70. }
  71. }