PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

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