Hive中的posexplode
函數用于將數組或結構體類型的列展開為多行
假設我們有一個名為user_info
的表,其中包含一個名為interests
的數組類型列,我們希望將其展開以便查看每個用戶的興趣。
CREATE TABLE user_info (
id INT,
name STRING,
interests ARRAY<STRING>
);
插入一些示例數據:
INSERT INTO user_info (id, name, interests)
VALUES (1, 'Alice', ARRAY('reading', 'traveling')),
(2, 'Bob', ARRAY('sports', 'music'));
使用posexplode
函數展開interests
列:
SELECT id, name, posexplode(interests) as interest
FROM user_info;
這將返回以下結果:
id | name | interest
-------------------------
1 | Alice | reading
1 | Alice | traveling
2 | Bob | sports
2 | Bob | music
在這個例子中,posexplode
函數根據數組索引展開interests
列,并將每個興趣單獨的行返回。這樣,您就可以輕松地查看和處理動態數據。