【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;

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

相关推荐
隐于花海,等待花开3 小时前
40.RAND 函数深度解析
hive·hadoop
2501_9272835818 小时前
荣联汇智助力天津艺虹打造“软硬一体”智慧工厂,全流程自动化引领印刷包装行业数智变革
大数据·运维·数据仓库·人工智能·低代码·自动化
孤雪心殇1 天前
快速上手数仓基础知识
数据仓库·hive·spark
渣渣盟1 天前
数据仓库 vs 数据湖 vs 湖仓一体:架构演进与选型
数据仓库·架构
隐于花海,等待花开1 天前
39.ROUND / FLOOR / CEIL 函数深度解析
hive·hadoop
juniperhan1 天前
Flink 系列第22篇:Flink SQL 参数配置与性能调优指南:从 Checkpoint 到聚合优化
大数据·数据仓库·分布式·sql·flink
juniperhan2 天前
Flink 系列第21篇:Flink SQL 函数与 UDF 全解读:类型推导、开发要点与 Module 扩展
java·大数据·数据仓库·分布式·sql·flink
看海的四叔2 天前
【SQL】SQL-管好你的字符串
大数据·数据库·hive·sql·数据分析·字符串
坚持就完事了2 天前
YARN资源管理器
大数据·linux·hadoop·学习
渣渣盟2 天前
大数据技术栈全景图:从零到一的入门路线(深度实战版)
大数据·hadoop·python·flink·spark