1 2 Fork me on GitHub 6

hiveSQL 常用函数总结

  • 查询系统自带的函数
show functions
  • 显示系统自带的函数的用法
desc function count
  • 详细显示自带函数的用法
desc function extended count

日期函数

1. unix时间戳类型转日期:
      - 10位数时间戳
      select from_unixtime(1564581347,'yyyy-MM-dd HH:mm:ss') ;
      - 13位数时间戳
      select from_unixtime(cast(1564581347793/1000 as bigint),'yyyy-MM-dd HH:mm:ss') ;

2、获取当前UNIX时间戳函数: unix_timestamp ***
语法: unix_timestamp()
返回值: bigint
说明: 获得当前时区的UNIX时间戳

hive> select unix_timestamp() from tableName;
- 返回 1323309615


3、指定格式日期转UNIX时间戳函数: unix_timestamp ***
语法: unix_timestamp(string date, string pattern)
返回值: bigint
说明: 转换pattern格式的日期到UNIX时间戳。如果转化失败,则返回0。

hive> select unix_timestamp('20111207 13:01:03','yyyyMMdd HH:mm:ss') from tableName;

- 返回 1323234063

4、日期时间转日期函数: to_date ***
语法: to_date(string timestamp)
返回值: string
说明: 返回日期时间字段中的日期部分。

hive> select to_date('2011-12-08 10:03:01') from tableName;

-返回 2011-12-08

5、获取某个日期对应的是星期几:
说明:datediff中的第一个参数是 要处理的数据,第二个参数默认些‘2012-01-01’即可;返回值中0表示星期日,1表示星期一......

hive> pmod(datediff(to_date("2021-11-26"), "2012-01-01"), 7) 

-返回 0

2. case when 的用法

select 
case 
    when mobile='17310698783'and status in (2,4)
        then 'leon'
    when mobile='1866375062' and status in (2,4)
        then 'alix'
    else '其他'
end,
nickname
from _cloud_vipcourse.order 
WHERE  mobile  in ('17310698783','1866375062')

3.字符串相关函数

1. 字符串连接函数:concat ***
语法: concat(string A, string B…)
返回值: string
说明:返回输入字符串连接后的结果,支持任意个输入字符串

hive> select concat('abc','def','gh') from tableName;

- 返回 abcdefgh


2. 带分隔符字符串连接函数:concat_ws ***
语法: concat_ws(string SEP, string A, string B…)
返回值: string
说明:返回输入字符串连接后的结果,SEP表示各个字符串间的分隔符

hive> select concat_ws('-','union','12345678')from tableName;

- 返回 union-12345678


3. 字符串截取函数:substr,substring ****
语法: substr(string A, int start),substring(string A, int start)
返回值: string
说明:返回字符串A从start位置到结尾的字符串

hive> select substr('abcde',3) from tableName;

- 返回 cde

hive> select substring('abcde',3) from tableName;

- 返回 cde

hive> select substr('abcde',-1) from tableName; (和ORACLE相同)

- 返回e

4. 字符串截取函数:substr,substring ****
语法: substr(string A, int start, int len),substring(string A, int start, int len)
返回值: string
说明:返回字符串A从start位置开始,长度为len的字符串

hive> select substr('abcde',3,2) from tableName;

- 返回 cd

hive> select substring('abcde',3,2) from tableName;

- 返回 cd

hive>select substring('abcde',-2,2) from tableName;

- 返回 de


5. 去空格函数:trim ***
语法: trim(string A)
返回值: string
说明:去除字符串两边的空格

hive> select trim(' abc ') from tableName;

- 返回 abc

6.去空格函数:trim ***
语法: trim(string A)
返回值: string
说明:去除字符串两边的空格

hive> select trim('  abc  ') from tableName;

- 返回 abc

7. json解析函数:get_json_object ****
语法: get_json_object(string json_string, string path)
返回值: string
说明:解析json的字符串json_string,返回path指定的内容。如果输入的json字符串无效,那么返回NULL。

hive> select get_json_object('{"store":{"fruit":\[{"weight":8,"type":"apple"},{"weight":9,"type":"pear"}], "bicycle":{"price":19.95,"color":"red"} },"email":"amy@only_for_json_udf_test.net","owner":"amy"}','$.owner') from tableName;

-返回 amy

8.分割字符串函数: split ****
语法: split(string str, string pat)
返回值: array
说明: 按照pat字符串分割str,会返回分割后的字符串数组

hive> select split('abtcdtef','t') from tableName;

-返回 ["ab","cd","ef"]

9.缺失值填充函数:nvl ****
语法:nvl(expr1,expr2)
返回值: string
说明:如果第一个参数为空那么显示第二个参数的值,如果第一个参数的值不为空,则显示第一个参数本来的值。
posted @ 2020-08-01 14:46  peng_li  阅读(1651)  评论(0编辑  收藏  举报
1