0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Apache Beam

Last updated at Posted at 2025-07-20

1.Apache Beamとは

Apache Beam は、データ処理パイプラインを抽象化して記述できるオープンソースのフレームワークです。Googleが提唱し、Apache Software Foundation によって管理されています。

Apache Beam には以下のような特徴があります。

(i) バッチ・ストリーミングを同じモデルで統一的に扱える

(ii) Java / Python / Goなどの SDK に対応

(iii) 実行環境(ランナー)を自由に選択可能

  • Google Cloud Dataflow

  • Apache Flink

  • Apache Spark

  • DirectRunner(ローカル)

2.パイプラインの処理

パイプラインは、PCollectionとTransformからなります。

2.1.PCollection(ピー・コレクション)

PCollectionとは、Apache Beamにおいてデータを表す「分散可能なデータの集合」です。

2.2.ParDo(Parallel Do) Transform(並列処理)

Apache Beamで複雑なデータ変換ロジックを書くときの定番ツール。
例えば、データパイプライン内でデータの一部が破損していることが判明し、これらの破損データを除外または分離する必要がある場合、ParDo トランスフォームが有効な手段となります。

2.3.DoFn(ユーザー定義関数)

Beam では、データの各要素に対して任意の処理を行うために DoFn(Do Function)を使います。

DoFn から外部サービス(Pub/Subなど)へ直接書き込むのは非推奨。Beam の原則に反し、可搬性や拡張性に問題あり。

3.マネージドI/O

3.1.BigQueryIO

BigQueryIO は、Apache Beam SDK(Java や Python)を使って BigQuery とやり取りするための I/O トランスフォームです。
Beam パイプラインの中で:

  • BigQuery からデータを読み取る(Read)

  • BigQuery にデータを書き込む(Write)

といった操作を行う際に使います。
BigQuery にロードするデータ形式として Avro を選びたい場合、Beam で Avro を処理して BigQuery にロードします。

コードの例

// --- BigQuery 読み込み設定 ---
    BigQueryIO.TypedRead<TableRow> reader =
        BigQueryIO.readTableRows()
            // Storage Read API を利用
            .withMethod(BigQueryIO.TypedRead.Method.DIRECT_READ)
            // 列プルーニング(読み取り列を絞る)
            .withSelectedFields(java.util.Arrays.asList(options.getSelectedFields().split(",")))
            // 読み取り時のスナップショット一貫性(必要に応じて)
            .withTemplateCompatibility();

3.2.KafkaIO

KafkaIO は、Apache Beam の I/O コネクタ(IO connector)の一つで、Kafka からデータを読み書きするためのモジュールです。

3.3.Side Output と Side Input

Side Input(副入力)とは参照用の静的または小規模データ。
Side Output(副出力) とは、1つの DoFn や ParDo の処理から 複数の出力先にデータを分岐して出すための仕組み。

4.Google Cloud の Dataflow

Dataflow は Apache Beam SDK をベースにしており、Python や Java 、Go などで記述したデータ処理プログラムを、Google Cloud のインフラ上で実行できます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?