final String TBL_LOCATION = "tbl_location";
final String TBL_PROPERTIES = "tbl_properties";
String tableName = showCreateTbl.getTableName();
Table tbl = db.getTable(tableName, false);
DataOutput outStream = null;
List<String> duplicateProps = new ArrayList<String>();
try {
Path resFile = new Path(showCreateTbl.getResFile());
FileSystem fs = resFile.getFileSystem(conf);
outStream = fs.create(resFile);
if (tbl.isView()) {
String createTab_stmt = "CREATE VIEW " + tableName + " AS " + tbl.getViewExpandedText();
outStream.writeBytes(createTab_stmt.toString());
((FSDataOutputStream) outStream).close();
outStream = null;
return 0;
}
ST createTab_stmt = new ST("CREATE <" + EXTERNAL + "> TABLE " +
tableName + "(\n" +
"<" + LIST_COLUMNS + ">)\n" +
"<" + TBL_COMMENT + ">\n" +
"<" + LIST_PARTITIONS + ">\n" +
"<" + SORT_BUCKET + ">\n" +
"<" + ROW_FORMAT + ">\n" +
"LOCATION\n" +
"<" + TBL_LOCATION + ">\n" +
"TBLPROPERTIES (\n" +
"<" + TBL_PROPERTIES + ">)\n");
// For cases where the table is external
String tbl_external = "";
if (tbl.getTableType() == TableType.EXTERNAL_TABLE) {
duplicateProps.add("EXTERNAL");
tbl_external = "EXTERNAL";
}
// Columns
String tbl_columns = "";
List<FieldSchema> cols = tbl.getCols();
List<String> columns = new ArrayList<String>();
for (FieldSchema col : cols) {
String columnDesc = " " + col.getName() + " " + col.getType();
if (col.getComment() != null) {
columnDesc = columnDesc + " COMMENT '" + escapeHiveCommand(col.getComment()) + "'";
}
columns.add(columnDesc);
}
tbl_columns = StringUtils.join(columns, ", \n");
// Table comment
String tbl_comment = "";
String tabComment = tbl.getProperty("comment");
if (tabComment != null) {
duplicateProps.add("comment");
tbl_comment = "COMMENT '" + escapeHiveCommand(tabComment) + "'";
}
// Partitions
String tbl_partitions = "";
List<FieldSchema> partKeys = tbl.getPartitionKeys();
if (partKeys.size() > 0) {
tbl_partitions += "PARTITIONED BY ( \n";
List<String> partCols = new ArrayList<String>();
for (FieldSchema partKey : partKeys) {
String partColDesc = " " + partKey.getName() + " " + partKey.getType();
if (partKey.getComment() != null) {
partColDesc = partColDesc + " COMMENT '" +
escapeHiveCommand(partKey.getComment()) + "'";
}
partCols.add(partColDesc);
}
tbl_partitions += StringUtils.join(partCols, ", \n");
tbl_partitions += ")";
}
// Clusters (Buckets)
String tbl_sort_bucket = "";
List<String> buckCols = tbl.getBucketCols();
if (buckCols.size() > 0) {
duplicateProps.add("SORTBUCKETCOLSPREFIX");
tbl_sort_bucket += "CLUSTERED BY ( \n ";
tbl_sort_bucket += StringUtils.join(buckCols, ", \n ");
tbl_sort_bucket += ") \n";
List<Order> sortCols = tbl.getSortCols();
if (sortCols.size() > 0) {
tbl_sort_bucket += "SORTED BY ( \n";
// Order
List<String> sortKeys = new ArrayList<String>();
for (Order sortCol : sortCols) {
String sortKeyDesc = " " + sortCol.getCol() + " ";
if (sortCol.getOrder() == BaseSemanticAnalyzer.HIVE_COLUMN_ORDER_ASC) {
sortKeyDesc = sortKeyDesc + "ASC";
}
else if (sortCol.getOrder() == BaseSemanticAnalyzer.HIVE_COLUMN_ORDER_DESC) {
sortKeyDesc = sortKeyDesc + "DESC";
}
sortKeys.add(sortKeyDesc);
}
tbl_sort_bucket += StringUtils.join(sortKeys, ", \n");
tbl_sort_bucket += ") \n";
}
tbl_sort_bucket += "INTO " + tbl.getNumBuckets() + " BUCKETS";
}
// Row format (SerDe)
String tbl_row_format = "";
StorageDescriptor sd = tbl.getTTable().getSd();
SerDeInfo serdeInfo = sd.getSerdeInfo();
tbl_row_format += "ROW FORMAT";
if (tbl.getStorageHandler() == null) {
if (serdeInfo.getParametersSize() > 1) {
// There is a "serialization.format" property by default,
// even with a delimited row format.
// But our result will only cover the following four delimiters.
tbl_row_format += " DELIMITED \n";
Map<String, String> delims = serdeInfo.getParameters();
// Warn:
// If the four delimiters all exist in a CREATE TABLE query,
// this following order needs to be strictly followed,
// or the query will fail with a ParseException.
if (delims.containsKey(serdeConstants.FIELD_DELIM)) {
tbl_row_format += " FIELDS TERMINATED BY '" +
escapeHiveCommand(StringEscapeUtils.escapeJava(delims.get(
serdeConstants.FIELD_DELIM))) + "' \n";
}
if (delims.containsKey(serdeConstants.COLLECTION_DELIM)) {
tbl_row_format += " COLLECTION ITEMS TERMINATED BY '" +
escapeHiveCommand(StringEscapeUtils.escapeJava(delims.get(
serdeConstants.COLLECTION_DELIM))) + "' \n";
}
if (delims.containsKey(serdeConstants.MAPKEY_DELIM)) {
tbl_row_format += " MAP KEYS TERMINATED BY '" +
escapeHiveCommand(StringEscapeUtils.escapeJava(delims.get(
serdeConstants.MAPKEY_DELIM))) + "' \n";
}
if (delims.containsKey(serdeConstants.LINE_DELIM)) {
tbl_row_format += " LINES TERMINATED BY '" +
escapeHiveCommand(StringEscapeUtils.escapeJava(delims.get(
serdeConstants.LINE_DELIM))) + "' \n";
}
}
else {
tbl_row_format += " SERDE \n '" +
escapeHiveCommand(serdeInfo.getSerializationLib()) + "' \n";
}
tbl_row_format += "STORED AS INPUTFORMAT \n '" +
escapeHiveCommand(sd.getInputFormat()) + "' \n";
tbl_row_format += "OUTPUTFORMAT \n '" +
escapeHiveCommand(sd.getOutputFormat()) + "'";
}
else {
duplicateProps.add(org.apache.hadoop.hive.metastore.api.hive_metastoreConstants.META_TABLE_STORAGE);
tbl_row_format += " SERDE \n '" +
escapeHiveCommand(serdeInfo.getSerializationLib()) + "' \n";
tbl_row_format += "STORED BY \n '" + escapeHiveCommand(tbl.getParameters().get(
org.apache.hadoop.hive.metastore.api.hive_metastoreConstants.META_TABLE_STORAGE)) + "' \n";
// SerDe Properties
if (serdeInfo.getParametersSize() > 0) {
tbl_row_format += "WITH SERDEPROPERTIES ( \n";
List<String> serdeCols = new ArrayList<String>();
for (Map.Entry<String, String> entry : serdeInfo.getParameters().entrySet()) {
serdeCols.add(" '" + entry.getKey() + "'='"
+ escapeHiveCommand(StringEscapeUtils.escapeJava(entry.getValue())) + "'");
}
tbl_row_format += StringUtils.join(serdeCols, ", \n");
tbl_row_format += ")";
}
}
String tbl_location = " '" + escapeHiveCommand(sd.getLocation()) + "'";
// Table properties
String tbl_properties = "";
Map<String, String> properties = tbl.getParameters();
if (properties.size() > 0) {
List<String> realProps = new ArrayList<String>();
for (String key : properties.keySet()) {
if (properties.get(key) != null && !duplicateProps.contains(key)) {
realProps.add(" '" + key + "'='" +
escapeHiveCommand(StringEscapeUtils.escapeJava(properties.get(key))) + "'");
}
}
tbl_properties += StringUtils.join(realProps, ", \n");
}
createTab_stmt.add(EXTERNAL, tbl_external);
createTab_stmt.add(LIST_COLUMNS, tbl_columns);
createTab_stmt.add(TBL_COMMENT, tbl_comment);
createTab_stmt.add(LIST_PARTITIONS, tbl_partitions);
createTab_stmt.add(SORT_BUCKET, tbl_sort_bucket);
createTab_stmt.add(ROW_FORMAT, tbl_row_format);
createTab_stmt.add(TBL_LOCATION, tbl_location);
createTab_stmt.add(TBL_PROPERTIES, tbl_properties);
outStream.writeBytes(createTab_stmt.render());
((FSDataOutputStream) outStream).close();
outStream = null;
} catch (FileNotFoundException e) {
LOG.info("show create table: " + stringifyException(e));
return 1;