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

Iceberg表中提取JSON数组ct值并拼接为新字段的方法求助

解决方案

可以通过Spark SQL的from_json、transform和array_join函数组合实现需求,具体SQL如下:

SELECT
  custDetail,
  array_join(
    transform(
      from_json(custDetail, 'array<struct<et:string, ct:string>>'),
      x -> x.ct
    ),
    '_'
  ) AS custInitials
FROM custTable;

分步说明:

  1. from_json解析JSON字符串:
    用from_json(custDetail, 'array<struct<et:string, ct:string>>')将字符串类型的custDetail转换成Spark的数组结构体类型,指定的schema完全匹配你的JSON数组结构——每个元素是包含et和ct两个字符串字段的结构体。

  2. transform提取ct字段:
    用transform(..., x -> x.ct)遍历解析后的数组,把每个结构体元素中的ct字段值提取出来,得到一个仅包含ct值的字符串数组(例如['MC', 'TC', 'EC'])。

  3. array_join拼接字符串:
    最后用array_join(..., '_')把这个字符串数组用下划线_连接起来,生成MC_TC_EC格式的字符串,命名为custInitials。

如果需要将结果写入新的Iceberg表,可使用如下语句:

CREATE TABLE newCustTable
USING iceberg
AS
SELECT
  *,
  array_join(
    transform(
      from_json(custDetail, 'array<struct<et:string, ct:string>>'),
      x -> x.ct
    ),
    '_'
  ) AS custInitials
FROM custTable;

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 01:22:39