Hive sql和Presto sql的一些对比

最近由于工作上和生活上的一些事儿好久没来博客园了,但是写博客的习惯还是得坚持,新的一年需要更加努力,困知勉行,终身学习,每天都保持空杯心态.废话不说,写一些最近使用到的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缓存更加如虎添翼了.

posted @ 2018-01-31 19:30  Syn良子  阅读(21194)  评论(0编辑  收藏  举报