我有一个配置单元列值存储为字符串
[[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
发布于 2021-01-22 18:19:23
因为您的初始数组不是实数组,而是字符串,所以您需要对其进行解析和分解:
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,2],[3,4,8],[5,6,7,9]] 1,2 2
[[1,2],[3,4,8],[5,6,7,9]] 3,4,8 3
[[1,2],[3,4,8],[5,6,7,9]] 5,6,7,9 4
现在您可以添加sum()
和group by
。
https://stackoverflow.com/questions/65837187
复制相似问题