Skip to main content

Overview

To write to Iceberg tables in Spark, first configure Spark catalogs. Some write operations require Iceberg SQL extensions.

Feature Support

Writing with SQL

INSERT INTO

Append new data to a table:

MERGE INTO

Perform upserts and conditional updates:
MERGE INTO is recommended over INSERT OVERWRITE because Iceberg only replaces affected data files, and dynamic overwrite behavior can change if partitioning changes.
Only one source record can update any target row. Multiple matches will cause an error.

Snapshot Summary (Spark 4.1+)

MERGE INTO operations provide detailed metrics in snapshot summaries:

INSERT OVERWRITE

Replace table or partition data:
Dynamic overwrite mode is recommended for Iceberg tables. Static mode requires a PARTITION clause that can only reference table columns, not hidden partitions.

DELETE FROM

Remove rows matching a filter:
If the filter matches entire partitions, Iceberg performs a metadata-only delete. Otherwise, only affected data files are rewritten.

UPDATE

Update rows matching a filter:

Writing to Branches

Write to specific branches for Write-Audit-Publish (WAP) workflows:
The branch must exist before writing. Use CREATE BRANCH to create branches.

Writing with DataFrames

The DataFrameWriterV2 API is recommended for DataFrame writes:

Appending Data

Overwriting Data

Creating Tables

Merging Data (Spark 4.0+)

Writing to Branches

When using the v1 DataFrame API, use saveAsTable or insertInto instead of format("iceberg") to ensure proper catalog integration.

Schema Merge

Enable automatic schema evolution during writes:
1

Enable Schema Evolution

Configure the table to accept schema changes:
2

Enable mergeSchema Option

Enable the merge option in your write:
Schema merge behavior:
  • New columns in source are added to target (set to NULL for existing rows)
  • Missing columns in source are set to NULL (insert) or unchanged (update)

Distribution Modes

Control how Spark distributes data before writing:
The hash distribution mode (new default in Iceberg 1.2.0) automatically sorts data by partition value, eliminating the need for manual sorting in most cases.

Controlling File Sizes

Target File Size

Control output file sizes with table properties:

Spark Task Size

Adjust Spark’s Adaptive Query Execution (AQE) to control task sizes:
The in-memory Spark row size is larger than on-disk columnar-compressed size. A larger AQE partition size than the target file size is typically needed.

Fanout Writers

For streaming workloads on partitioned tables:
Fanout writers keep files open per partition value until the write task finishes. Use only for streaming; not recommended for batch writes.

Write Options

Common write options for DataFrameWriterV2:

Example with Options

Next Steps

Procedures

Maintain tables with compaction and cleanup

Configuration

Configure write performance and behavior

Structured Streaming

Stream data with continuous writes

DDL Operations

Create and alter table schemas