Skip to main content
Apache Iceberg provides maintenance operations for Flink to optimize table performance and manage storage efficiently.

Batch Mode Maintenance

Rewrite Files Action

Iceberg provides an API to rewrite small files into larger files by submitting Flink batch jobs. The behavior is the same as Spark’s rewriteDataFiles action.
For more details, see RewriteDataFilesAction.

Streaming Mode Maintenance

Overview

The TableMaintenance API in Apache Iceberg empowers Flink jobs to execute maintenance tasks natively, either embedded within existing streaming pipelines or deployed as standalone Flink jobs. This eliminates dependencies on external systems like Spark. Benefits:
  • Streamlined architecture
  • Reduced operational costs
  • Enhanced automation capabilities
  • No dependency on Spark infrastructure

Supported Features

ExpireSnapshots

Removes old snapshots and their files. Internally uses cleanExpiredFiles(true) when committing.

RewriteDataFiles

Compacts small files to optimize file sizes. Supports partial progress commits and limiting maximum rewritten bytes per run.

DeleteOrphanFiles

Removes files which are not referenced in any metadata files of an Iceberg table.

Lock Management

The TriggerLockFactory is essential for coordinating maintenance tasks. It prevents concurrent maintenance operations on the same table.
Why locks are needed:
  • Prevents concurrent access conflicts
  • Ensures data consistency
  • Manages resources effectively
  • Avoids duplicate work even with a single job

JDBC Lock Factory

Uses a database table to manage distributed locks:

ZooKeeper Lock Factory

Uses Apache ZooKeeper for distributed locks:

Quick Start Example

The following example demonstrates automated maintenance for an Iceberg table:

Configuration Options

TableMaintenance Builder

Common Task Options

ExpireSnapshots Options

RewriteDataFiles Options

DeleteOrphanFiles Options

Post-Commit Integration

Automatic execution of maintenance tasks after data is committed using addPostCommitTopology(...):

SQL Examples

Enable maintenance using SQL:
Or specify options in table DDL:

Best Practices

Resource Management

  • Use dedicated slot sharing groups for maintenance tasks
  • Set appropriate parallelism based on cluster resources
  • Enable checkpointing for fault tolerance

Scheduling Strategy

  • Avoid too frequent executions with rateLimit
  • Use scheduleOnCommitCount for write-heavy tables
  • Use scheduleOnDataFileCount for fine-grained control

Performance Tuning

  • Adjust deleteBatchSize based on storage performance
  • Enable partialProgressEnabled for large rewrite operations
  • Set reasonable maxRewriteBytes limits
  • Set appropriate maxFileGroupSizeBytes for parallel processing

Troubleshooting

OutOfMemoryError during file deletion

Cause: Large number of files being deleted in a single batch. Solution: Reduce the batch size:

Lock conflicts

Cause: Multiple jobs attempting maintenance simultaneously. Solution: Increase lock check delay and rate limit:

Slow rewrite operations

Cause: Too much data being rewritten in a single run. Solution: Enable partial progress and limit bytes:

Complete Production Example

Next Steps

Configuration

Configure Flink for Iceberg

Writes

Learn about writing data with Flink