Hive-高级操作

复杂数据类型

array, map, struct...

访问复杂数据类型:array[n], map[key], struct.x

Built-in Functions

concat_ws():连接字符串,需要制定分隔符(concat with separator)

regexp_extract():提取

regexp_replace():替换

get_json_object():$: Root object; .: Child operator 

Built-in Aggregate Functions (UDAF)

collect_set():将某字段的值进行去重汇总,返回array类型字段

collect_list():将某字段的值进行不去重汇总,返回array类型字段

扫描二维码关注公众号,回复: 13224792 查看本文章

Built-in Table-Generating Functions (UDTF)

explode():输入为array或map,将集合中多个元素进行炸裂开,每一个元素占用单独的一行;解析json数组时需要用到explode()函数

lateral view语法: 和UDTF函数如explode()函数连用,因为UDTF有限制:No other expressions are allowed in SELECT; UDTF's can't be nested; GROUP BY / CLUSTER BY / DISTRIBUTE BY / SORT BY is not supported

行列转换

行转列

  • case when
  • collect_set() ( + concat_ws() ):行聚合

列转行

  • union
  • split() + explode() + lateral view:行炸裂

pivot()函数和unpivot()函数的行列转换

行转列

pivot()第一个参数为计算结果的聚合函数,for后面跟需要转化的列,in后面跟该列具体的值(即新的列的列名);也可以用case when实现同样的效果

SELECT *
FROM grade
PIVOT(
SUM(score) FOR subject in (Chinese, Math, English)
)

列转行

也可以用union实现同样的效果

SELECT *
FROM grade
UNPIVOT(
score FOR subject in ("Chinese","Math","English") 

Reference

LanguageManual UDF

LanguageManual LateralView

SQL行转列、列转行的简单实现

猜你喜欢

转载自blog.csdn.net/qq_34276652/article/details/114076842