hive随笔3

1.hive函数使用

if函数 if( , , )
if(条件表达式,如果条件成立返回值,如果条件不成立返回值)
select age,if(person_age=‘0’,null,person_age) from student;

case when 函数 case when … end
case a when b then c [when d then e]* [else] end
说明:
如果a=b,那么返回c;
如果a=d,那么返回e;
否则返回f
举例:hive>
select case 100 when 50 then ‘tom’ when 100 then ‘mary’ else ‘tim’ end from lxw1234;
mary

hive>
select case 200 when 50 then ‘tom’ when 100 then ‘mary’ else ‘tim’ end from lxw1234;
tim

条件判断函数:case
case when a then b [when c then d]* [else e] end
返回值:T
说明:
如果a为true,则返回b;
如果c为true,则返回d;
否则返回e
举例:hive>
select case when 1=2 then ‘tom’ when 2=2 then ‘mary’ else ‘tim’ end from lxw1234;
mary

hive>
select case when 1=1 then ‘tom’ when 2=2 then ‘mary’ else ‘tim’ end from lxw1234;
tom

select age,case when person_age=‘20’ then ‘20’ when person_age=‘30’ then ‘30’ else ‘40’;

select age,case when person_age=‘20’ then null else person_age end from student;

日期函数 to_date…
字符串函数 concat,concat_ws
聚合函数 sum,count…
null值判断 is null , is not null

2.实现–if函数
数据样式:
number dt
AS125689 12
DA252133 36
WSA31233 52
处理成:手机号码,qq号码(666666),微信号码(888888)(第一个字段变成其中一个),平均时长
//这种写死的,不对应就会报错,不建议这么写
select if(number=‘AS125689’,‘666666’,‘888888’) as num,avg(dt) from data group by number ;
//正常应该这么写
select if(number=‘AS125689’,‘666666’,‘888888’) as num,avg(dt) from data group by
if(number=‘AS125689’,‘666666’,‘888888’);
//还可以转化成别的字段返回
select if(number=‘AS125689’,‘666666’,city_id) as num,avg(dt) from data group by
if(number=‘AS125689’,‘666666’,city_id);

3.实现–concat_ws函数 //第一个参数数分隔符
select concat_ws(“hello”,’–’,“world”);
输出:–helloworld
select concat_ws(’–’,“hello”,“world”);
输出:hello–world //连起来

将数组拼接成字符串
create table test(
id int,
name array
)

数据:
1 [“zhangsan”,“lisi”]
1 [“wangwu”,“zhaoliu”]
2 [“aa”,“bb”]

开始拼接语句:
select concat_ws(第一个字段是分隔符,第二个字段是数组字段)
select concat_ws(’–’,name) from test;
输出为:
zhangsan–li
wangwu-zhaoliu
aa–bb

4.split

create table test(
name string
)

数据为:
hello world
hi tom

select split(name,’ ') from test; //这里参数是空格,第一个参数为字段,第二个为参数符号

[“hello”,“world”]
[“hi”,“tom”]

5.explode
select explode(split(name,’ ')) from test;
hello
world
hi
tom

6.单词统计sql
select w.word,count(*) from
(select explode(split(name,’ ')) as word from test) w
group by w.word;

输出为:
hello 1
world 1
hi 1
tom 1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值