2020-12-22-Hive-12(hive函数)

1.高阶聚合

with cube 函数包含所有可能的组合
grouping sets() 用户自定义所有可能的组合
with rollup 右到做递减多级的统计

2.常用函数

  1. collect_list
  2. collect_set 去重聚合字符串
  3. sort_array 排序
  4. array_contains 数组是否包含
  5. regexp_extract/regexp_replace 正则匹配(替换/解析)
  6. bin conv 转进制 注意是bigint类型
  7. lpad rpad 左补 右补
  8. str_to_map 字符串转map
  9. cast 基础类型之间强制转换
  10. coalesce 非空查找
  11. find_in_set(string str, string strList) 返回 str 在 strlist第一次出现的位置,strlist 是用逗号分割的字符串。如果没有找到该 str 字符,则返回 0
  12. reverse(string str) 反转
  13. lateral view explode 行转列
  14. concat_ws 列转行
  15. concat_ws(string SEP, array) 返回将数组链接成字符串后的结果,SEP 表示各个字符串间的分隔符
  16. get_json_object(string json_string, string path) json解析
data =
{
 "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" 
}
单层查询
select  get_json_object(data, '$.owner') from test;  结果 amy

get多层值
select  get_json_object(data, '$.store.bicycle.price') from test;  结果 19.95

get数组值[]
select  get_json_object(data, '$.store.fruit[0]') from test; 结果 {"weight":8,"type":"apple"}
  1. instr(string str, string substr) 字符串查找(返回字符串 substr 在 str 中首次出现的位置)
  2. locate(string substr, string str[, int pos]) 字符串查找(返回字符串 substr 从pos位置开始在 str 中首次出现的位置)
  3. substr(string A, int start, int len),substring(string A, intstart, int len) 字符串截取

3.窗口函数

sum求和
count求总
max/min求最值
first_value/last_value取第一个/取最后一个
row_number/dense_rank/rank序列函数
lag/lead偏移量函数
ntile桶函数
percentile分位函数(可以求中位数)

4.日期函数

from_unixtime(unix_timestamp(),’yyyy-MM-dd HH:mm:ss’)
unix_timestamp 转时间戳函数(可指定格式注意)
datediff/date_add/date_sub日期相减/相加/比较
year/month/day/hour/minute/second/weekofyear 日期取数

你可能感兴趣的:(2020-12-22-Hive-12(hive函数))