Truncate HBase table with Java in Hadoop 2.7.1
Find the below code snippet to truncate HBase table in Java code,
Configuration config = HBaseConfiguration.create();// Add custom config parameters hereConnection connection = ConnectionFactory.createConnection(config);Admin admin = connection.getAdmin() admin.truncateTable(TableName.valueOf("bigtable");admin.close();
Hope this helps!.
Succeeded, Here is the solution:
Admin admin = null; Configuration config = HBaseConfiguration.create(); // Add custom config parameters here Connection connection = ConnectionFactory.createConnection(config); try { admin = connection.getAdmin(); for (String tableName : tableNames) { System.out.print("Truncate table " + tableName); try { if (HBaseHelper.isTableExists(tableName)) { if (admin.isTableDisabled(TableName.valueOf(tableName))) { System.out.print("Table " + tableName + " was disabled. Enabling it..."); admin.enableTable(TableName.valueOf(tableName ); } HBaseHelper.truncateTable(tableName); } } catch (IOException e) { System.out.print("Failed to truncate table " + tableName + "\nError Msg: " + e.getMessage()); } } } catch (Exception e){ System.out.print("Could not connect to HBase Admin. Error Msg: " + e.getMessage()); }