One of the HBase features is that it can delete the rows in the table automatically. This feature reduces lot of time that is required to maintain rows if you are handling sensitive data. In this article, we will check automatically delete HBase row using time to live (TTL) setting.
HBase Time to Live (TTL) Option – Automatically Delete HBase Row
You can set ColumnFamilies a TTL length in seconds, and HBase will automatically delete rows or automatically expires the row once the expiration time is reached. This setting applies to all versions of a row in that table– even the current one. The TTL time encoded in the HBase for the row is specified in UTC.
Automatically Delete HBase Row using Time to Live (TTL) setting
Below example demonstrate how to use time to live (TTL) on HBase tables to automatically delete HBase row or automatically expire HBase row.
- Create TTL_DEMO table with TTL value as 20 seconds. This is simple, you just have to use TTL option with column family name that you want to delete automatically after certain number of seconds.
hbase(main):002:0> create 'ttl_demo', {'NAME' => 'cf','TTL' => 20} 0 row(s) in 2.5280 seconds => Hbase::Table - ttl_demo
- Put data into TTL_DEMO table for testing
hbase(main):003:0> put 'ttl_demo','row123','cf:desc', 'TTL Demo' 0 row(s) in 0.3710 seconds
- Use ‘get’ command to display get row from table
hbase(main):004:0> get 'ttl_demo','row123','cf:desc' COLUMN CELL cf:desc timestamp=1519979478359, value=TTL Demo 1 row(s) in 0.1430 seconds
- If you try to retrieve record after 20 seconds, HBase row will automatically disappear.
hbase(main):005:0> get 'ttl_demo','row123','cf:desc' COLUMN CELL 0 row(s) in 0.0440 seconds hbase(main):006:0>
HBase store files which contains only expired rows are deleted by setting hbase.store.delete.expired.storefile to true. You can also disable storing expired record by setting versions to 0.
You can keep the deleted records by setting VERSIONS number to the column family. Read more on HBase table column versions in my other post:
Related reading:
- Insert data using HBase shell put Command and Examples
- Read HBase Table using HBase shell get Command and Examples