在 Hadoop 2.7.1 中使用 Java 截断 HBase table

Truncate HBase table with Java in Hadoop 2.7.1

我正在尝试使用简单的 java 代码截断 HBase table:

HBaseAdmin hbaseAdmin = new HBaseAdmin(new HadoopConfig());

但我得到 "java.lang.reflect.InvocationTargetException"。 我看到 HBaseAdmin 构造函数已被弃用。

它适用于 Hadoop 2.2.0

在 Java 代码中找到以下代码片段来截断 HBase table,

Configuration config = HBaseConfiguration.create();
// Add custom config parameters here
Connection connection = ConnectionFactory.createConnection(config);
Admin admin = connection.getAdmin() 
admin.truncateTable(TableName.valueOf("bigtable");
admin.close();

希望对您有所帮助!

成功,解决方法如下:

    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());
    }