Hive中排序和聚集
生活随笔
收集整理的這篇文章主要介紹了
Hive中排序和聚集
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
//五種子句是有嚴格順序的:
where → group by → having → order by → limit
//where和having的區別:
//where是先過濾再分組(對原始數據過濾),where限定聚合函數
hive> select count(*),age from tea where id>18 group by age;
//having是先分組再過濾(對每個組進行過濾,having后只能跟select中已有的列)
hive> select age,count(*) c from tea group by age having c>2;
//group by后面沒有的列,select后面也絕不能有(聚合函數除外)
hive> select ip,sum(load) as c from logs group by ip sort by c desc limit 5;
//distinct關鍵字返回唯一不同的值(返回age和id均不相同的記錄)
hive> select distinct age,id from tea;
//hive只支持Union All,不支持Union
//hive的Union All相對sql有所不同,要求列的數量相同,并且對應的列名也相同,但不要求類的類型相同(可能是存在隱式轉換吧)
select name,age from tea where id<80
union all
select name,age from stu where age>18;
//where和having的區別:
//where是先過濾再分組(對原始數據過濾),where限定聚合函數
hive> select count(*),age from tea where id>18 group by age;
//having是先分組再過濾(對每個組進行過濾,having后只能跟select中已有的列)
hive> select age,count(*) c from tea group by age having c>2;
//group by后面沒有的列,select后面也絕不能有(聚合函數除外)
hive> select ip,sum(load) as c from logs group by ip sort by c desc limit 5;
//distinct關鍵字返回唯一不同的值(返回age和id均不相同的記錄)
hive> select distinct age,id from tea;
//hive只支持Union All,不支持Union
//hive的Union All相對sql有所不同,要求列的數量相同,并且對應的列名也相同,但不要求類的類型相同(可能是存在隱式轉換吧)
select name,age from tea where id<80
union all
select name,age from stu where age>18;
Order?By特性:
- 對數據進行全局排序,只有一個reducer?task,效率低下。
- 與mysql中?order?by區別在于:在?strict?模式下,必須指定?limit,否則執行會報錯
- 對于分區表,還必須顯示指定分區字段查詢
?Sort?BY特性:
- 可以有多個Reduce?Task(以DISTRIBUTE?BY后字段的個數為準)。也可以手工指定:set?mapred.reduce.tasks=4;
- 每個Reduce?Task?內部數據有序,但全局無序?
?上述查詢語句,將結果保存在本地磁盤?/root/hive/b?,此目錄下產生2個結果文件:000000_0?+?000001_0?。每個文件中依據te字段排序。?
Distribute?by特性:
- 按照指定的字段對數據進行劃分到不同的輸出 reduce?文件中
- distribute?by相當于MR?中的paritioner,默認是基于hash?實現的
- distribute?by通常與Sort by連用
Cluster?By特性:
- 如果?Sort?By?和?Distribute?By?中所有的列相同,可以縮寫為Cluster?By以便同時指定兩者所使用的列。
- 注意被cluster by指定的列只能是降序,不能指定asc和desc。一般用于桶表
?
轉載于:https://www.cnblogs.com/skyl/p/4736477.html
總結
以上是生活随笔為你收集整理的Hive中排序和聚集的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Coreseek-带中文分词的Sphin
- 下一篇: java Socket 长连接 心跳包