![](/img/trans.png)
[英]HBase: truncate table via Java API enable the table truncated
[英]Is it possible to truncate and enable a HBase table using Java?
有沒有辦法使用Java API截斷並重新啟用htable,類似於在shell中使用以下命令?
truncate 'tablename'
enable 'tablename'
有沒有比迭代每行和使用deleteAll更好的方法?
API中沒有可用的截斷。 但是, HBaseAdmin類具有您需要的所有方法。 看一下 :
1. public void disableTable(String tableName)拋出IOException
2. public void deleteTable(String tableName)拋出IOException
3. public void enableTable(String tableName)拋出IOException
HTH
如果你可以使用Thrift和Python和HappyBase:
import happybase
c = happybase.Connection()
c.disable_table('tablename')
c.delete_table('tablename')
c.create_table(
'tablename', {'cf1': dict() }
)
這創造了一個新的空桌子。
現在可以使用admin
Admin admin = Connection.getAdmin();
admin.truncateTable(TableName.valueOf("name_of_the_table",true);
我正在使用版本0.98,它不包含該API,因此這里是一個示例代碼:
public static void main(String[] args) throws Exception{
String table = "yourTableName";
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "hbaseZK host");
HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);
HTableDescriptor desciptor = hBaseAdmin.getTableDescriptor(table.getBytes());
hBaseAdmin.disableTable(table);
hBaseAdmin.deleteTable(table);
hBaseAdmin.createTable(desciptor);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.