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