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

Set TTL for a column family in HBase using shell and using Java API

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

两种方式

使用 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

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

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