我有一个hive外部表data_table(id bigint,dataset string)其中dataset是逗号分隔的整数或字符串值集合,如
id dataset
1 4,3,0,6
2 1,9,7
3 0,8,2,7
我的问题:如何使用hive迭代逗号分隔的数据集值?
让我们说,当id = 2(因此数据集= 1,9,7)时,我希望有一个循环结构,允许我创建类似的东西:
when id=2,
select val from source where x=1;
select val from source where x=9;
select val from source where x=7;
有什么建议吗?
答案 0 :(得分:1)
在理想情况下,您不应该有需要迭代数据集的用例。 如果你只需要访问一个特定的元素,你可以使用方括号运算符和像myArray [index]这样的索引来实现。
在其他情况下,来自hive的explode()函数(如上所述)应该有助于你的情况。语法:
cancel()
确保拆分data_set以从中获取数组。您可以使用拆分功能。语法:
SELECT id, explodeTable.dataset from data_table LATERAL VIEW explode(dataset) testTable as explodeTable
您的最终查询应该是这样的:
split(dataset,'\\,')
爆炸功能只接受数组或地图作为参数。
答案 1 :(得分:0)
SELECT id, dummy.x FROM baseTable
LATERAL VIEW explode(split(dataset,'\\,') dummy AS x;
会给你一张这样的表:
id x
1 4
1 3
1 0
1 6
2 1
2 9
2 7
3 0
3 8
3 7
3 2
现在你可以:
select id, x
from newtable
where x in (1,9,7)
答案 2 :(得分:0)
您需要在dataset
和,
explode
select id, x
from (
select id, split(dataset, '\\,') dataset_array
from table ) A
lateral view explode(dataset_array) explodetable as x
之后不确定你想要做什么,但我会留给你写这个查询的外层。