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

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

相关推荐
一张假钞1 小时前
Spark SQL读写Hive Table部署
hive·sql·spark
想做富婆2 小时前
Hive:窗口函数[ntile, first_value,row_number() ,rank(),dens_rank()]和自定义函数
数据仓库·hive·hadoop
好记性+烂笔头9 小时前
4 Hadoop 面试真题
大数据·hadoop·面试
B站计算机毕业设计超人1 天前
计算机毕业设计Python+CNN卷积神经网络考研院校推荐系统 考研分数线预测 考研推荐系统 考研爬虫 考研大数据 Hadoop 大数据毕设 机器学习
hadoop·python·机器学习·spark·网络爬虫·课程设计·数据可视化
字节全栈_rJF1 天前
Hive 整合 Spark 全教程 (Hive on Spark)
hive·hadoop·spark
好记性+烂笔头2 天前
2 MapReduce
大数据·hadoop·mapreduce
字节全栈_ZKt2 天前
Hadoop集群中Hbase的介绍、安装、使用_root@master001 hadoop]# start-hbase
大数据·hadoop·hbase
一张假钞2 天前
Sqoop源码修改:增加落地HDFS文件数与MapTask数量一致性检查
java·hadoop·hdfs·sqoop
weixin_307779132 天前
设计转换Apache Hive的HQL语句为Snowflake SQL语句的Python程序方法
数据仓库·hive·python·sql
想做富婆2 天前
Hive:窗口函数(1)
数据仓库·hive·hadoop