Hive常见问题记录
 # 常用函数
- coalesce ( expression [ ,...n ] ) :返回其参数中第一个非空表达式
 - row_number() over(partition by field order by field) : 每个分组内对行进行排序并编号
 - split(str,exp) : 拆分字符串
 - size(ary) :求数组长度
 - concat_ws( Separator,field [ ,...n ]) :连接字符串的函数
 - collect_list(field) :对某一列进行合并
 - collect_set(field) :对某一列进行合并(去重)
 - get_json_object(str,'$.field') :解析json字符串,获取对应key的value值
 - date_add(timestamp, num) :时间计算,如添加固定天数
 - date_format(timestamp|str,formatStr) :时间格式化
 - from_unixtime(unix_time, format): 将一个时间戳转换为指定的格式
 - unix_timestamp(date[, pattern]):返回日期对应的时间戳
 
# SQL
- 创建表
 
create external table if not exists my.table_name (
    name String COMMENT ' ',
    path String COMMENT ' ',
    title String COMMENT ' ',
    icon String COMMENT ' ',
    index int COMMENT ' ',
    visible int COMMENT ' '
)
COMMENT  ''
stored as orcfile
location '/warehouse/my.db/table_name';
 1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
- 创建临时表
 
create external table my.table_name stored as orcfile location '/warehouse/my.db/table_name' as
    select ...
 1
2
2
- 创建临时视图
 
ss.sql(sql.format([args])).createOrReplaceTempView("table_name")
 1
- 删除视图
 
ss.catalog.dropTempView("table_name")
 1
- 分区表插入
 
insert  overwrite table table_name partition(分区字段) ...
 1
- 查询分区表
 
select * from table_name where day = '2024-01-01'
 1
- json字符串解析
 
- 正常json字符串
 
select get_json_object(query, "$.attr") as attr from  table_name where day = '2024-01-01'
 1
- 去掉字符串首尾的引号,替换转义字符
 
select get_json_object(replace(regexp_replace(query,'("*+)$|^("*+)',''),'\\\\"','"'), '$.attr') as attr from  table_name where day = '2024-01-01'
 1
- 解析json中的数组
 
explode 炸裂函数: explode函数能够将array及map类型的数据炸开,实现一行变多行
lateral view侧视图: Lateral 也是用来对数组进行列转行的,Lateral View主要解决在select使用UDTF(如explode)做查询的过程中查询只能包含单个UDTF,不能包含其它字段以及多个UDTF的情况(及不能添加额外的select列的问题)
# explode就是将hive一行中复杂的array或者map结构拆分成多行。配合 lateral view一起使用,能够将array或者map结构的数据展开并且添加额外的列。
lateral view explode(from_json(GET_JSON_OBJECT(get_json_object(query, '$.query'), '$.objectInfo'),'ARRAY<STRUCT<objectCode: STRING,objectName: STRING>>'))
 1
2
3
2
3
最后更新时间: 2024/08/13, 16:59:16