hadoop2.7.1中用java截短hbase表

yhxst69z  于 2021-06-02  发布在  Hadoop
关注(0)|答案(2)|浏览(383)

我试图用简单的java代码截断hbase表:

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

但是我得到了“java.lang.reflect.invocationtargetexception”。我看到hbaseadmin构造函数被弃用了。
它与hadoop2.2.0配合得很好

lvmkulzt

lvmkulzt1#

找到下面的代码片段,在java代码中截断hbase表,

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

希望这有帮助!。

uyto3xhc

uyto3xhc2#

如果成功,解决方案如下:

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

相关问题