问题描述
[[1,2],[3,4,8],[5,6,7,9]]
我需要找出每个内部数组的长度。我该怎么办?
基本上我需要一个查询来总结每个内部数组的大小。如果将此列存储为数组数组,我会做这样的事情
select sum(size(innerArray)) from myTab lateral view explode (mycol) arr as innerArray;
但是现在当我尝试上述方法时,我明白了
Failed: UDFArgumentException explode() takes an array or a map as a parameter
解决方法
因为你的初始数组不是真正的数组,是字符串,你需要解析和分解它:
with mytable as(
select '[[1,2],[3,4,8],[5,6,7,9]]' as mycol
)
select mycol as original_string,innerArray_str,--split inner array and get size
size(split(innerArray_str,',')) inner_array_size
from mytable
--explode upper array
--replace `],` (allow spaces before comma) with `,` and remove all `[` and `]`,split using,as a delimiter
lateral view outer explode(split(regexp_replace(regexp_replace(mycol,'\\] *,'),'\\[|\\]',''),') )e as innerArray_str
结果:
original_string innerarray_str inner_array_size
[[1,9]] 1,2 2
[[1,9]] 3,8 3
[[1,9]] 5,9 4
现在您可以添加 sum()
和 group by
。