BITMAP
description
BITMAP
BITMAP类型的列可以在Aggregate表或Unique表中使用。 在Unique表中使用时,其必须作为非key列使用。 在Aggregate表中使用时,其必须作为非key列使用,且建表时配合的聚合类型为BITMAP_UNION。 用户不需要指定长度和默认值。长度根据数据的聚合程度系统内控制。 并且BITMAP列只能通过配套的bitmap_union_count、bitmap_union、bitmap_hash、bitmap_hash64等函数进行查询或使用。
离线场景下使用BITMAP会影响导入速度,在数据量大的情况下查询速度会慢于HLL,并优于Count Distinct。 注意:实时场景下BITMAP如果不使用全局字典,使用了bitmap_hash()可能会导致有千分之一左右的误差。如果这个误差不可接受,可以使用bitmap_hash64。
example
建表示例如下:
1. create table metric_table (
2. datekey int,
3. hour int,
4. device_id bitmap BITMAP_UNION
5. )
6. aggregate key (datekey, hour)
7. distributed by hash(datekey, hour) buckets 1
8. properties(
9. "replication_num" = "1"
10. );
插入数据示例:
1. insert into metric_table values
2. (20200622, 1, to_bitmap(243)),
3. (20200622, 2, bitmap_from_array([1,2,3,4,5,434543])),
4. (20200622, 3, to_bitmap(287667876573));
查询数据示例:
1. select hour, BITMAP_UNION_COUNT(pv) over(order by hour) uv from(
2. select hour, BITMAP_UNION(device_id) as pv
3. from metric_table -- 查询每小时的累计UV
4. where datekey=20200622
5. group by hour order by 1
6. ) final;
在查询时,BITMAP 可配合return_object_data_as_binary变量进行使用,详情可查看变量章节。
keywords
1. BITMAP
