ahmedabu98 commented on code in PR #41584: URL: https://github.com/apache/beam/pull/41584#discussion_r4687353278
########## website/www/site/content/en/documentation/io/built-in/iceberg-cdc.md: ########## @@ -0,0 +1,886 @@ +--- +title: "Apache Iceberg CDC quickstart" +--- +<!-- +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +--> + +[Built-in I/O Transforms](/documentation/io/built-in/) + +# Apache Iceberg CDC quickstart + +In this guide, you'll keep one Iceberg table in sync with another using change data capture (CDC), +with a streaming pipeline that applies each change as it lands. Everything will run locally on your +machine. + +We'll work with two pieces: + +* The **Iceberg CDC source** reads a table's inserts, updates and deletes from its snapshots, in + batch or streaming. +* The **Iceberg CDC sink** applies those changes to another table by primary key, via merge-on-read. + +For the why and how behind it, read +[Stream changes between Iceberg tables with Apache Beam](/blog/iceberg-cdc-sql/). + +{{< language-switcher sql java py yaml >}} + +<style> + /* The selector above sets the language for the whole page, so hide the per-section tabs. */ + div.language-switcher { display: none !important; } + /* Without a tab bar above it, a code block doesn't need the gap or the square corner left for one. */ + .code-snippet { border-top-left-radius: 8px; } + .code-snippet pre { margin-top: 0; } + /* Expected rows: a small bordered table, only as wide as its content. */ + .expected-rows table { width: auto; } + .expected-rows th, .expected-rows td { border: 1px solid #ddd; padding: 8px 12px; } +</style> + +## Before you begin + +{{< paragraph class="language-sql" >}} +You need Java 17 or later and the Beam SQL shell, version 2.79.0 or later. Pipelines run on your +machine, and on Dataflow when you're ready to scale up. +{{< /paragraph >}} + +{{< paragraph class="language-java" >}} +You need Java 17 or later and Beam 2.79.0 or later. This guide runs on the Java DirectRunner, and later on Dataflow when you're ready to scale up. +{{< /paragraph >}} + +{{< paragraph class="language-py language-yaml" >}} +You need Python 3.11 or later and Beam 2.79.0 or later. You also need Java 17 or later on your `PATH`, since the +Iceberg transforms run in Java. The streaming steps use the +[Prism runner](/documentation/runners/prism/), which Beam downloads the first time you use it. +{{< /paragraph >}} + +{{< paragraph class="language-sql" >}} +Install the [Beam SQL shell](/documentation/dsls/sql/shell/#installation), and start it in your terminal with Iceberg support: +{{< /paragraph >}} + +{{% section class="language-sql" %}} +```shell +./beam-sql.sh --version 2.79.0 --io iceberg +``` +{{% /section %}} + +{{< paragraph class="language-java" >}} +Add these dependencies. `slf4j-simple` prints Beam's logs, including the rows you'll check later: +{{< /paragraph >}} + +<section class="language-java"> + +{{< runner maven >}} +<dependency> + <groupId>org.apache.beam</groupId> + <artifactId>beam-sdks-java-managed</artifactId> + <version>{{< param release_latest >}}</version> +</dependency> +<dependency> + <groupId>org.apache.beam</groupId> + <artifactId>beam-sdks-java-io-iceberg</artifactId> + <version>{{< param release_latest >}}</version> + <scope>runtime</scope> +</dependency> +<dependency> + <groupId>org.apache.beam</groupId> + <artifactId>beam-runners-direct-java</artifactId> + <version>{{< param release_latest >}}</version> + <scope>runtime</scope> +</dependency> +<dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-simple</artifactId> + <version>2.0.16</version> + <scope>runtime</scope> +</dependency> +{{< /runner >}} +{{< runner gradle >}} +dependencies { + implementation("org.apache.beam:beam-sdks-java-managed:{{< param release_latest >}}") + runtimeOnly("org.apache.beam:beam-sdks-java-io-iceberg:{{< param release_latest >}}") + runtimeOnly("org.apache.beam:beam-runners-direct-java:{{< param release_latest >}}") + runtimeOnly("org.slf4j:slf4j-simple:2.0.16") +} +{{< /runner >}} + +</section> + +{{< paragraph class="language-py" >}} +Install Beam: +{{< /paragraph >}} + +{{% section class="language-py" %}} +```shell +pip install 'apache-beam>=2.79.0' +``` +{{% /section %}} + +{{< paragraph class="language-yaml" >}} +Install Beam with YAML support. Save each pipeline below to a file and run it with +`python -m apache_beam.yaml.main --yaml_pipeline_file=<file>`. +{{< /paragraph >}} + +{{% section class="language-yaml" %}} +```shell +pip install 'apache-beam[yaml]>=2.79.0' +``` +{{% /section %}} + +## 1. Set up a catalog + +Use a Hadoop catalog in a local directory. No extra setup is needed. If you've run this guide +before, delete the directory first, so you start with empty tables: + +```shell +rm -rf /tmp/beam-iceberg-cdc-quickstart +``` + +{{< paragraph class="language-sql" >}} +You'll use three shells. Start the first one, then create the catalog and a database: +{{< /paragraph >}} + +{{< highlight sql >}} +CREATE CATALOG quickstart TYPE 'iceberg' +PROPERTIES ( + 'type' = 'hadoop', + 'warehouse' = 'file:///tmp/beam-iceberg-cdc-quickstart' +); + +CREATE DATABASE IF NOT EXISTS quickstart.shop; +USE DATABASE quickstart.shop; +{{< /highlight >}} +{{< highlight java >}} +Map<String, Object> catalog = + Map.of( + "catalog_name", "quickstart", + "catalog_properties", + Map.of("type", "hadoop", "warehouse", "file:///tmp/beam-iceberg-cdc-quickstart")); +{{< /highlight >}} +{{< highlight py >}} +import apache_beam as beam + +catalog = { + "catalog_name": "quickstart", + "catalog_properties": { + "type": "hadoop", + "warehouse": "file:///tmp/beam-iceberg-cdc-quickstart", + }, +} +{{< /highlight >}} +{{< highlight yaml >}} +# Every pipeline in this guide uses this catalog: +catalog_name: quickstart +catalog_properties: {type: hadoop, warehouse: "file:///tmp/beam-iceberg-cdc-quickstart"} +{{< /highlight >}} + +{{< paragraph class="language-java" >}} +You'll run the steps below as separate programs, and each one starts with this `catalog`. +{{< /paragraph >}} + +{{< paragraph class="language-py" >}} +You'll run the steps below as separate scripts, and each one starts with this `catalog`. +{{< /paragraph >}} + +## 2. Create the source table + +{{< paragraph class="language-sql" >}} +Create the source table and open a live view of the table's changes. It's empty for now, but will +get populated when we start writing to it in step 4. Leave it open. +{{< /paragraph >}} + +{{< paragraph class="language-java language-py language-yaml" >}} +Create the source table by writing a first round of changes to it with Beam's CDC sink. +{{< /paragraph >}} + +{{< highlight sql >}} +CREATE EXTERNAL TABLE orders ( + id BIGINT PRIMARY KEY NOT ENFORCED, + item VARCHAR, + qty INTEGER +) +TYPE 'iceberg'; + +SELECT * FROM CHANGES(orders) +WITH ('streaming' = true, 'starting_strategy' = 'earliest', 'poll_interval_seconds' = 1); +{{< /highlight >}} + +<section class="language-sql"> + +<img class="center-block" src="/images/iceberg-cdc/live-view-open.gif" + alt="The SELECT is typed into the Beam SQL shell, and the live view opens with 0 rows."> + +</section> + +{{< highlight java >}} +// The sink creates the table on first write, with `id` as its primary key. +static final Schema CHANGE = + Schema.builder() + .addInt64Field("id") + .addStringField("item") + .addInt32Field("qty") + .addStringField("op") // the change type + .addInt64Field("seq") // the sequence number + .build(); + +static Row change(long id, String item, int qty, String op, long seq) { + return Row.withSchema(CHANGE).addValues(id, item, qty, op, seq).build(); +} + +static void applyChanges(Map<String, Object> catalog, Row... changes) { + Map<String, Object> write = new HashMap<>(catalog); + write.put("table", "shop.orders"); + write.put("mode", "merge-on-read"); + write.put("equality_columns", List.of("id")); + write.put("change_type_column", "op"); + write.put("sequence_number_column", "seq"); + + Pipeline p = Pipeline.create(); + p.apply(Create.of(Arrays.asList(changes)).withRowSchema(CHANGE)) + .apply(Managed.write(Managed.ICEBERG).withConfig(write)); + p.run().waitUntilFinish(); +} +{{< /highlight >}} +{{< highlight py >}} +# The sink creates the table on first write, with `id` as its primary key. +def change(id, item, qty, op, seq): + return beam.Row(id=id, item=item, qty=qty, op=op, seq=seq) + +def apply_changes(*changes): + with beam.Pipeline() as p: + _ = ( + p + | beam.Create(changes) + | beam.managed.Write(beam.managed.ICEBERG, config={ + **catalog, + "table": "shop.orders", + "mode": "merge-on-read", + "equality_columns": ["id"], + "change_type_column": "op", + "sequence_number_column": "seq", + })) +{{< /highlight >}} + +<section class="language-java language-py language-yaml"> + +Each change record carries two metadata values: + +- the record's change type (`op`) +- the record's sequence number (`seq`), which goes up with each change to the same key. Both halves of + an update (`UPDATE_BEFORE` and `UPDATE_AFTER`) share one. + +</section> + +{{< highlight java >}} +// Snapshot 1: three new orders +applyChanges(catalog, + change(1, "apple", 3, "INSERT", 1), + change(2, "pear", 5, "INSERT", 1), + change(3, "plum", 2, "INSERT", 1)); +{{< /highlight >}} +{{< highlight py >}} +# Snapshot 1: three new orders +apply_changes( + change(1, "apple", 3, "INSERT", 1), + change(2, "pear", 5, "INSERT", 1), + change(3, "plum", 2, "INSERT", 1)) +{{< /highlight >}} +{{< highlight yaml >}} +# snapshot_1.yaml: three new orders. The sink creates the table, with `id` as its key. +pipeline: + type: chain + transforms: + - type: Create + config: + elements: + - {id: 1, item: apple, qty: 3, op: INSERT, seq: 1} + - {id: 2, item: pear, qty: 5, op: INSERT, seq: 1} + - {id: 3, item: plum, qty: 2, op: INSERT, seq: 1} + - type: WriteToIceberg + config: + table: shop.orders + catalog_name: quickstart + catalog_properties: {type: hadoop, warehouse: "file:///tmp/beam-iceberg-cdc-quickstart"} + mode: merge-on-read + equality_columns: [id] + change_type_column: op + sequence_number_column: seq +{{< /highlight >}} + +## 3. Keep a table in sync (streaming) + +<section class="language-sql"> + +Now open a **second shell**. Here, we'll run a streaming pipeline that continuously observes the +original `orders` table and applies its changes to a downstream table. + +Connect to the catalog, and create a second table, `orders_mirror`. + +</section> + +{{< highlight sql >}} +CREATE CATALOG quickstart TYPE 'iceberg' +PROPERTIES ( + 'type' = 'hadoop', + 'warehouse' = 'file:///tmp/beam-iceberg-cdc-quickstart' +); +USE DATABASE quickstart.shop; + +CREATE EXTERNAL TABLE orders_mirror ( + id BIGINT PRIMARY KEY NOT ENFORCED, + item VARCHAR, + qty INTEGER +) +TYPE 'iceberg'; +{{< /highlight >}} + +{{< paragraph class="language-sql" >}} +Continuously stream changes from `orders` into `orders_mirror`. On the read side, select two +Iceberg metadata columns by name: +{{< /paragraph >}} + +{{< paragraph class="language-java" >}} +Now write a **second program**. Here, we'll run a streaming pipeline that continuously observes the +original `orders` table and applies its changes to a downstream table, `orders_mirror`. +{{< /paragraph >}} + +{{< paragraph class="language-py language-yaml" >}} +Now open a **second terminal**. Here, we'll run a streaming pipeline that continuously observes the +original `orders` table and applies its changes to a downstream table, `orders_mirror`. +{{< /paragraph >}} + +{{< paragraph class="language-java language-py language-yaml" >}} +On the read side, request two Iceberg metadata columns with `include_metadata_columns`: +{{< /paragraph >}} + +- the change type (`_change_type`) +- the snapshot sequence number (`_commit_snapshot_sequence_number`) + +On the write side, point `change_type_column` at the type. You don't need to set +`sequence_number_column`, because it already defaults to `_commit_snapshot_sequence_number`. The +sink drops both columns before it writes. + +Streaming writes need a commit window, which you set with `triggering_frequency_seconds`. Streaming +reads need a polling interval, which you set with `poll_interval_seconds` (defaults to 60s). A +streaming read starts at the latest snapshot by default, so set `starting_strategy` to `earliest` +to also apply the changes made before the pipeline started. + +{{< highlight sql >}} +INSERT INTO orders_mirror +WITH ('mode' = 'merge-on-read', + 'change_type_column' = '_change_type', + 'triggering_frequency_seconds' = 1) +SELECT *, _change_type, _commit_snapshot_sequence_number +FROM CHANGES(orders) +WITH ('streaming' = true, + 'starting_strategy' = 'earliest', + 'poll_interval_seconds' = 1); +{{< /highlight >}} +{{< highlight java >}} +Map<String, Object> read = new HashMap<>(catalog); +read.put("table", "shop.orders"); +read.put("include_metadata_columns", List.of("_change_type", "_commit_snapshot_sequence_number")); +read.put("streaming", true); +read.put("starting_strategy", "earliest"); +read.put("poll_interval_seconds", 1); + +Map<String, Object> write = new HashMap<>(catalog); +write.put("table", "shop.orders_mirror"); +write.put("mode", "merge-on-read"); +write.put("equality_columns", List.of("id")); +write.put("change_type_column", "_change_type"); +write.put("triggering_frequency_seconds", 1); + +Pipeline p = Pipeline.create(); +p.apply(Managed.read(Managed.ICEBERG_CDC).withConfig(read)) + .getSinglePCollection() + .apply(Managed.write(Managed.ICEBERG).withConfig(write)); +p.run().waitUntilFinish(); // runs until you stop it +{{< /highlight >}} +{{< highlight py >}} +from apache_beam.options.pipeline_options import PipelineOptions + +with beam.Pipeline(options=PipelineOptions(streaming=True, runner="PrismRunner")) as p: + _ = ( + p + | beam.managed.Read("iceberg_cdc", config={ + **catalog, + "table": "shop.orders", + "include_metadata_columns": [ + "_change_type", "_commit_snapshot_sequence_number"], + "streaming": True, + "starting_strategy": "earliest", + "poll_interval_seconds": 1, + }) + | beam.managed.Write(beam.managed.ICEBERG, config={ + **catalog, + "table": "shop.orders_mirror", + "mode": "merge-on-read", + "equality_columns": ["id"], + "change_type_column": "_change_type", + "triggering_frequency_seconds": 1, + })) +{{< /highlight >}} +{{< highlight yaml >}} +# mirror.yaml +pipeline: + type: chain + transforms: + - type: ReadFromIcebergCDC + config: + table: shop.orders + catalog_name: quickstart + catalog_properties: {type: hadoop, warehouse: "file:///tmp/beam-iceberg-cdc-quickstart"} + include_metadata_columns: [_change_type, _commit_snapshot_sequence_number] + streaming: true + starting_strategy: earliest + poll_interval_seconds: 1 + - type: WriteToIceberg + config: + table: shop.orders_mirror + catalog_name: quickstart + catalog_properties: {type: hadoop, warehouse: "file:///tmp/beam-iceberg-cdc-quickstart"} + mode: merge-on-read + equality_columns: [id] + change_type_column: _change_type + triggering_frequency_seconds: 1 + +options: + streaming: true + runner: PrismRunner +{{< /highlight >}} + +<section class="language-java"> + +Within a few seconds, the mirror catches up on this first round of changes. Leave the streaming +pipeline running and open a new program to check the contents of `orders_mirror`: + +</section> + +<section class="language-py language-yaml"> + +Within 15 seconds or so, the mirror catches up on this first round of changes. Leave the streaming +pipeline running and open a new terminal to check the contents of `orders_mirror`: + +</section> + +{{< highlight java >}} +static void printTable(Map<String, Object> catalog, String table) { + Map<String, Object> read = new HashMap<>(catalog); + read.put("table", table); + + Pipeline p = Pipeline.create(); + p.apply(Managed.read(Managed.ICEBERG).withConfig(read)) + .getSinglePCollection() + .apply(LogElements.info()); + p.run().waitUntilFinish(); +} + +printTable(catalog, "shop.orders_mirror"); +{{< /highlight >}} +{{< highlight py >}} +def print_table(table): + with beam.Pipeline() as p: + _ = ( + p + | beam.managed.Read(beam.managed.ICEBERG, config={**catalog, "table": table}) + | beam.Map(print)) + +print_table("shop.orders_mirror") +{{< /highlight >}} +{{< highlight yaml >}} +# check.yaml. Run it with: +# python -m apache_beam.yaml.main --yaml_pipeline_file=check.yaml \ +# --jinja_variables='{"table": "shop.orders_mirror"}' +pipeline: + type: chain + transforms: + - type: ReadFromIceberg + config: + table: {{table}} + catalog_name: quickstart + catalog_properties: {type: hadoop, warehouse: "file:///tmp/beam-iceberg-cdc-quickstart"} + - type: LogForTesting +{{< /highlight >}} + +{{< paragraph class="language-yaml" >}} +`check.yaml` takes the table as a [Jinja variable](/documentation/sdks/yaml/#jinja-templatization). +`LogForTesting` logs each row as a line like `{"id": 1, "item": "apple", "qty": 3}`, among Beam's +other log output. +{{< /paragraph >}} + +<section class="language-java language-py language-yaml"> + +You should see these rows, in any order: + +<div class="expected-rows"> + +| id | item | qty | +|---:|-------|----:| +| 1 | apple | 3 | +| 2 | pear | 5 | +| 3 | plum | 2 | + +</div> + +</section> + +## 4. Write some changes and watch them arrive + +{{< paragraph class="language-sql" >}} +Open a third shell. Connect it to the catalog, and check that both tables are there: +{{< /paragraph >}} + +{{< highlight sql >}} +CREATE CATALOG quickstart TYPE 'iceberg' +PROPERTIES ( + 'type' = 'hadoop', + 'warehouse' = 'file:///tmp/beam-iceberg-cdc-quickstart' +); +USE DATABASE quickstart.shop; +SHOW TABLES; +{{< /highlight >}} + +<section class="language-sql"> + +Now change `orders` in two rounds, one snapshot each. Each change record carries two metadata +values: + +- the record's change type (`op`) +- the record's sequence number (`seq`), which goes up with each change to the same key. Both halves of + an update (`UPDATE_BEFORE` and `UPDATE_AFTER`) share one. + +</section> + +{{< highlight sql >}} +-- Snapshot 1: three new orders +INSERT INTO orders +WITH ( + 'mode' = 'merge-on-read', + 'change_type_column' = 'op', + 'sequence_number_column' = 'seq') + (id, item, qty, op, seq) +VALUES (1, 'apple', 3, 'INSERT', 1), + (2, 'pear', 5, 'INSERT', 1), + (3, 'plum', 2, 'INSERT', 1); +{{< /highlight >}} + +<section class="language-sql"> + +Soon you'll see three inserts show up in the live view in the **first shell**, in any order: + +<img class="center-block" src="/images/iceberg-cdc/live-view-snapshot-1.gif" + alt="The live view in the first shell: three inserts appear, for apple, pear and plum."> + +Query both tables. Each should hold the same three orders. + +</section> + +{{< highlight sql >}} +SELECT * FROM orders; +SELECT * FROM orders_mirror; +{{< /highlight >}} + +{{< paragraph class="language-sql" >}} +Now make a second round of changes: +{{< /paragraph >}} + +{{< paragraph class="language-java" >}} +Now make a second round of changes from another program, using the +helpers from step 2: +{{< /paragraph >}} + +{{< paragraph class="language-py" >}} +Now make a second round of changes from another script, using the helpers from step 2: +{{< /paragraph >}} + +{{< paragraph class="language-yaml" >}} +Now make a second round of changes from another terminal: +{{< /paragraph >}} + +{{< highlight sql >}} +-- Snapshot 2: change order 1, delete order 2, add order 4 +INSERT INTO orders +WITH ( + 'mode' = 'merge-on-read', + 'change_type_column' = 'op', + 'sequence_number_column' = 'seq') + (id, item, qty, op, seq) +VALUES (1, 'apple', 3, 'UPDATE_BEFORE', 2), + (1, 'apple', 4, 'UPDATE_AFTER', 2), + (2, 'pear', 5, 'DELETE', 2), + (4, 'kiwi', 1, 'INSERT', 2); +{{< /highlight >}} +{{< highlight java >}} +// Snapshot 2: change order 1, delete order 2, add order 4 +applyChanges(catalog, + change(1, "apple", 3, "UPDATE_BEFORE", 2), + change(1, "apple", 4, "UPDATE_AFTER", 2), + change(2, "pear", 5, "DELETE", 2), + change(4, "kiwi", 1, "INSERT", 2)); +{{< /highlight >}} +{{< highlight py >}} +# Snapshot 2: change order 1, delete order 2, add order 4 +apply_changes( + change(1, "apple", 3, "UPDATE_BEFORE", 2), + change(1, "apple", 4, "UPDATE_AFTER", 2), + change(2, "pear", 5, "DELETE", 2), + change(4, "kiwi", 1, "INSERT", 2)) +{{< /highlight >}} +{{< highlight yaml >}} +# snapshot_2.yaml: change order 1, delete order 2, add order 4 +pipeline: + type: chain + transforms: + - type: Create + config: + elements: + - {id: 1, item: apple, qty: 3, op: UPDATE_BEFORE, seq: 2} + - {id: 1, item: apple, qty: 4, op: UPDATE_AFTER, seq: 2} + - {id: 2, item: pear, qty: 5, op: DELETE, seq: 2} + - {id: 4, item: kiwi, qty: 1, op: INSERT, seq: 2} + - type: WriteToIceberg + config: + table: shop.orders + catalog_name: quickstart + catalog_properties: {type: hadoop, warehouse: "file:///tmp/beam-iceberg-cdc-quickstart"} + mode: merge-on-read + equality_columns: [id] + change_type_column: op + sequence_number_column: seq +{{< /highlight >}} + +<section class="language-sql"> + +You'll see four new changes stream into the live view: + +<img class="center-block" src="/images/iceberg-cdc/live-view-snapshot-2.gif" + alt="The live view: below the first three inserts, four more changes appear: +I for kiwi, -D for + pear, and -U and +U for apple's old and new quantities."> + +Query both tables again to verify the changes were applied correctly: + +</section> + +{{< highlight sql >}} +SELECT * FROM orders; +SELECT * FROM orders_mirror; +{{< /highlight >}} + +<section class="language-java language-py language-yaml"> + +Run the check from step 3 again to verify the changes were applied correctly to `orders` and its downstream mirror: + +</section> + +{{< highlight java >}} +printTable(catalog, "shop.orders"); +printTable(catalog, "shop.orders_mirror"); +{{< /highlight >}} +{{< highlight py >}} +print_table("shop.orders") +print_table("shop.orders_mirror") +{{< /highlight >}} +{{< highlight yaml >}} +# Run check.yaml for each table: +# python -m apache_beam.yaml.main --yaml_pipeline_file=check.yaml \ +# --jinja_variables='{"table": "shop.orders"}' +# python -m apache_beam.yaml.main --yaml_pipeline_file=check.yaml \ +# --jinja_variables='{"table": "shop.orders_mirror"}' +{{< /highlight >}} + +`orders_mirror` should now match `orders`, in any order: + +<div class="expected-rows"> + +| id | item | qty | +|---:|-------|----:| +| 1 | apple | 4 | +| 3 | plum | 2 | +| 4 | kiwi | 1 | + +</div> + +{{< paragraph class="language-sql" >}} +When you're done, press `q` in the first shell to close the live view, and stop the streaming +`INSERT` in the second shell with Ctrl+C. + +{{< /paragraph >}} + +{{< paragraph class="language-java language-py language-yaml" >}} +When you're done, stop the streaming pipeline with Ctrl+C. +{{< /paragraph >}} + +## Run it on Dataflow + +When you're ready to move off your machine, point the catalog at a REST catalog such as +[Lakehouse](/documentation/io/built-in/iceberg/#choose-your-catalog), and run on Dataflow. + +{{< paragraph class="language-sql" >}} +Start the shell with the Dataflow runner. In this quickstart, do this for the +[**second shell**](#3-keep-a-table-in-sync-streaming): +{{< /paragraph >}} + +{{% section class="language-sql" %}} +```shell +./beam-sql.sh --version 2.79.0 --runner dataflow --io iceberg +``` +{{% /section %}} + +{{< paragraph class="language-sql" >}} +Within the shell, set the following pipeline options: +{{< /paragraph >}} + +{{< paragraph class="language-java" >}} +Add these dependencies. The Iceberg ones are for Lakehouse: +{{< /paragraph >}} + +<section class="language-java"> + +{{< runner maven >}} +<dependency> + <groupId>org.apache.beam</groupId> + <artifactId>beam-runners-google-cloud-dataflow-java</artifactId> + <version>{{< param release_latest >}}</version> + <scope>runtime</scope> +</dependency> +<dependency> + <groupId>org.apache.iceberg</groupId> + <artifactId>iceberg-gcp</artifactId> + <version>1.12.0</version> + <scope>runtime</scope> +</dependency> +<dependency> + <groupId>org.apache.iceberg</groupId> + <artifactId>iceberg-gcp-bundle</artifactId> + <version>1.12.0</version> + <scope>runtime</scope> +</dependency> +{{< /runner >}} +{{< runner gradle >}} +dependencies { + runtimeOnly("org.apache.beam:beam-runners-google-cloud-dataflow-java:{{< param release_latest >}}") + runtimeOnly("org.apache.iceberg:iceberg-gcp:1.12.0") + runtimeOnly("org.apache.iceberg:iceberg-gcp-bundle:1.12.0") +} +{{< /runner >}} + +</section> + +{{< paragraph class="language-java language-py language-yaml" >}} +Set the following pipeline options. In this quickstart, do this for the +[**mirror pipeline**](#3-keep-a-table-in-sync-streaming): +{{< /paragraph >}} + +{{< highlight sql >}} +SET runner = DataflowRunner; +SET project = 'my-project'; +SET region = 'us-central1'; +SET tempLocation = 'gs://my-bucket/tmp'; +{{< /highlight >}} +{{< highlight java >}} +Pipeline p = + Pipeline.create( + PipelineOptionsFactory.fromArgs( + "--runner=DataflowRunner", + "--project=my-project", + "--region=us-central1", + "--tempLocation=gs://my-bucket/tmp") + .create()); +{{< /highlight >}} +{{< highlight py >}} +# pip install 'apache-beam[gcp]>=2.79.0' +from apache_beam.options.pipeline_options import PipelineOptions + +options = PipelineOptions( + runner="DataflowRunner", + project="my-project", + region="us-central1", + temp_location="gs://my-bucket/tmp", + streaming=True) +with beam.Pipeline(options=options) as p: + ... +{{< /highlight >}} +{{< highlight yaml >}} +# pip install 'apache-beam[gcp,yaml]>=2.79.0', then: +# python -m apache_beam.yaml.main --yaml_pipeline_file=mirror.yaml \ +# --runner=DataflowRunner --project=my-project --region=us-central1 \ Review Comment: Opted for the latter -- mentioning the options block again and telling users to set DataflowRunner there ########## website/www/site/content/en/blog/iceberg-cdc-sql.md: ########## @@ -0,0 +1,133 @@ +--- +title: "Stream changes between Iceberg tables with Apache Beam" +date: 2026-10-06 00:00:00 -0700 +categories: + - blog +authors: + - ahmedabu98 +--- +<!-- +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +--> + +You can now use Apache Beam to continuously **keep one Iceberg table in sync with another**. +Beam's Iceberg change data capture (CDC) [source](/blog/iceberg-cdc-source/) reads a table's row-level changes (inserts, updates, deletes), +and its merge-on-read [sink](/blog/iceberg-cdc-sink/) applies them downstream by primary key. Try it with this +**[quickstart](/documentation/io/built-in/iceberg-cdc/)**. + +Most engines only stream appends out of an Iceberg table. **Beam streams updates and deletes too.** + +<!--more--> + +It's common for teams to keep a backup copy of their Iceberg table. This backup might be a replica +in another region or different cloud provider, a copy in a new catalog they're migrating to, a +smaller table containing only the rows and columns the team needs, or a masked copy that hides +sensitive data from teams that shouldn't see it. **The source data isn't static**, +though. Orders get updated, erroneous records get fixed, and customers send GDPR requests to delete +their data. Consequently, you need a process to let these updates reach the downstream copy in +real-time. + +If you're in this situation, one option you have is to **reload the entire table periodically**. +However, this approach uses a lot of compute resources, and the downstream copy still becomes +out-of-sync between reload operations. Alternatively, you could **write your own custom pipeline** to +forward the change log downstream, but then you need to deal with all the mechanics yourself, such +as applying changes in the correct order, handling deletes, and recovering from a failure without +applying the same change twice. + +Beam's new Iceberg CDC source and sink handle these details for you. You just have to run +**one streaming pipeline** and it will keep the two tables continuously in sync. You also +have the option to run **periodic, sequential batch jobs**, each job picking up where the +last one left off. + +## What it looks like + +Consider an `orders` table that gets three new orders, followed by an update that +modifies order 1, deletes order 2, and adds order 4. Read as a stream of changes, these commits +look like this: + +<img class="center-block" src="/images/blog/iceberg-cdc/changes-live-view.gif" + alt="A Beam SQL live view of CHANGES(orders). Three inserts stream in from the first commit, + then the second commit's changes: order 1's old and new versions, a delete of order 2, and an + insert of order 4."> + +Each row is one change to one key. The `op` column describes what happened, and +`_commit_snapshot_sequence_number` says which commit it came from. You can see each commit's net +effect on each key, and an update shows up as two rows: the old version and the new one. You can +apply these commits to a downstream table to keep that table in sync. + +Here's a Beam SQL pipeline that applies commits to a downstream table continuously: + +```sql +INSERT INTO lakehouse.sales.orders_mirror + WITH ('mode' = 'merge-on-read', + 'change_type_column' = '_change_type', + 'triggering_frequency_seconds' = 15) +SELECT *, _change_type, _commit_snapshot_sequence_number +FROM CHANGES(lakehouse.sales.orders, 123) + WITH ('streaming' = true, 'poll_interval_seconds' = 5); +``` + +The `CHANGES` function reads `orders` as a stream of changes, starting at snapshot `123`. Specifying +`'streaming' = true` keeps the stream reading continuously as new snapshots are observed. + +On the write side, `'mode' = 'merge-on-read'` turns on the CDC sink, which applies each change to +`orders_mirror` by primary key, in 15-second commit windows. + +The [Iceberg CDC quickstart](/documentation/io/built-in/iceberg-cdc/) walks through a full +step-by-step example. + +### Masked copies and medallion architectures + +The `SELECT` can do more than copy. It can filter rows, drop columns and mask values, and the sink +still applies each change to the right key. So the same pipeline can keep a masked copy of a table +with personal data, or move changes through a medallion architecture, one pipeline per hop. This one +cleans bronze into silver: + +```sql +INSERT INTO lakehouse.silver.customers + WITH ('mode' = 'merge-on-read', + 'change_type_column' = '_change_type', + 'triggering_frequency_seconds' = 15) +SELECT id, name, + '***' || SUBSTRING(email FROM POSITION('@' IN email)) AS email, + country, _change_type, _commit_snapshot_sequence_number +FROM CHANGES(lakehouse.bronze.customers) + WITH ('streaming' = true) +WHERE country IS NOT NULL; +``` + +When a customer's data is deleted in bronze, the delete reaches silver too, and the next hop carries +it on to gold. + +## How it works + +Two shorter posts go into each half: + +* [Read an Iceberg table's change stream with Apache Beam](/blog/iceberg-cdc-source/) covers + how the source turns each snapshot into changes, and what Spark and Flink can't read yet. +* [Apply row-level changes to Iceberg with Apache Beam](/blog/iceberg-cdc-sink/) covers + how the sink orders each key's changes, and why it writes less than Flink's sink for keys that + change often. + +## Try it + +You can use the CDC sink feature starting with the Beam SDK 2.79.0 version. This works for table format v2 or v3. Review Comment: Done -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
