最近由於工作上和生活上的一些事兒好久沒來博客園了,但是寫博客的習慣還是得堅持,新的一年需要更加努力,困知勉行,終身學習,每天都保持空杯心態.廢話不說,寫一些最近使用到的Presto SQL和Hive SQL的體會和對比.
一.JSON處理對比
-
Hive
select get_json_object(json, '$.book');
-
Presto
select json_extract_scalar(json, '$.book');
注意這里Presto中json_extract_scalar返回值是一個string類型,其還有一個函數json_extract是直接返回一個json串,所以使用的時候你得自己知道取的到底是一個什么類型的值.
二.列轉行對比
-
Hive
select student, score from tests lateral view explode(split(scores, ',')) t as score;
-
Presto
select student, score from tests cross json unnest(split(scores, ',') as t (score);
簡單的講就是將scores字段中以逗號隔開的分數列比如
80,90,99,80
這種單列的值轉換成和student列一對多的行的值映射.
三.復雜Grouping對比
-
Hive
select origin_state, origin_zip, sum(package_weight) from shipping group by origin_state,origin_zip with rollup;
-
Presto
select origin_state, origin_zip, sum(package_weight) from shipping group by rollup (origin_state, origin_zip);
用過rollup的都知道,這是從右向左的遞減的多級統計的聚合,等價於(如下為Presto寫法)
select origin_state, origin_zip, sum(package_weight) from shipping group by grouping sets ((origin_state, origin_zip), (origin_state), ());
其他一些語法有細微的差別可以慢慢了解,當然Hive和Presto底層架構不一樣導致Presto比Hive運算速度要快很多,再加上開源的Alluxio緩存更加如虎添翼了.