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

寻求Beam 2.19后org.apache.beam.sdk.schemas.transforms.Unnest的替代方案文档

Beam 2.19+ 替代原Unnest变换的方案

1. 处理数组型嵌套字段:FieldAccess + Flatten

如果要展开的是数组类型字段,可先通过FieldAccess提取目标数组字段,再用Flatten将数组拆分为单个元素:

// 假设输入PCollection的Schema包含名为"array_field"的数组字段
PCollection<Row> input = ...;
PCollection<Row> unnested = input
    .apply(FieldAccess.field("array_field"))
    .apply(Flatten.iterables());

2. 展开结构体嵌套字段:Select变换

对于嵌套的结构体字段,直接通过Select指定嵌套字段的完整路径,即可将嵌套字段展开到顶层:

PCollection<Row> input = ...;
// 展开nested.field1、nested.field2,保留顶层字段other_top_field
PCollection<Row> unnested = input.apply(
    Select.fields("nested.field1", "nested.field2", "other_top_field")
);

3. 自定义ParDo实现复杂Unnest逻辑

如果需要同时处理多层嵌套或混合类型的展开需求,可自定义ParDo来实现灵活的展开逻辑:

PCollection<Row> input = ...;
// 预先定义输出数据的Schema
Schema outputSchema = Schema.builder()
    .addStringField("top_field")
    .addInt32Field("nested_field")
    .build();

PCollection<Row> unnested = input.apply(ParDo.of(new DoFn<Row, Row>() {
  @ProcessElement
  public void processElement(ProcessContext c) {
    Row originalRow = c.element();
    // 提取嵌套数组字段
    List<Row> nestedRows = originalRow.getArray("nested_array", Row.class);
    for (Row nestedRow : nestedRows) {
      // 合并顶层字段与嵌套字段,构造新Row输出
      Row newRow = Row.withSchema(outputSchema)
          .addValue(originalRow.getString("top_field"))
          .addValue(nestedRow.getInt("nested_field"))
          .build();
      c.output(newRow);
    }
  }
}));

4. 利用SchemaTransforms模块的内置能力

Beam后续版本的SchemaTransforms模块提供了更丰富的字段处理组合能力,可通过字段投影、数组展开等内置逻辑的组合,完全替代原Unnest变换的功能。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 05:00:55