PageRenderTime 31ms CodeModel.GetById 14ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/release-0.2.0-rc0/src/java/org/apache/hcatalog/mapreduce/HCatTableInfo.java

#
Java | 274 lines | 114 code | 39 blank | 121 comment | 11 complexity | aa758224b61c7c8a03c6a0c97bc6f4c2 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.hcatalog.mapreduce;
 20
 21import java.io.Serializable;
 22import java.util.List;
 23import java.util.Map;
 24
 25import org.apache.hadoop.hive.metastore.MetaStoreUtils;
 26
 27/**
 28 *
 29 * HCatTableInfo - class to communicate table information to {@link HCatInputFormat}
 30 * and {@link HCatOutputFormat}
 31 *
 32 */
 33public class HCatTableInfo implements Serializable {
 34
 35
 36  private static final long serialVersionUID = 1L;
 37
 38  public enum TableInfoType {
 39    INPUT_INFO,
 40    OUTPUT_INFO
 41  };
 42
 43  private final TableInfoType tableInfoType;
 44
 45  /** The Metadata server uri */
 46  private final String serverUri;
 47
 48  /** If the hcat server is configured to work with hadoop security, this
 49   * variable will hold the principal name of the server - this will be used
 50   * in the authentication to the hcat server using kerberos
 51   */
 52  private final String serverKerberosPrincipal;
 53
 54  /** The db and table names */
 55  private final String dbName;
 56  private final String tableName;
 57
 58  /** The partition filter */
 59  private String filter;
 60
 61  /** The partition predicates to filter on, an arbitrary AND/OR filter, if used to input from*/
 62  private final String partitionPredicates;
 63
 64  /** The information about the partitions matching the specified query */
 65  private JobInfo jobInfo;
 66
 67  /** The partition values to publish to, if used for output*/
 68  private Map<String, String> partitionValues;
 69
 70  /** List of keys for which values were not specified at write setup time, to be infered at write time */
 71  private List<String> dynamicPartitioningKeys;
 72  
 73
 74  /**
 75   * Initializes a new HCatTableInfo instance to be used with {@link HCatInputFormat}
 76   * for reading data from a table.
 77   * @param serverUri the Metadata server uri
 78   * @param serverKerberosPrincipal If the hcat server is configured to
 79   * work with hadoop security, the kerberos principal name of the server - else null
 80   * The principal name should be of the form:
 81   * <servicename>/_HOST@<realm> like "hcat/_HOST@myrealm.com"
 82   * The special string _HOST will be replaced automatically with the correct host name
 83   * @param dbName the db name
 84   * @param tableName the table name
 85   */
 86  public static HCatTableInfo getInputTableInfo(String serverUri,
 87      String serverKerberosPrincipal,
 88      String dbName,
 89          String tableName) {
 90    return new HCatTableInfo(serverUri, serverKerberosPrincipal, dbName, tableName, (String) null);
 91  }
 92
 93  /**
 94   * Initializes a new HCatTableInfo instance to be used with {@link HCatInputFormat}
 95   * for reading data from a table.
 96   * @param serverUri the Metadata server uri
 97   * @param serverKerberosPrincipal If the hcat server is configured to
 98   * work with hadoop security, the kerberos principal name of the server - else null
 99   * The principal name should be of the form:
100   * <servicename>/_HOST@<realm> like "hcat/_HOST@myrealm.com"
101   * The special string _HOST will be replaced automatically with the correct host name
102   * @param dbName the db name
103   * @param tableName the table name
104   * @param filter the partition filter
105   */
106  public static HCatTableInfo getInputTableInfo(String serverUri, String serverKerberosPrincipal, String dbName,
107          String tableName, String filter) {
108    return new HCatTableInfo(serverUri, serverKerberosPrincipal, dbName, tableName, filter);
109  }
110
111  private HCatTableInfo(String serverUri, String serverKerberosPrincipal,
112      String dbName, String tableName, String filter) {
113      this.serverUri = serverUri;
114      this.serverKerberosPrincipal = serverKerberosPrincipal;
115      this.dbName = (dbName == null) ? MetaStoreUtils.DEFAULT_DATABASE_NAME : dbName;
116      this.tableName = tableName;
117      this.partitionPredicates = null;
118      this.partitionValues = null;
119      this.tableInfoType = TableInfoType.INPUT_INFO;
120      this.filter = filter;
121  }
122  /**
123   * Initializes a new HCatTableInfo instance to be used with {@link HCatOutputFormat}
124   * for writing data from a table.
125   * @param serverUri the Metadata server uri
126   * @param serverKerberosPrincipal If the hcat server is configured to
127   * work with hadoop security, the kerberos principal name of the server - else null
128   * The principal name should be of the form:
129   * <servicename>/_HOST@<realm> like "hcat/_HOST@myrealm.com"
130   * The special string _HOST will be replaced automatically with the correct host name
131   * @param dbName the db name
132   * @param tableName the table name
133   * @param partitionValues The partition values to publish to, can be null or empty Map to
134   * indicate write to a unpartitioned table. For partitioned tables, this map should
135   * contain keys for all partition columns with corresponding values.
136   */
137  public static HCatTableInfo getOutputTableInfo(String serverUri,
138          String serverKerberosPrincipal, String dbName, String tableName, Map<String, String> partitionValues){
139      return new HCatTableInfo(serverUri, serverKerberosPrincipal, dbName,
140          tableName, partitionValues);
141  }
142
143  private HCatTableInfo(String serverUri, String serverKerberosPrincipal,
144      String dbName, String tableName, Map<String, String> partitionValues){
145    this.serverUri = serverUri;
146    this.serverKerberosPrincipal = serverKerberosPrincipal;
147    this.dbName = (dbName == null) ? MetaStoreUtils.DEFAULT_DATABASE_NAME : dbName;
148    this.tableName = tableName;
149    this.partitionPredicates = null;
150    this.partitionValues = partitionValues;
151    this.tableInfoType = TableInfoType.OUTPUT_INFO;
152  }
153
154  /**
155   * Gets the value of serverUri
156   * @return the serverUri
157   */
158  public String getServerUri() {
159    return serverUri;
160  }
161
162  /**
163   * Gets the value of dbName
164   * @return the dbName
165   */
166  public String getDatabaseName() {
167    return dbName;
168  }
169
170  /**
171   * Gets the value of tableName
172   * @return the tableName
173   */
174  public String getTableName() {
175    return tableName;
176  }
177
178  /**
179   * Gets the value of partitionPredicates
180   * @return the partitionPredicates
181   */
182  public String getPartitionPredicates() {
183    return partitionPredicates;
184  }
185
186  /**
187   * Gets the value of partitionValues
188   * @return the partitionValues
189   */
190  public Map<String, String> getPartitionValues() {
191    return partitionValues;
192  }
193
194  /**
195   * Gets the value of job info
196   * @return the job info
197   */
198  public JobInfo getJobInfo() {
199    return jobInfo;
200  }
201
202  /**
203   * Sets the value of jobInfo
204   * @param jobInfo the jobInfo to set
205   */
206  public void setJobInfo(JobInfo jobInfo) {
207    this.jobInfo = jobInfo;
208  }
209
210  public TableInfoType getTableType(){
211    return this.tableInfoType;
212  }
213
214  /**
215   * Sets the value of partitionValues
216   * @param partitionValues the partition values to set
217   */
218  void setPartitionValues(Map<String, String>  partitionValues) {
219    this.partitionValues = partitionValues;
220  }
221
222  /**
223   * Gets the value of partition filter
224   * @return the filter string
225   */
226  public String getFilter() {
227    return filter;
228  }
229
230  /**
231   * @return the serverKerberosPrincipal
232   */
233  public String getServerKerberosPrincipal() {
234    return serverKerberosPrincipal;
235  }
236
237  /**
238   * Returns whether or not Dynamic Partitioning is used
239   * @return whether or not dynamic partitioning is currently enabled and used
240   */
241  public boolean isDynamicPartitioningUsed() {
242    return !((dynamicPartitioningKeys == null) || (dynamicPartitioningKeys.isEmpty()));
243  }
244
245  /**
246   * Sets the list of dynamic partitioning keys used for outputting without specifying all the keys
247   * @param dynamicPartitioningKeys
248   */
249  public void setDynamicPartitioningKeys(List<String> dynamicPartitioningKeys) {
250    this.dynamicPartitioningKeys = dynamicPartitioningKeys;
251  }
252  
253  public List<String> getDynamicPartitioningKeys(){
254    return this.dynamicPartitioningKeys;
255  }
256
257
258  @Override
259  public int hashCode() {
260    int result = 17;
261    result = 31*result + (serverUri == null ? 0 : serverUri.hashCode());
262    result = 31*result + (serverKerberosPrincipal == null ? 0 : serverKerberosPrincipal.hashCode());
263    result = 31*result + (dbName == null? 0 : dbName.hashCode());
264    result = 31*result + tableName.hashCode();
265    result = 31*result + (filter == null? 0 : filter.hashCode());
266    result = 31*result + (partitionPredicates == null ? 0 : partitionPredicates.hashCode());
267    result = 31*result + tableInfoType.ordinal();
268    result = 31*result + (partitionValues == null ? 0 : partitionValues.hashCode());
269    result = 31*result + (dynamicPartitioningKeys == null ? 0 : dynamicPartitioningKeys.hashCode());
270    return result;
271  }
272
273}
274