PageRenderTime 79ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/release-0.0.0-rc0/hive/external/serde/src/java/org/apache/hadoop/hive/serde2/ColumnProjectionUtils.java

#
Java | 118 lines | 63 code | 15 blank | 40 comment | 17 complexity | 27ba2361d5324919207eab0fe197f719 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;
  19. import java.util.ArrayList;
  20. import org.apache.hadoop.conf.Configuration;
  21. import org.apache.hadoop.util.StringUtils;
  22. /**
  23. * ColumnProjectionUtils.
  24. *
  25. */
  26. public final class ColumnProjectionUtils {
  27. public static final String READ_COLUMN_IDS_CONF_STR = "hive.io.file.readcolumn.ids";
  28. /**
  29. * Sets read columns' ids(start from zero) for RCFile's Reader. Once a column
  30. * is included in the list, RCFile's reader will not skip its value.
  31. *
  32. */
  33. public static void setReadColumnIDs(Configuration conf, ArrayList<Integer> ids) {
  34. String id = toReadColumnIDString(ids);
  35. setReadColumnIDConf(conf, id);
  36. }
  37. /**
  38. * Sets read columns' ids(start from zero) for RCFile's Reader. Once a column
  39. * is included in the list, RCFile's reader will not skip its value.
  40. *
  41. */
  42. public static void appendReadColumnIDs(Configuration conf,
  43. ArrayList<Integer> ids) {
  44. String id = toReadColumnIDString(ids);
  45. if (id != null) {
  46. String old = conf.get(READ_COLUMN_IDS_CONF_STR, null);
  47. String newConfStr = id;
  48. if (old != null) {
  49. newConfStr = newConfStr + StringUtils.COMMA_STR + old;
  50. }
  51. setReadColumnIDConf(conf, newConfStr);
  52. }
  53. }
  54. private static void setReadColumnIDConf(Configuration conf, String id) {
  55. if (id == null || id.length() <= 0) {
  56. conf.set(READ_COLUMN_IDS_CONF_STR, "");
  57. return;
  58. }
  59. conf.set(READ_COLUMN_IDS_CONF_STR, id);
  60. }
  61. private static String toReadColumnIDString(ArrayList<Integer> ids) {
  62. String id = null;
  63. if (ids != null) {
  64. for (int i = 0; i < ids.size(); i++) {
  65. if (i == 0) {
  66. id = "" + ids.get(i);
  67. } else {
  68. id = id + StringUtils.COMMA_STR + ids.get(i);
  69. }
  70. }
  71. }
  72. return id;
  73. }
  74. /**
  75. * Returns an array of column ids(start from zero) which is set in the given
  76. * parameter <tt>conf</tt>.
  77. */
  78. public static ArrayList<Integer> getReadColumnIDs(Configuration conf) {
  79. if (conf == null) {
  80. return new ArrayList<Integer>(0);
  81. }
  82. String skips = conf.get(READ_COLUMN_IDS_CONF_STR, "");
  83. String[] list = StringUtils.split(skips);
  84. ArrayList<Integer> result = new ArrayList<Integer>(list.length);
  85. for (String element : list) {
  86. // it may contain duplicates, remove duplicates
  87. Integer toAdd = Integer.parseInt(element);
  88. if (!result.contains(toAdd)) {
  89. result.add(toAdd);
  90. }
  91. }
  92. return result;
  93. }
  94. /**
  95. * Clears the read column ids set in the conf, and will read all columns.
  96. */
  97. public static void setFullyReadColumns(Configuration conf) {
  98. conf.set(READ_COLUMN_IDS_CONF_STR, "");
  99. }
  100. private ColumnProjectionUtils() {
  101. // prevent instantiation
  102. }
  103. }