当前位置:Gxlcms > 数据库问题 > Hive sql常用函数

Hive sql常用函数

时间:2021-07-01 10:21:17 帮助过:34人阅读

或者current_date 获取当前时间from_unixtime(unix_timestamp()) -->返回格式:yyyy-MM-dd HH:mm:ss       :current_timestamp()                  -->返回格式:yyyy-MM-dd HH:mm:ss.xxx

2.时间戳转为日期

from_unixtime(时间戳,string format=yyyy-MM-dd HH:mm:ss)   //实际写出来不需要带string,只是表明格式 --时间戳为10位,H为24小时计数,h为12小时计数 --string format:默认标准格式为 yyyy-MM-dd HH:mm:ss --其他格式写法多种多样 yyyy-MM-dd HH:mm --yyyy-MM-dd HH --yyyy-MM-dd --yyyyMMdd --yyyy/MM/dd

3.日期转为时间戳

unix_timestamp(string date=当前时间)   //实际写出来不需要带string,只是表明格式 --默认为获取当前时间戳:unix_timestamp() --date的格式必须是标准格式:"yyyy-MM-dd HH:mm:ss",如不符合返回null

4.时间间隔计算  --(理解:date difference日期差异)

datediff(string enddate,string startdate) --计算方式为:enddate-startdate --结果为天数

5.保留年月日

to_date("标准时间格式") --结果为如:2020-03-22

6.单独年,月,日

year(format_date) month(format_date) day(format_date) -- format_date格式至少包含年月日 -- 如year("2020-03-22") -- year("2020-03-22 12:23")

7.日期增加函数

date_add(string startdate,intdays) --如:date_add("2020-03-11",10) -->2020-03-21

8.日期减少函数

date_sub(string startdate,intdays) --如:date_add("2020-03-11",10) -->2020-03-01

9.截取字符串

substr(str,pos,len) -- 常用于截取字符串时间 -- pos从1开始算,不是0

10.条件函数:case when

--如:select case when age<20 then "20岁以下" when age>=20 and age<30 then "20~30岁" when age>=30 and age<40 then "30~40岁" else "40岁以上" end as age_type, count (distinct user_id) user_num from user_info group by ...;

11.if函数

if(条件表达式,结果1,结果2) :当条件为真-->结果1,否则结果2 --如:select if (level>5,"高","低") [as level_type] from...

12.对json字符串和map类型的处理

get_json_object(string json_string,string path) string json_string:列名 string path:用$.key取值 --如:字段: extra1(string): {"systemtype":"ios","education":"master","marriage_status":"1","phone brand":"iphone X"} --字段: extra2(map<string,string>): {"systemtype":"ios","education":"master","marriage_status":"1","phone brand":"iphone X"} 对于json类型: 例如: SELECT get_json_object(extra1, ‘$.phonebrand‘) as phone_brand, count(distinct user_id) user_num FROM user_info GROUP BY get_json_object(extra1, ‘$.phonebrand‘);
对于map类型: 例如:select extra2[‘phonebrand‘] as phone_brand, count(distinct user_id) user_num FROM user_info GROUP BY extra2[‘phonebrand‘];    

Hive sql常用函数

标签:日期   inf   mon   extra   _id   else   保留   key   group   

人气教程排行