TTL缩写Time To Live,即生存期。TTL是作用于列族的,它设置了一个基于时间戳的临界值, 内部的管理会自动检查TTL值是否达到上限,在major合并过程中时间戳被判定为超过TTL的数据会被自动删除。
TTL参数的单位是秒,默认值是Integer.MAX_VALUE,即2^31-1=2 147 483 647 秒,大约68年。使用TTL默认值的数据可以理解为永久保存。
创建t_task_log表,列族f, TTL设置86400秒过期
create 't_task_log',{NAME => 'f', TTL=>'86400'}
查看设置,可以看到 TTL => '86400 SECONDS (1 DAY)'
hbase(main):004:0> desc 't_task_log'
Table t_task_log is ENABLED
t_task_log
COLUMN FAMILIES DESCRIPTION
{NAME => 'f', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => '86400 SECONDS (1 DAY)
', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}
1 row(s) in 0.0370 seconds
(1)查看表结构
看到TTL => 'FOREVER',FOREVER永不过期
hbase(main):003:0> desc "t_task_log"
Table t_task_log is ENABLED
t_task_log
COLUMN FAMILIES DESCRIPTION
{NAME => 'data', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSIO
N => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}
1 row(s) in 0.0580 seconds
(2)设置1天后过期
hbase(main):004:0> disable "t_task_log" #禁用表
0 row(s) in 2.3380 seconds
hbase(main):005:0> alter "t_task_log",NAME=>'data',TTL=>'86400' #设置TTL值,作用于列族data
Updating all regions with the new schema...
1/1 regions updated.
Done.
0 row(s) in 1.9510 seconds
hbase(main):006:0> enable "t_task_log" #恢复表
0 row(s) in 1.2640 seconds
(3)再次查看
可以看到TTL => '86400 SECONDS (1 DAY)',86400 秒,即一天后过期,列族data下的数据会被自动删除
hbase(main):007:0> desc "t_task_log"
Table t_task_log is ENABLED
t_task_log
COLUMN FAMILIES DESCRIPTION
{NAME => 'data', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => '86400 SECONDS (1 DAY
)', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}
1 row(s) in 0.0330 seconds