本文主要是介绍hive常用处理函数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
- 行列转换
- get_json_object
当字段是json字符串时,可用此方法解析需要的字段
-- 单层解析
select get_json_object(data, "$.log") as log
-- 嵌套解析
select get_json_object(get_json_object(data, "$.log"), "$.request_id") as request_id
字符串处理
- substr:字符串截断
-- 返回字符串pt从start开始,长度为len的字符串
select substr(pt,start,len) as pt_new
- concat、CONCAT_WS:字符串拼接
-- 拼接3个字符串
select concat(str1, str2,str3) as str_new--以","为分隔符拼接字符串
select CONCAT_WS(",", str1,str2) as str_new
聚合
- group by
- sum
- sum(case when )
条件
- case when
窗口函数
- row_number() over (partition by A order by B desc)
先根据字段A进行分组,每一组按照字段B降序排列,然后给每一组中的数据增加一个行序
例:按照分数给各班级同学排名
class | score | id |
---|---|---|
A | 80 | s1 |
A | 90 | s2 |
B | 85 | s3 |
B | 80 | s4 |
B | 90 | s5 |
C | 85 | s6 |
C | 90 | s7 |
selectclass,score,idrow_number() over (partition by class order by score desc) as rankfrom student
结果:
class | score | id | rank |
---|---|---|---|
A | 90 | s2 | 1 |
A | 80 | s1 | 2 |
B | 90 | s5 | 1 |
B | 85 | s3 | 2 |
B | 80 | s4 | 3 |
C | 90 | s7 | 1 |
C | 85 | s6 | 2 |
其它
- with as
(1)使代码可读性更高
with table_1 as (select * from table_2
)select * from table_1
参考
1.https://blog.csdn.net/qq_41704237/article/details/107300158
这篇关于hive常用处理函数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!