clickhouse中bitmap在用户标签,访客去重生产中使用及clickhouse建表null值数据类型处理
? ? ? ?
????????clickhouse中bitmap的使用對于用戶標簽及用戶,訪客跨維度去重統計是十分合適的選擇,具有更快的查詢效率。
1.bitmap
bitmap建表方式:
create table test.bitmap_test(tenant_id String comment '租戶id',shop_id String comment '門店id',h int comment '小時段',uv AggregateFunction(groupBitmap(),Int64) comment '記錄訪客人數',dt Date comment '日期' )engine =AggregatingMergeTree() partition by (dt) order by (tenant_id,shop_id) settings index_granularity =8192;uv字段就是bitmap的數據類型,里面存儲的元素就是bitmap對象
創建數據源表:
create table test.bitmap_source(tenant_id String comment '租戶id',shop_id String comment '門店id',h int comment '小時段',uv String comment '記錄訪客人數',dt Date comment '日期' )engine =MergeTree() partition by (dt) order by (tenant_id,shop_id) settings index_granularity =8192;將數據源表中的數據寫入bitmap表中:
insert into test.bitmap_testselecttenant_id,shop_id,h,groupBitmapState(metroHash64(uv)),dtfrom test.bitmap_test1group by tenant_id, shop_id, h, dt;? ? ? ? 其中在將數據寫入bitmap表中,使用兩個函數metroHash64()將uv String類型的轉換為int類型,而后在使用groupBitmapState()函數轉換為bitmap對象存入bitmap表中。
? ? ? ? 通常在bitmap函數后加State則構造為bitmap對象數據,對于構造bitmap有兩種方式:
一種是從聚合函數groupBitmapState構造的,另外一種就是使用Array對象進行構建 //構建bitmap函數 SELECT bitmapOrCardinality(bitmapBuild([1,2,3,4,5,6]), bitmapBuild([3,4,5,6,7,8]));求取去重的uv訪客量:
select groupBitmapOr(uv) from test.bitmap_test;groupBitmap函數就是bitmap對象的聚合函數,作用就是去重統計該bitmap字段對象的的數量。
更多的bitmap聚合函數可以通過下面鏈接在官網中查看。
2.常用的位圖函數
? ? ? ? 除了上面所說到的聚合函數外,還用很多常用的位圖操作函數,對于位圖操作函數,可以在下面參考文章和官網資料中有詳細的介紹就不在一一解釋了。
3.bitmap表join求取去重效果
select * from (select * from test.bitmap_test t1 left join (select * from test.bitmap_test where tenant_id='1000000007') t2 on t1.tenant_id=t2.tenant_id);?執行結果:
?對于t2中沒有關聯上的數據,uv直接為空,在使用bitmap聚合函數求取uv的去重人數時候,
select groupBitmapOr(t2.uv) from (select * from test.bitmap_test t1 left join (select * from test.bitmap_test where tenant_id='1000000007') t2 on t1.tenant_id=t2.tenant_id);執行結果為:
該結果和下面語句的執行結果相同
select groupBitmapOr(uv) from test.bitmap_test where tenant_id='1000000007';? ? ? ? ?這表示用bitmap聚合函數求取去重人數的時候字段中有空值并不會造成結果的不準確。
4.clickhouse建表時候null值字段設置
?建表時如果沒有對字段指定可以存儲null值,則在數據時候會對null值數據數值類型補為:0,字符串類型補為:''空字符串;
create table if not exists test(id int,name String ) engine =MergeTree() order by (id);--沒有對字段設置為可支持null值存儲,則保存時候會無法存入null值,對null數據替換解決方案:clickhouse支持null值存儲語法
create table if not exists test(id Nullable(int),name Nullable(String) ) engine =MergeTree() order by (id);--使用nullable語法來實現支持null值官網資料:
1.bitmap建表數據類型指定?
2.bitmap的聚合函數
3.位圖操作函數
參考文章:
1.BitMap使用在clickhouse中
2.參考
總結
以上是生活随笔為你收集整理的clickhouse中bitmap在用户标签,访客去重生产中使用及clickhouse建表null值数据类型处理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 20165220 我期望的师生关系
- 下一篇: SpringBoot整合RabbitMQ