Hive sql常用函数「建议收藏」

Hive sql常用函数「建议收藏」1.获取当前日期 如2020-13-22 current_date() 或者current_date 获取当前时间:from_unixtime(unix_timestamp()) –>返回格式

Hive sql常用函数

1.获取当前日期 如2020-13-22

current_date() 或者
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”];
 
 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
转载请注明出处: https://daima100.com/7934.html

(0)
上一篇 2023-03-11
下一篇 2023-03-11

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注