hive explode使用示例
假设我们有一个表students,其中包含学生姓名和成绩,以逗号分隔。我们想要将每个学生的成绩拆分成单独的行,以便更容易进行数据分析。
首先,我们创建一个名为students的表,并插入一些数据:
CREATE TABLE students (name STRING, scores STRING);
INSERT INTO students VALUES ('John', '80,90,85'), ('Jane', '95,92,87');
现在,我们可以使用explode函数来将scores列拆分为单独的行:
SELECT name, score
FROM students
LATERAL VIEW explode(split(scores, ',')) scores_table AS score;
这将返回以下结果:
name score
John 80
John 90
John 85
Jane 95
Jane 92
Jane 87
我们使用split函数将scores列拆分为逗号分隔的字符串数组,并使用explode函数将其转换为单独的行。最后,我们使用LATERAL VIEW子句将scores_table别名应用于生成的行集
原文地址: http://www.cveoy.top/t/topic/dqtB 著作权归作者所有。请勿转载和采集!