Beam YAML の集計

Beam YAML には、記録全体で値をグループ化して結合する集計を実行する機能があります。これは Combine 変換タイプによって実現されます。

たとえば、次のように書くことができます。

- type: Combine
  config:
    group_by: col1
    combine:
      total:
        value: col2
        fn:
          type: sum

関数が構成要件を持たない場合、それを文字列として直接提供できます。

- type: Combine
  config:
    group_by: col1
    combine:
      total:
        value: col2
        fn: sum

出力フィールド名が入力フィールド名と同じである場合、これはさらに簡略化できます。

- type: Combine
  config:
    group_by: col1
    combine:
      col2: sum

一度に多くのフィールドを集計できます

- type: Combine
  config:
    group_by: col1
    combine:
      col2: sum
      col3: max

または、複数のフィールドでグループ化します

- type: Combine
  config:
    group_by: [col1, col2]
    combine:
      col3: sum

またはまったくグループ化しません(これにより、単一の出力を備えたグローバルな結合が生成されます)

- type: Combine
  config:
    group_by: []
    combine:
      col2: sum
      col3: max

ウィンドウ化された集計

他の変換と同様に、Combine はウィンドウ化パラメータをとることができます

- type: Combine
  windowing:
    type: fixed
    size: 60s
  config:
    group_by: col1
    combine:
      col2: sum
      col3: max

ウィンドウ化の仕様が提供されていない場合、それはアップストリームからウィンドウ化のパラメータを継承します。たとえば、

- type: WindowInto
  windowing:
    type: fixed
    size: 60s
- type: Combine
  config:
    group_by: col1
    combine:
      col2: sum
      col3: max

は上記の例と同じです。

カスタムの集計関数

language パラメータを設定することで、Python で定義された集計関数を使用できます。

- type: Combine
  config:
    language: python
    group_by: col1
    combine:
      biggest:
        value: "col2 + col2"
        fn:
          type: 'apache_beam.transforms.combiners.TopCombineFn'
          config:
            n: 10

SQL スタイルの集計

言語を SQL に設定することで、結合 fn として完全な SQL スニペットを提供できます。

- type: Combine
  config:
    language: sql
    group_by: col1
    combine:
      num_values: "count(*)"
      total: "sum(col2)"

もちろん、Sql 変換タイプを使用してクエリを直接提供することもできます。