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

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

相关推荐
juniperhan13 小时前
Flink 系列第4篇:Flink 时间系统与 Timer 定时器实战精讲
java·大数据·数据仓库·flink
juniperhan16 小时前
link 系列第7篇:Flink 状态管理全解析(原理+类型+存储+实操)
大数据·数据仓库·flink
juniperhan17 小时前
Flink 系列第6篇:Watermark 水印全解析(原理+实操+避坑)
大数据·数据仓库·flink
武子康1 天前
大数据-264 实时数仓-MySQL Binlog配置详解:从原理到实践|数据恢复与主从复制实战
大数据·hadoop·后端
武子康1 天前
大数据-265 实时数仓-Canal MySQL Binlog配置详解:从原理到实践|数据恢复与主从复制实战
大数据·hadoop·后端
晓纪同学1 天前
WPF-03 第一个WPF程序
大数据·hadoop·wpf
2501_933329552 天前
技术深度剖析:Infoseek 字节探索舆情处置系统的全链路架构与核心实现
大数据·数据仓库·人工智能·自然语言处理·架构
xiaoyaohou112 天前
024、大数据技术栈概览:Hadoop、Spark与Flink
大数据·hadoop·spark
虚幻如影2 天前
Hive 中“STRING类型无需显式指定长度
数据仓库·hive·hadoop
荒川之神2 天前
Oracle 数据仓库雪花模型设计(完整实战方案)
数据库·数据仓库·oracle