hive的高级查询

1、查询操作

group by、 order by、 join 、 distribute by、sort by、 clusrer by、 union all

【底层是通过mapreduce】

2、常见的聚合操作

2.1 count 计数

count(*) 所有值不全为 NULL 时,加1操作

count(1) 不管有没有值,只要有这条记录,值就加1

count(col) col 列里面的值为 null ,值不会加 1,这个列里面的值不为 null,才加1

【例子】

原始数据

name ,adx,tran_id ,cost,ts

select count(*) from t2;//这行不全为null 的时候才会统计,(null 代表什么都没有,‘’还是有值得,他为空)

select count(1) from t2; //只要这行有记录,就会统计他

select count(adx) from t2;//统计的这列要是为null 则不会统计他

//最后三行的第二列是null

分析:

当数据量很大的时候,

count(*)会判断每行数据是否全为空,这样效率低,不要使用,舍弃

count(1)直接统计,效率高,推荐

count(字段),效率中间

2.2 sum 求和 【求某列的值】

sum (可转成数字的值) 返回 bigint

select sum(adx) from t2;

2.3 avg 求平均值【求某列的平均值】

avg(可转成数字的值)返回double

select avg(adx) from t2;

2.4 distinct不同值个数 【distinct 放在字段名前面,把该字段重复的数据去掉】

count(distinct col)

select distinct name from t2;

//过滤掉重复的数据,显示不重复的,空数据也算

select count(distnct name) from t2;

3、ORDER BY

按照某些字段排序

select col1,other...

from table

where conditio

order by col1,col2 [asc|desc]

注意

order by后面可以有多列进行排序,默认按字典排序

order by为全局排序

order by需要reduce操作,且 只有一个reduce ,与配置无关(虽然我们可以通过配置增加reduce的个数,但是没用)。 数据量很大时,慎用

猜你喜欢

转载自www.cnblogs.com/liubao-xiaoqin/p/8984480.html