Using Spark to Read and Write Iceberg in Streaming Mode
Iceberg uses Apache Spark's DataSourceV2 API for data source and catalog implementations. Spark DSv2 is an evolving API, and its support varies across different Spark versions.
Streaming Read
Iceberg can read incremental data from a specified historical timestamp in Spark structured stream processing tasks.
val df = spark.readStream
.format("iceberg")
.option("stream-from-timestamp", Long.toString(streamStartTimestamp))
.load("database.table_name") Iceberg supports reading data only from append snapshots. Overwrite snapshots cannot be processed and an exception is thrown by default. In this case, you can set streaming-skip-overwrite-snapshots to true to ignore the overwrite operation. Similarly, an exception is thrown by default for delete snapshots. You can set streaming-skip-delete-snapshots to true to ignore the delete operation.
Streaming Write
To write the results of a streaming query to an Iceberg table, use DataStreamWriter:
data.writeStream
.format("iceberg")
.outputMode("append")
.trigger(Trigger.ProcessingTime(1, TimeUnit.MINUTES))
.option("checkpointLocation", checkpointPath)
.toTable("database.table_name") Iceberg supports the following output modes:
- append: Appends the rows of each micro-batch to a table.
- complete: Replaces the content of the entire table with the content of each micro-batch.
Before starting a streaming query, ensure that a table has been created. For details about how to create an Iceberg table, see the SQL table creation documentation.
Iceberg does not support an experimental continuous processing mode because it does not provide an interface for committing output.
Partitioned Tables
Iceberg requires that the data of each task be sorted by partition before being written. In Spark, tasks are divided by Spark partition. Explicit sorting is recommended for batch queries, but this approach may increase latency because repartitioning and sorting are heavyweight operations for streaming workloads. In this case, you can enable the fanout writer to avoid unnecessary delay.
data.writeStream
.format("iceberg")
.outputMode("append")
.trigger(Trigger.ProcessingTime(1, TimeUnit.MINUTES))
.option("fanout-enabled", "true")
.option("checkpointLocation", checkpointPath)
.toTable("database.table_name") The fanout writer opens a file for each partition value and does not close these files until the write task is complete. Avoid using the fanout writer in batch write operations, as explicitly sorting output rows is less costly for batch workloads.
Maintaining Streaming Tables
Streaming writes quickly create new table versions, generating a large amount of table metadata to track these versions. It is highly recommended to maintain metadata by adjusting the commit frequency, expiring old snapshots, and automatically cleaning up metadata files.
Adjusting the Commit Frequency
High-frequency commits generate data files, manifest files, and snapshots, increasing maintenance overhead. It is recommended to set at least a 1-minute interval for commits and increase it as needed.
Expiring Old Snapshots
Each batch written to the table generates a new snapshot. Iceberg tracks snapshots in the table metadata until they expire. Frequent commits may lead to snapshot stacking, so you are strongly advised to periodically maintain the tables to which streaming queries write. Snapshot expiration is the process of removing metadata and any data files that are no longer needed. By default, this process expires snapshots older than five days.
Merging Small Files
Streaming writes typically involve small amounts of data, which causes table metadata to track a large number of small files. Merging small files into larger ones reduces the metadata required by the table and improves query efficiency. Iceberg and Spark provide the rewrite_data_files stored procedure for this purpose.
Rewriting Manifest Files
To optimize write latency for streaming workloads, Iceberg can write new snapshots in "fast append" mode without automatically merging manifest files. This may result in a large number of small manifest files. Iceberg can rewrite the number of manifest files to improve query performance. To achieve this, Iceberg and Spark provide the rewrite_manifests stored procedure.
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot