You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Hive或Presto中将字典格式列转换为按国家汇总人数的格式

实现方案

前提说明

默认你所用的表名为event_attendees,若attendees_per_countries为JSON字符串类型,需先转换为Map类型再执行拆解逻辑。


Presto 写法

SELECT 
  country,
  SUM(people_count) AS number_of_people
FROM event_attendees
-- 将Map类型字段拆解为国家、人数两列
CROSS JOIN UNNEST(attendees_per_countries) AS t(country, people_count)
GROUP BY country
ORDER BY number_of_people DESC;

如果attendees_per_countries是JSON字符串,调整字段转换逻辑即可:

SELECT 
  country,
  SUM(people_count) AS number_of_people
FROM (
  SELECT 
    -- 把JSON字符串转为Map类型
    CAST(json_parse(attendees_per_countries) AS map(varchar, int)) AS attendees_map
  FROM event_attendees
) t
CROSS JOIN UNNEST(attendees_map) AS t(country, people_count)
GROUP BY country
ORDER BY number_of_people DESC;

Hive 写法

SELECT 
  country,
  SUM(people_count) AS number_of_people
FROM event_attendees
-- 用LATERAL VIEW + EXPLODE拆解Map字段
LATERAL VIEW EXPLODE(attendees_per_countries) tmp AS country, people_count
GROUP BY country
ORDER BY number_of_people DESC;

如果attendees_per_countries是JSON字符串,先通过from_json转换为Map类型:

SELECT 
  country,
  SUM(people_count) AS number_of_people
FROM (
  SELECT 
    -- 把JSON字符串转为Map类型
    from_json(attendees_per_countries, 'map<string,int>') AS attendees_map
  FROM event_attendees
) t
LATERAL VIEW EXPLODE(attendees_map) tmp AS country, people_count
GROUP BY country
ORDER BY number_of_people DESC;

内容的提问来源于stack exchange,提问作者yueyue

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 21:30:01