【hive】数据采样

参考https://hadoopsters.com/how-random-sampling-in-hive-works-and-how-to-use-it-7cdb975aa8e2,可以直接查看原文,下面只是对原文进行概括和实际性能测试。

在说数据采样之前,需要先了解下hivesql中几个... by的区别,也是面试中比较容易问的问题。

1)group by:分组。

2)cluster by:cluster by=distribute by+sort by,唯一区别在于cluster by数据分发和排序的列只能是同一个,而distribute by+sort by可以不同。

3)distribute by:仅数据分发,相同的列值会被分发到同一个reducer,不保证reducer中的结果顺序。

4)sort by:局部(reducer)排序,只保证同一个reducer中的数据有序,不保证全局顺序。

5)order by:全局排序,将所有数据拉取到一个reducer中排序。

以上参考:https://cwiki.apache.org/confluence/display/Hive/LanguageManual+SortBy#LanguageManualSortBy-SyntaxofClusterByandDistributeBy

因为分布式环境并不保证每次返回的结果顺序,因此好像直接limit进行随机采样也不是不可以,但是直接limit采样有个非常明显的弊端:采样数据分布不均。

举例来说,select * from tb limit 10,假设key分别为abc,量级分别为5000,3000,2000的数据分布在3个reducer上,则每个reducer为了减少数据IO会先局部limit 10,最终汇总成30条数据的基础上再limit 10,这样抽样的结果数据中,key为abc的数据量级就和原有每个key的总量级不匹配,不能很好的代表整体。所以说limit在分布式环境中只能算是一种伪随机。

1.distribute by + sort by

从上面可以看到造成结果伪随机的原因就是每个reducer中的数据不随机,相同的key数据都在同一个reducer,因此可以通过distribute by + 随机数的方式对数据随机分发,保证了reducer中数据的随机性。

每个reducer内部中,再通过sort by + 随机数的方式对数据局部随机排序,这样就能保证数据完全无序,样本不同key的量级也能代表整体。

order by + 随机数也行,但是分布式环境中理论上没有distribute by 随机数 sort by 随机数这种方式快,后者多个reducer同时处理更好的利用了集群资源。

2.测试

测试数据包含下面三类数据及量级。

房地产;内部楼栋(7820091)

公司企业;公司(4132401)

购物;超市(1371641)

sql 复制代码
create temporary view sample as
select * from poi_data.poi_res where std_tag in ('房地产;内部楼栋', '公司企业;公司', '购物;超市')
distribute by rand() sort by rand() limit 100000;

select std_tag, count(*) from sample1 group by std_tag order by count(*) desc;
sql 复制代码
drop view if exists sample;
create temporary view sample as
select * from poi_data.poi_res where std_tag in ('房地产;内部楼栋', '公司企业;公司', '购物;超市')
limit 100000;

select std_tag, count(*) from sample group by std_tag order by count(*) desc;
sql 复制代码
drop view if exists sample;
create temporary view sample as
select * from poi_data.poi_res where std_tag in ('房地产;内部楼栋', '公司企业;公司', '购物;超市')
order by rand() limit 100000;

select std_tag, count(*) from sample group by std_tag order by count(*) desc;

3.map端数据过滤优化采样

思想就是在map端就过滤一部分数据,减少shuffle的数据量。

eg:

sql 复制代码
drop view if exists sample;
create temporary view sample as
select * from poi_data.poi_res where std_tag in ('房地产;内部楼栋', '公司企业;公司', '购物;超市')
and rand() <= 0.01
distribute by rand() sort by rand() limit 100000;

select std_tag, count(*) from sample group by std_tag order by count(*) desc;

rand()用于生成[0, 1]的随机数,<=0.01的概率为1%,总数据量1300W+,那么理论上到达reducer的数据量有13w+,因此不影响最终的采样结果。

如果像下面这样将阈值设置为rand()<=0.0001,到达reducer的数据量占总数据量的0.1%(约1.3w),虽然最终结果的量级占比正确,但总量级不够采样数量。

sql 复制代码
drop view if exists sample;
create temporary view sample as
select * from poi_data.poi_res where std_tag in ('房地产;内部楼栋', '公司企业;公司', '购物;超市')
and rand() <= 0.001
distribute by rand() sort by rand() limit 100000;

select std_tag, count(*) from sample group by std_tag order by count(*) desc;

因此要注意阈值的合理设置,设置大了优化效果不明显,设置小了影响采样结果。

相关推荐
qqxhb6 分钟前
系统架构设计师备考第68天——大数据处理架构
大数据·hadoop·flink·spark·系统架构·lambda·kappa
yumgpkpm6 小时前
Hadoop大数据平台在中国AI时代的后续发展趋势研究CMP(类Cloudera CDP 7.3 404版华为鲲鹏Kunpeng)
大数据·hive·hadoop·python·zookeeper·oracle·cloudera
凯子坚持 c11 小时前
基于VMware与CentOS 7的Hadoop集群部署全景指南
linux·hadoop·centos
KANGBboy1 天前
ES 总结
hive·elasticsearch
FeelTouch Labs1 天前
数据仓库和数据集市之ODS、CDM、ADS、DWD、DWS
数据仓库
大数据CLUB1 天前
酒店预订数据分析及预测可视化
大数据·hadoop·分布式·数据挖掘·数据分析·spark·mapreduce
TTBIGDATA2 天前
【Ambari开启Kerberos】Step1-KDC服务初始化安装-适合Ubuntu
运维·数据仓库·hadoop·ubuntu·ambari·hdp·bigtop
TTBIGDATA2 天前
【Ambari开启Kerberos】KERBEROS SERVICE CHECK 报错
大数据·运维·hadoop·ambari·cdh·bigtop·ttbigdata
码·蚁2 天前
SpringMVC
数据仓库·hive·hadoop
2021_fc2 天前
StarRocks技术分享
数据仓库