trick:
1.能不用distinct去重,就不用它,可以考虑使用group by;
函数汇总:
1.求日期时间差:
datediff(时间A,时间B),输入是字符串类型,因此用单引号'';输出是int类型。
例子:select datediff('2018-06-06','2018-05-06')
结果:31
hive中还要一个日期前推n天的函数,得到的还是日期格式的时间:date_sub(日期,前推的时间),例子:
select date_sub('2018-08-23',1)
得到的结果是‘2018-08-22’。
两种日期的转换:dt = '20190408',partition_date = '2019-04-08'
从partition_date到dt:date2datekey('partition_date'),反过来用datekey2datek('dt')
select date2datekey('2019-04-08')
2.求星期几:pmod()
pmod(datediff(partition_date,'2018-05-06'),7)
3.根据时间戳求对应的日期:
from_unixtime(时间戳)。这里的时间戳包含两种形式:一种是10位,直接作为from_unixtime的输入就好,一类是13位,这时需要除以1000.返回值是'2018-06-02'字样的日期。
from_unixtime(timestamp)
4.获取小时:
hour()
hour(from_unixtime(order_add_timestamp))
5.字符串中查找字符:
instr( string1, string2, start_position,nth_appearance ),
string1:源字符串,要在此字符串中查找。
string2:要在string1中查找的字符串 。
start_position:代表string1 的哪个位置开始查找。
instr(weekday_unavailable,'5')
6.拆分字符串split()
split('192.168.0.1','\\.')
得到的结果:
["192","168","0","1"]
需要注意的是:
当然当split包含在 "" 之中时 需要加4个\
如 hive -e ".... split('192.168.0.1','\\\\.') ... " 不然得到的值是null
同样的 | 等特殊符号也需要做类似 处理。
7. 连接字段的函数:concat()
用法:concat(string s1, string s2, string s3)
这个函数能够把字符串类型的数据连接起来,连接的某个元素可以是列值。
如 concat( name, ':', score) 就相当于把name列和score列用逗号连接起来了。
8. 强制转换类型函数:cast()
用法:cast(value AS TYPE)
功能:将某个列的值显示的转化为某个类型
例子:cast(score as string ) 将double类型的数据转化为了String类型
9. 设置固定连接符号的连接字段的函数:contact_ws()
9.1去重collect_set()
用法:contact_ws(seperator, string s1, string s2...)
功能:制定分隔符将多个字符串连接起来
使用concat_ws()和collect_set()进行合并行
将上面列表中一个user可能会占用多行转换为每个user占一行的目标表格式,实际是“列转行”
select user,concat_ws(',',collect_set(concat(order_type,'(',order_number,')'))) order from table group by user
其中 order是别名
collect_set的作用:
(1)去重,对group by后面的user进行去重【划重点,很重要,在某公司的组件上,不去重,不能运行】
(2)对group by以后属于同一user的形成一个集合,结合concat_ws对集合中元素使用,进行分隔形成字符串
10.组内排序函数:row_number()
用法:select COLUMN_A,COLUMN_B,
row_number() OVER (partition BY COLUMN_A ORDER BY COLUMN_B ASC/DESC) rn
from table
功能:组内排序
详情参考:https://blog.csdn.net/pearl8899/article/details/83144224
11.组内连接各个字段的函数:group_concat
用法:group_concat([DISTINCT] 要连接的字段 [Order BY ASC/DESC 排序字段] [Separator '分隔符'])
功能:将有相同key的记录中的其他字段组合起来,自动连接为一个字段,该字段中的每一个元素都是同一个意思,具有相同的key。
例子:
- select * from aa;
+------+------+
| id| name |
+------+------+
|1 | 10|
|1 | 20|
|1 | 20|
|2 | 20|
|3 | 200 |
|3 | 500 |
+------+------+
select id,group_concat(name) from aa group by id;
+------+--------------------+
| id| group_concat(name) |
+------+--------------------+
|1 | 10,20,20|
|2 | 20 |
|3 | 200,500|
+------+--------------------+
3 rows in set (0.00 sec)
12.返回第一个非null的参数,Coalesce函数
用法:COALESCE ( expression [ ,...n ] ),返回其参数中第一个非空表达式
举例:SELECT COALESCE(NULL, NULL, GETDATE())
解释:由于两个参数都为null,所以返回getdate()函数的值,也就是当前时间。即返回第一个非空的值。由于这个函数是返回第一个非空的值,所以参数里面必须最少有一个非空的值。如果里面都是null,则会报错。
13.将字符串转化为map的格式,函数str_to_map
用法:str_to_map(text[, delimiter1, delimiter2])
Splits text into key-value pairs using two delimiters. Delimiter1 separates text into K-V pairs, and Delimiter2 splits each K-V pair. Default delimiters are ',' for delimiter1 and '=' for delimiter2.
hive>
> select str_to_map('aaa:11&bbb:22', '&', ':')
> from tmp.tmp_jzl_20140725_test11;
OK
{"bbb":"22","aaa":"11"}
案例2:
hive> select str_to_map('aaa:11&bbb:22', '&', ':')['aaa']
> from tmp.tmp_jzl_20140725_test11;
14 删除1年前的数据分区
alter table app.test drop if exists partition(dt='$now.$now.delta(365).datekey');
15 get_json_object(AAA, '$.XXX')
解析json格式的字段,AAA={"DIB":"249.0","DIe3":"179.0"} ,
select get_json_object(AAA, '$.DIe3')
from aaa.xxxx
out:179.0
16 size()计算数组长度
17 length()计算字符串的长度