if (this.tableName == null)
return new ReturnMsg(0, "Syntax error : Please check 'Truncate' syntax.");
try {
HConnection conn = HConnectionManager.getConnection(conf);
HBaseAdmin admin = new HBaseAdmin(conf);
if (!conn.tableExists(tableName)) {
return new ReturnMsg(0, "Table not found.");
}
HTableDescriptor[] tables = conn.listTables();
HColumnDescriptor[] columns = null;
for (int i = 0; i < tables.length; i++) {
if (tables[i].getName().equals(tableName)) {
columns = tables[i].getFamilies().values().toArray(
new HColumnDescriptor[] {});
break;
}
}
println("Truncating a '" + tableName + "' table ... Please wait.");
admin.deleteTable(tableName); // delete the table
HTableDescriptor tableDesc = new HTableDescriptor(tableName.toString());
for (int i = 0; i < columns.length; i++) {
tableDesc.addFamily(columns[i]);
}
admin.createTable(tableDesc); // re-create the table
} catch (IOException e) {
return new ReturnMsg(0, "error msg : " + e.toString());
}
return new ReturnMsg(0, "'" + tableName + "' is successfully truncated.");
}