我有带有基元数据类型的 Hive 表,如下所示:
CustomerName|City|Product1|RatingByMe|RatingByOthers|Product2|RatingByMe|RatingByOthers
我希望将其转换为复杂的数据类型,其中 2 个产品有 2 个单独的行,并具有以下架构:
CustomerName|City|Product1|struct[RatingByMe,RatingByOthers]
CustomerName|City|Product2|struct[RatingByMe,RatingByOthers]
如何在 Hive 中实现这一目标?任何线索将不胜感激。
你只需要使用一个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