HConnection conn = HConnectionManager.getConnection(conf);
if (!conn.tableExists(new Text(this.tableName))) {
return new ReturnMsg(0, "'" + this.tableName + "'" + TABLE_NOT_FOUND);
}
HBaseAdmin admin = new HBaseAdmin(conf);
Set<String> columns = null;
HColumnDescriptor columnDesc = null;
switch (operationType) {
case ADD:
disableTable(admin, tableName);
columns = columnSpecMap.keySet();
for (String c : columns) {
columnDesc = getColumnDescriptor(c, columnSpecMap.get(c));
println("Adding " + c + " to " + tableName + "... Please wait.");
admin.addColumn(new Text(tableName), columnDesc);
}
enableTable(admin, tableName);
break;
case DROP:
disableTable(admin, tableName);
println("Dropping " + column + " from " + tableName + "... Please wait.");
column = appendDelimiter(column);
admin.deleteColumn(new Text(tableName), new Text(column));
enableTable(admin, tableName);
break;
case CHANGE:
disableTable(admin, tableName);
Map.Entry<String, Map<String, Object>> columnEntry = (Map.Entry<String, Map<String, Object>>) columnSpecMap
.entrySet().toArray()[0];
// add the : if there isn't one
Text columnName = new Text(
columnEntry.getKey().endsWith(":") ? columnEntry.getKey()
: columnEntry.getKey() + ":");
// get the table descriptor so we can get the old column descriptor
HTableDescriptor tDesc = getTableDescByName(admin, tableName);
HColumnDescriptor oldColumnDesc = tDesc.families().get(columnName);
// combine the options specified in the shell with the options
// from the exiting descriptor to produce the new descriptor
columnDesc = getColumnDescriptor(columnName.toString(), columnEntry
.getValue(), oldColumnDesc);
// send the changes out to the master
admin.modifyColumn(new Text(tableName), columnName, columnDesc);
enableTable(admin, tableName);
break;
case NOOP:
return new ReturnMsg(0, "Invalid operation type.");