hiveql—在配置单元中构造复杂数据类型

jjjwad0x  于 2021-06-26  发布在  Hive
关注(0)|答案(1)|浏览(452)

我有一个包含如下基本数据类型的配置单元表:

CustomerName|City|Product1|RatingByMe|RatingByOthers|Product2|RatingByMe|RatingByOthers

我希望将其转换为复杂的数据类型,其中两个产品有两个单独的行,并且具有以下模式:

CustomerName|City|Product1|struct[RatingByMe,RatingByOthers]
CustomerName|City|Product2|struct[RatingByMe,RatingByOthers]

如何在Hive中实现这一点?任何线索都将不胜感激。

n3schb8v

n3schb8v1#

你只需要用一个 union 和一个 named_struct ```
select
CustomerName,
City,
Product1,
named_struct("RatingByMe", RatingByMe, "RatingByOthers", RatingByOthers) as rating
from your_table
union all
select
CustomerName,
City,
Product2,
named_struct("RatingByMe", RatingByMe, "RatingByOthers", RatingByOthers) as rating
from your_table

相关问题