hive sql的重要函数

来源:互联网 发布:怎么看淘宝店铺买家秀 编辑:程序博客网 时间:2024/06/08 05:05

1,类型转化

cast(expr as <type>) 指定type;类型转换。例如将字符”1″转换为整数:cast(’1′ as bigint),如果转换失败返回NULL。

2,日期函数:

1>from_unixtime;from_unixtime语法:from_unixtime(bigint unixtime[, stringformat])

说明: 转化UNIX时间戳到当前时区的时间格式

返回值:string

eg:select from_unixtime(1323308943,'yyyyMMdd') from dual;

20111208


2>

(1)unix_timestamp;语法:   unix_timestamp() 

返回值:   bigint
说明: 获得当前时区的UNIX时间戳

eg:

eg:

(2) unix_timestamp;语法:unix_timestamp(string date) 

返回值:   bigint
说明: 转换格式为“yyyy-MM-dd HH:mm:ss“的日期到UNIX时间戳。如果转化失败,则返回0。

eg:

(3) unix_timestamp;语法:   unix_timestamp(string date,string pattern) 

返回值:   bigint
说明: 指定格式日期转UNIX时间戳函数:转换pattern格式的日期到UNIX时间戳。如果转化失败,则返回0。

eg: select unix_timestamp('20111207 13:01:03','yyyyMMddHH:mm:ss') from dual;

1323234063


3>

 to_date;语法:   to_date(string timestamp) 
返回值:   string
说明: 返回日期时间字段中的日期部分。
举例:
eg: select to_date('2011-12-08 10:03:01') from dual;
2011-12-08


4>

(1)year语法:   year(string date) 
返回值: int
说明: 返回日期中的年。
举例:
hive>   select year('2011-12-08 10:03:01') from dual;
2011
hive>   select year('2012-12-08') from dual;
2012

(2)month语法: month   (string date) 
返回值: int
说明: 返回日期中的月份。
举例:
hive>   select month('2011-12-08 10:03:01') from dual;
12
hive>   select month('2011-08-08') fromdual;
8

(3) day语法: day   (string date) 
返回值: int
说明: 返回日期中的天。
举例:
hive>   select day('2011-12-08 10:03:01') from dual;
8
hive>   select day('2011-12-24') fromdual;
24

(4) hour语法: hour   (string date) 
返回值: int
说明: 返回日期中的小时。
举例:
hive>   select hour('2011-12-08 10:03:01') from dual;
10

(5) minute语法: minute   (string date) 
返回值: int
说明: 返回日期中的分钟。
举例:
hive>   select minute('2011-12-08 10:03:01') from dual;
3

(6) second语法: second   (string date) 
返回值: int
说明: 返回日期中的秒。
举例:
hive>   select second('2011-12-08 10:03:01') from dual;
1

(7) weekofyear语法:   weekofyear (string date) 
返回值: int
说明: 返回日期在当前的周数。
举例:
hive>   select weekofyear('2011-12-08 10:03:01') from dual;
49

5>

(1) datediff语法:   datediff(string enddate,string startdate) 
返回值: int
说明: 返回结束日期减去开始日期的天数。
举例:
hive>   select datediff('2012-12-08','2012-05-09')from dual;
213

(2) date_add语法:   date_add(string startdate, intdays) 
返回值: string
说明: 返回开始日期startdate增加days天后的日期。
举例:
hive>   select date_add('2012-12-08',10)from dual;
2012-12-18

(3) date_sub语法:   date_sub (string startdate,int days) 
返回值: string
说明: 返回开始日期startdate减少days天后的日期。
举例:
hive>   select date_sub('2012-12-08',10)from dual;
2012-11-28

3.字符函数:

(1)stringreverse(string A)返回倒序字符串(2)stringconcat(string A, string B…)连接多个字符串,合并为一个字符串,可以接受任意数量的输入字符串eg: 1. concat(aa,',',bb,',',cc)

2.SELECT CONCAT('FIRST ', 'SECOND');

(3)collect_set(concat(aa,',',serviceurl,',',servicerunningtime))列转行

eg: 

(4)是把我上面查出来的数组格式中的字符串也以“”,“”链接起来了stringconcat_ws(string SEP, string A, string B…)链接多个字符串,字符串之间以指定的分隔符分开。eg:


(5)<--可以和(3)比较,看看有什么不同-->arraysplit(string str, string pat)将字符串转换为数组。eg:

(6)stringregexp_replace(string A, string B, string C)字符串A中的B字符被C字符替代eg:
eg:

(7)内置的聚合函数(UDAF):max();min();sum();avg()...

(8)内置表生成函数(UDTF):explode(array a)

eg:

数组SQL返回myCol
[1,2]
[3,4]SELECT explode(myCol) AS myNewCol FROM myTablemyNewCol
1
2
3
4

select serviceNumber,split(concat_ws(',',collect_set(concat(aa,',',serviceurl,',',servicerunningtime))),',')  from
(
SELECT serviceNumber, int(list.servicerunningtime) aa,list.serviceurl,list.servicerunningtime from 
(select serviceNumber,serviceBasicInfo from gw_periodic where dt='2017042813')y 
lateral view explode(serviceBasicInfo) as list
order by aa desc limit 100) z group by serviceNumber;












原创粉丝点击