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