PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

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