使用shell并使用Java API为HBase中的列族设置TTL

时间:2015-03-29 16:17:46

标签: java apache hbase

我是HBase的新手,我在最后搜索但我无法找到一种简单直接的方法来在HBase的列族中设置TTL属性。请指定使用shell和使用Java API的两种方式。

2 个答案:

答案 0 :(得分:15)

使用Java API:

HColumnDescriptor cfDescriptor = new HColumnDescriptor(Bytes.toBytes("cfName"));
cfDescriptor.setTimeToLive(20); // in seconds

tableDesc.addFamily(cfDescriptor);
admin.createTable(tableDesc);

使用shell:

alter ‘tableName′, NAME => ‘cfname′, TTL => 20

答案 1 :(得分:0)

修改现有表以使用Java API添加TTL:

HConnection connection = HBaseConnection.createConnection("localhost", "2181");
            HBaseAdmin hBaseAdmin = new HBaseAdmin(connection);
            HTableDescriptor hTableDescriptor = new HTableDescriptor("TTLDemo".getBytes());
            HColumnDescriptor hColumnDescriptor = new HColumnDescriptor("C".getBytes());
            hColumnDescriptor.setTimeToLive(2);
            hTableDescriptor.addFamily(hColumnDescriptor);
            hBaseAdmin.modifyTable("TTLDemo", hTableDescriptor);
相关问题