日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Hive中排序和聚集

發布時間:2024/4/14 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Hive中排序和聚集 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
//五種子句是有嚴格順序的: wheregroup byhavingorder 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;

Order?By特性

  • 對數據進行全局排序只有一個reducer?task,效率低下。
  • 與mysql中?order?by區別在于:在?strict?模式下,必須指定?limit,否則執行會報錯
? 使用命令set hive.mapred.mode; 查詢當前模式 ? 使用命令set hive.mapred.mode=strick; 設置當前模式 hive> select * from logs where date='2015-01-02' order by te; FAILED: SemanticException 1:52 In strict mode,if ORDER BY is specified, LIMIT must also be specified. Error encountered near token 'te'
  • 對于分區表,還必須顯示指定分區字段查詢
hive> select * from logs order by te limit 5; FAILED: SemanticException [Error 10041]: No partition predicate found for Alias "logs" Table "logs"

?Sort?BY特性

  • 可以有多個Reduce?Task(以DISTRIBUTE?BY后字段的個數為準)。也可以手工指定:set?mapred.reduce.tasks=4;
  • 每個Reduce?Task?內部數據有序,但全局無序?
set mapred.reduce.tasks = 2; insert overwrite local directory '/root/hive/b'select * from logs sort by te;

?上述查詢語句,將結果保存在本地磁盤?/root/hive/b?,此目錄下產生2個結果文件:000000_0?+?000001_0?。每個文件中依據te字段排序。?

Distribute?by特性:

  • 按照指定的字段對數據進行劃分到不同的輸出 reduce?文件中
  • distribute?by相當于MR?中的paritioner,默認是基于hash?實現的
  • distribute?by通常與Sort by連用
set mapred.reduce.tasks = 2; insert overwrite local directory '/root/hive/b'select * from logsdistribute by datesort by te;

Cluster?By特性:

  • 如果?Sort?By?和?Distribute?By?中所有的列相同,可以縮寫為Cluster?By以便同時指定兩者所使用的列。
  • 注意被cluster by指定的列只能是降序,不能指定asc和desc。一般用于桶表
set mapred.reduce.tasks = 2; insert overwrite local directory '/root/hive/b'select * from logscluster by date;

?

轉載于:https://www.cnblogs.com/skyl/p/4736477.html

總結

以上是生活随笔為你收集整理的Hive中排序和聚集的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。