ahmedabu98 commented on code in PR #41584: URL: https://github.com/apache/beam/pull/41584#discussion_r4687359598
########## website/www/site/content/en/blog/iceberg-cdc-sink.md: ########## @@ -0,0 +1,209 @@ +--- +title: "Apply row-level changes to Iceberg with Apache Beam" +date: 2026-11-22 00:00:00 -0800 +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 **apply a stream of row-level changes (inserts, updates, deletes) to an Iceberg +table** by primary key. The merge-on-read sink works in streaming and batch, and it takes changes +from any source: another Iceberg table, a database's change log, or a Kafka topic. Try it with +this **[quickstart](/documentation/io/built-in/iceberg-cdc/)**. + +<!--more--> + +Applying changes to a table has a few hard parts. The changes have to land in the order they +happened. Deletes have to find the rows they remove. And if a commit is retried, or the job +restarts, the same change can't be applied twice. **Beam's CDC sink handles these details for +you.** + +## What the sink expects + +The sink takes a collection of change rows. Besides the table's own columns, each row carries +two more: + +- the change type: `INSERT`, `UPDATE_BEFORE`, `UPDATE_AFTER` or `DELETE` +- a sequence number that goes up with each change to the same key. Both halves of an update share + one. + +Here's a pipeline that applies those changes to `sales.orders`: + +{{< highlight sql >}} +INSERT INTO lakehouse.sales.orders + WITH ('mode' = 'merge-on-read', + 'change_type_column' = 'op', + 'sequence_number_column' = 'seq', + 'triggering_frequency_seconds' = 10) +SELECT id, item, qty, op, seq FROM order_changes; +{{< /highlight >}} +{{< highlight java >}} +Map<String, Object> config = + Map.of( + "catalog_properties", catalogProperties, + "table", "sales.orders", + "mode", "merge-on-read", + "equality_columns", List.of("id"), + "change_type_column", "op", + "sequence_number_column", "seq", + "triggering_frequency_seconds", 10); + +changes.apply(Managed.write(Managed.ICEBERG).withConfig(config)); +{{< /highlight >}} +{{< highlight py >}} +changes | beam.managed.Write(beam.managed.ICEBERG, config={ + "catalog_properties": {...}, + "table": "sales.orders", + "mode": "merge-on-read", + "equality_columns": ["id"], + "change_type_column": "op", + "sequence_number_column": "seq", + "triggering_frequency_seconds": 10, +}) +{{< /highlight >}} +{{< highlight yaml >}} +- type: WriteToIceberg + input: Changes + config: + catalog_properties: {...} + table: sales.orders + mode: merge-on-read + equality_columns: [id] + change_type_column: op + sequence_number_column: seq + triggering_frequency_seconds: 10 +{{< /highlight >}} + +Setting `mode` to `merge-on-read` turns on the CDC sink. `equality_columns` is the primary key, +and the sink uses it to create the table if it doesn't exist yet. In SQL, the table has to exist +already, and the key comes from its `PRIMARY KEY`. The sink drops the `op` and `seq` columns before +writing to the table. + +If your changes use their own labels, like Debezium's `c`, `u` and `d`, map them with +`change_type_map`. If your source doesn't send a before-image for each update, set `upsert` to +`true`, and the sink applies inserts and updates as upserts. For a Debezium stream, that looks +like this: + +{{< highlight sql >}} + WITH (..., + 'change_type_map' = MAP['c', 'INSERT', + 'u', 'UPDATE_AFTER', + 'd', 'DELETE'], + 'upsert' = true) +{{< /highlight >}} +{{< highlight java >}} +Map<String, Object> config = + Map.of( + ..., + "change_type_map", + Map.of("c", "INSERT", "u", "UPDATE_AFTER", "d", "DELETE"), + "upsert", true); +{{< /highlight >}} +{{< highlight py >}} +config={ + ... + "change_type_map": {"c": "INSERT", "u": "UPDATE_AFTER", "d": "DELETE"}, + "upsert": True, +} +{{< /highlight >}} +{{< highlight yaml >}} +config: + ... + change_type_map: {c: INSERT, u: UPDATE_AFTER, d: DELETE} + upsert: true +{{< /highlight >}} + +If your changes come from Beam's [Iceberg CDC source](/blog/iceberg-cdc-source/), you only need +to point `change_type_column` at `_change_type`. Review Comment: Updated the language to just mention sequence_number_column -- 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]
