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

I am new to HBase and I searched at my end, but I cannot find a simple and direct way to set the TTL attribute in a column family in HBase. Specify both ways to use the shell and use the Java API.

+4
source share
2 answers

Using the Java API:

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

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

And using the shell:

alter ‘tableName′, NAME => ‘cfname′, TTL => 20
+10
source

Modifying an existing table to add TTL using the Java 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);
0
source

All Articles