用于处理一对多关系的Hive复杂类型

时间:2016-12-20 08:08:36

标签: sql hadoop hive

如何使用Hive复杂类型处理一对多关系?例如,给出两个表:

artist: artist_id, first_name, last_name 
song:  song_id, song_name, song_date, artist_id 

如何编写hiveql或sql以将歌曲集合包含在独特的艺术家中,即

e.g。

112, drew, jackson, {10: [hill, 1992], 13: [away, 2011], .... }
113, maria,mcmillan, {25: [denial, 2000], 26: [fly, 1990], .... }

1 个答案:

答案 0 :(得分:2)

select      a.artist_id, a.first_name, a.last_name 
           ,s.songs

from                    artist  as a

            left join  (select      artist_id
                                   ,concat('{',concat_ws(',',collect_list(concat(cast(song_id as string),':[',song_name,',',cast(song_date as string),']'))),'}') as songs

                        from        song    as s

                        group by    artist_id
                        ) s

            on          s.artist_id =
                        a.artist_id
;