本文主要是介绍hive的内置函数unix_timestamp 、case when 、cast记录,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
记录一下hive的几个小的内内置函数。
新建个默认表dual
oracle数据库中有个默认表是dual,但是hive里面没有,为了方便测试我们需要的一些函数,创建一个dual方便测试。
参考链接:hive中构建dual虚表
准备个需要加载的数据
echo 'X' > dual.txt
创建dual表
create table dual(
temp string
);
加载数据
load data local inpath '/opt/cdhmoduels/data/dual.txt' into table dual;
unix_timestamp
时间转换函数,可以把毫秒数转成我们需要的日期格式,也可以把指定的日期格式转换成毫秒数。
select unix_timestamp("20170520 12:00:00","yyyyMMdd HH:mm:ss") from dual limit 1;
select from_unixtime(1495252800,"yyyyMMdd HH:mm:ss") from dual limit 1;
case when的用法
case when的用法一般两种,这里只列了一种,数据自己准备下就好。
select
case
when userid=='186' then 'this is 186'
when userid=='196' then 'this is 196'
else '不知道干啥'
end as www
from u_data_new limit 5;
cast 类型转换用法
下面的案例是创建表的时候是从源表来创建的,也就是源表是什么类型,新表也是什么类型,但是使用了cast(movieid as string)
就把原来的int类型转成了String类型。
源表类型:
hive (default)> desc u_data;
OK
col_name data_type comment
userid int
movieid int
rating int
unixtime string
Time taken: 0.143 seconds, Fetched: 4 row(s)
创建表的SQL
create table uu_cast as select userid as userid ,cast(movieid as string) as movieid from u_data;
新表类型
hive (default)> desc uu_cast;
OK
col_name data_type comment
userid int
movieid string //这里变成了String类型
Time taken: 0.103 seconds, Fetched: 2 row(s)
hive (default)>
这篇关于hive的内置函数unix_timestamp 、case when 、cast记录的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!