PageRenderTime 36ms CodeModel.GetById 16ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/release-0.0.0-rc0/hive/external/ql/src/java/org/apache/hadoop/hive/ql/metadata/MetaDataFormatUtils.java

#
Java | 287 lines | 206 code | 50 blank | 31 comment | 20 complexity | 329fb1e9e344a2c5906f5bb438ff6380 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.ql.metadata;
 20
 21import java.util.ArrayList;
 22import java.util.Collections;
 23import java.util.Date;
 24import java.util.List;
 25import java.util.Map;
 26
 27import org.apache.commons.lang.StringEscapeUtils;
 28import org.apache.hadoop.hive.metastore.api.FieldSchema;
 29import org.apache.hadoop.hive.metastore.api.Index;
 30import org.apache.hadoop.hive.metastore.api.StorageDescriptor;
 31import org.apache.hadoop.hive.ql.index.HiveIndex;
 32import org.apache.hadoop.hive.ql.index.HiveIndex.IndexType;
 33import org.apache.hadoop.hive.ql.plan.DescTableDesc;
 34import org.apache.hadoop.hive.ql.plan.ShowIndexesDesc;
 35
 36/**
 37 * This class provides methods to format table and index information.
 38 *
 39 */
 40public final class MetaDataFormatUtils {
 41
 42  public static final String FIELD_DELIM = "\t";
 43  public static final String LINE_DELIM = "\n";
 44
 45  private static final int DEFAULT_STRINGBUILDER_SIZE = 2048;
 46  private static final int ALIGNMENT = 20;
 47
 48  private MetaDataFormatUtils() {
 49  }
 50
 51  public static String getAllColumnsInformation(Table table) {
 52
 53    StringBuilder columnInformation = new StringBuilder(DEFAULT_STRINGBUILDER_SIZE);
 54    formatColumnsHeader(columnInformation);
 55    formatAllFields(columnInformation, table.getCols());
 56
 57    // Partitions
 58    if (table.isPartitioned()) {
 59      columnInformation.append(LINE_DELIM).append("# Partition Information")
 60          .append(LINE_DELIM);
 61      formatColumnsHeader(columnInformation);
 62      formatAllFields(columnInformation, table.getPartCols());
 63    }
 64    return columnInformation.toString();
 65  }
 66
 67  private static void formatColumnsHeader(StringBuilder columnInformation) {
 68    columnInformation.append("# "); // Easy for shell scripts to ignore
 69    formatOutput(getColumnsHeader(), columnInformation);
 70    columnInformation.append(LINE_DELIM);
 71  }
 72
 73  public static String getAllColumnsInformation(List<FieldSchema> cols) {
 74    StringBuilder columnInformation = new StringBuilder(DEFAULT_STRINGBUILDER_SIZE);
 75    formatColumnsHeader(columnInformation);
 76    formatAllFields(columnInformation, cols);
 77    return columnInformation.toString();
 78  }
 79
 80  private static void formatAllFields(StringBuilder tableInfo, List<FieldSchema> cols) {
 81    for (FieldSchema col : cols) {
 82      formatFieldSchemas(tableInfo, col);
 83    }
 84  }
 85
 86
 87  public static String getAllColumnsInformation(Index index) {
 88    StringBuilder indexInfo = new StringBuilder(DEFAULT_STRINGBUILDER_SIZE);
 89
 90    List<String> indexColumns = new ArrayList<String>();
 91
 92    indexColumns.add(index.getIndexName());
 93    indexColumns.add(index.getOrigTableName());
 94
 95    // index key names
 96    List<FieldSchema> indexKeys = index.getSd().getCols();
 97    StringBuilder keyString = new StringBuilder();
 98    boolean first = true;
 99    for (FieldSchema key : indexKeys)
100    {
101      if (!first)
102      {
103        keyString.append(", ");
104      }
105      keyString.append(key.getName());
106      first = false;
107    }
108
109    indexColumns.add(keyString.toString());
110
111    indexColumns.add(index.getIndexTableName());
112
113    // index type
114    String indexHandlerClass = index.getIndexHandlerClass();
115    IndexType indexType = HiveIndex.getIndexTypeByClassName(indexHandlerClass);
116    indexColumns.add(indexType.getName());
117
118    indexColumns.add(index.getParameters().get("comment"));
119
120    formatOutput(indexColumns.toArray(new String[0]), indexInfo);
121
122    return indexInfo.toString();
123}
124
125  /*
126    Displaying columns unformatted for backward compatibility.
127   */
128  public static String displayColsUnformatted(List<FieldSchema> cols) {
129    StringBuilder colBuffer = new StringBuilder(DEFAULT_STRINGBUILDER_SIZE);
130    for (FieldSchema col : cols) {
131      colBuffer.append(col.getName());
132      colBuffer.append(FIELD_DELIM);
133      colBuffer.append(col.getType());
134      colBuffer.append(FIELD_DELIM);
135      colBuffer.append(col.getComment() == null ? "" : col.getComment());
136      colBuffer.append(LINE_DELIM);
137    }
138    return colBuffer.toString();
139  }
140
141  public static String getPartitionInformation(Partition part) {
142    StringBuilder tableInfo = new StringBuilder(DEFAULT_STRINGBUILDER_SIZE);
143
144    // Table Metadata
145    tableInfo.append(LINE_DELIM).append("# Detailed Partition Information").append(LINE_DELIM);
146    getPartitionMetaDataInformation(tableInfo, part);
147
148    // Storage information.
149    tableInfo.append(LINE_DELIM).append("# Storage Information").append(LINE_DELIM);
150    getStorageDescriptorInfo(tableInfo, part.getTPartition().getSd());
151
152    return tableInfo.toString();
153  }
154
155  public static String getTableInformation(Table table) {
156    StringBuilder tableInfo = new StringBuilder(DEFAULT_STRINGBUILDER_SIZE);
157
158    // Table Metadata
159    tableInfo.append(LINE_DELIM).append("# Detailed Table Information").append(LINE_DELIM);
160    getTableMetaDataInformation(tableInfo, table);
161
162    // Storage information.
163    tableInfo.append(LINE_DELIM).append("# Storage Information").append(LINE_DELIM);
164    getStorageDescriptorInfo(tableInfo, table.getTTable().getSd());
165
166    if (table.isView()) {
167      tableInfo.append(LINE_DELIM).append("# View Information").append(LINE_DELIM);
168      getViewInfo(tableInfo, table);
169    }
170
171    return tableInfo.toString();
172  }
173
174  private static void getViewInfo(StringBuilder tableInfo, Table tbl) {
175    formatOutput("View Original Text:", tbl.getViewOriginalText(), tableInfo);
176    formatOutput("View Expanded Text:", tbl.getViewExpandedText(), tableInfo);
177  }
178
179  private static void getStorageDescriptorInfo(StringBuilder tableInfo,
180                                               StorageDescriptor storageDesc) {
181
182    formatOutput("SerDe Library:", storageDesc.getSerdeInfo().getSerializationLib(), tableInfo);
183    formatOutput("InputFormat:", storageDesc.getInputFormat(), tableInfo);
184    formatOutput("OutputFormat:", storageDesc.getOutputFormat(), tableInfo);
185    formatOutput("Compressed:", storageDesc.isCompressed() ? "Yes" : "No", tableInfo);
186    formatOutput("Num Buckets:", String.valueOf(storageDesc.getNumBuckets()), tableInfo);
187    formatOutput("Bucket Columns:", storageDesc.getBucketCols().toString(), tableInfo);
188    formatOutput("Sort Columns:", storageDesc.getSortCols().toString(), tableInfo);
189
190    if (storageDesc.getSerdeInfo().getParametersSize() > 0) {
191      tableInfo.append("Storage Desc Params:").append(LINE_DELIM);
192      displayAllParameters(storageDesc.getSerdeInfo().getParameters(), tableInfo);
193    }
194  }
195
196  private static void getTableMetaDataInformation(StringBuilder tableInfo, Table  tbl) {
197    formatOutput("Database:", tbl.getDbName(), tableInfo);
198    formatOutput("Owner:", tbl.getOwner(), tableInfo);
199    formatOutput("CreateTime:", formatDate(tbl.getTTable().getCreateTime()), tableInfo);
200    formatOutput("LastAccessTime:", formatDate(tbl.getTTable().getLastAccessTime()), tableInfo);
201    String protectMode = tbl.getProtectMode().toString();
202    formatOutput("Protect Mode:", protectMode == null ? "None" : protectMode, tableInfo);
203    formatOutput("Retention:", Integer.toString(tbl.getRetention()), tableInfo);
204    if (!tbl.isView()) {
205      formatOutput("Location:", tbl.getDataLocation().toString(), tableInfo);
206    }
207    formatOutput("Table Type:", tbl.getTableType().name(), tableInfo);
208
209    if (tbl.getParameters().size() > 0) {
210      tableInfo.append("Table Parameters:").append(LINE_DELIM);
211      displayAllParameters(tbl.getParameters(), tableInfo);
212    }
213  }
214
215  private static void getPartitionMetaDataInformation(StringBuilder tableInfo, Partition part) {
216    formatOutput("Partition Value:", part.getValues().toString(), tableInfo);
217    formatOutput("Database:", part.getTPartition().getDbName(), tableInfo);
218    formatOutput("Table:", part.getTable().getTableName(), tableInfo);
219    formatOutput("CreateTime:", formatDate(part.getTPartition().getCreateTime()), tableInfo);
220    formatOutput("LastAccessTime:", formatDate(part.getTPartition().getLastAccessTime()),
221        tableInfo);
222    String protectMode = part.getProtectMode().toString();
223    formatOutput("Protect Mode:", protectMode == null ? "None" : protectMode, tableInfo);
224    formatOutput("Location:", part.getLocation(), tableInfo);
225
226    if (part.getTPartition().getParameters().size() > 0) {
227      tableInfo.append("Partition Parameters:").append(LINE_DELIM);
228      displayAllParameters(part.getTPartition().getParameters(), tableInfo);
229    }
230  }
231
232  private static void displayAllParameters(Map<String, String> params, StringBuilder tableInfo) {
233    List<String> keys = new ArrayList<String>(params.keySet());
234    Collections.sort(keys);
235    for (String key : keys) {
236      tableInfo.append(FIELD_DELIM); // Ensures all params are indented.
237      formatOutput(key, StringEscapeUtils.escapeJava(params.get(key)), tableInfo);
238    }
239  }
240
241  private static void formatFieldSchemas(StringBuilder tableInfo, FieldSchema col) {
242    String comment = col.getComment() != null ? col.getComment() : "None";
243    formatOutput(col.getName(), col.getType(), comment, tableInfo);
244  }
245
246  private static String formatDate(long timeInSeconds) {
247    if (timeInSeconds != 0) {
248      Date date = new Date(timeInSeconds * 1000);
249      return date.toString();
250    }
251    return "UNKNOWN";
252  }
253
254  private static void formatOutput(String[] fields, StringBuilder tableInfo) {
255    for (String field : fields) {
256      if (field == null) {
257        tableInfo.append(FIELD_DELIM);
258        continue;
259      }
260      tableInfo.append(String.format("%-" + ALIGNMENT + "s", field)).append(FIELD_DELIM);
261    }
262    tableInfo.append(LINE_DELIM);
263  }
264
265  private static void formatOutput(String name, String value,
266                                   StringBuilder tableInfo) {
267    tableInfo.append(String.format("%-" + ALIGNMENT + "s", name)).append(FIELD_DELIM);
268    tableInfo.append(String.format("%-" + ALIGNMENT + "s", value)).append(LINE_DELIM);
269  }
270
271  private static void formatOutput(String col1, String col2, String col3,
272                                   StringBuilder tableInfo) {
273    tableInfo.append(String.format("%-" + ALIGNMENT + "s", col1)).append(FIELD_DELIM);
274    tableInfo.append(String.format("%-" + ALIGNMENT + "s", col2)).append(FIELD_DELIM);
275    tableInfo.append(String.format("%-" + ALIGNMENT + "s", col3)).append(LINE_DELIM);
276  }
277
278  public static String[] getColumnsHeader() {
279    return DescTableDesc.getSchema().split("#")[0].split(",");
280  }
281
282  public static String getIndexColumnsHeader() {
283    StringBuilder indexCols = new StringBuilder(DEFAULT_STRINGBUILDER_SIZE);
284    formatOutput(ShowIndexesDesc.getSchema().split("#")[0].split(","), indexCols);
285    return indexCols.toString();
286  }
287}