【presto & hive 对比2】json字符串提取,行转列,位运算

json字符串提取

--hive
select get_json_object(json, '$.book');
--Presto
select json_extract_scalar(json, '$.book');
--注意这里Presto中json_extract_scalar返回值是一个string类型,其还有一个函数json_extract是直接返回一个json串,所以使用的时候你得自己知道取的到底是一个什么类型的值.

列转行

有两个字段, user_id, scores(分别是用户的得分)
示例: yn 98,97,95
如果我们想将其转化为 user_id, score(一个分值),则需要用到列转行,结果是
yn 98
yn 97
yn 95
对应的sql写法为

--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);

位运算

如果想判断某个数字的某一二级制位是否为1, 则需要用到位运算
示例: 判断某一数字的二进制表示的右起第6位是否为1
注意若第6位为1,则结果为64

--hive
select 8 & 64 != 64; -- 0,
select 64 & 64 != 64; --64,
--Presto
select bitwise_and(64,64); --结果: 64
select bitwise_and(2,64); --结果: 0

presto其它的位运算相关函数
bit_count(x, bits) → bigint 返回 x 的补码中置 1 的位数
bitwise_and(x, y) → bigint 位与函数
bitwise_not(x) → bigint 取非操作
bitwise_or(x, y) → bigint 位或函数
bitwise_xor(x, y) → bigint 抑或函数
bitwise_and_agg(x) → bigint 返回 x 中所有值的与操作结果,x 为数组
bitwise_or_agg(x) → bigint 返回 x 中所有值的或操作结果,x 位数组

参考链接: https://www.cnblogs.com/cssdongl/p/8394000.html

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值