Script icon
If icon
SlackIncomingWebhook icon
Log icon
Labels icon
Schedule icon

DuckDB Iceberg Catalog Metadata Optimizer

Audits Apache Iceberg table snapshot metadata and manifest fragmentation with DuckDB, recommending maintenance actions via Slack.

Categories
CloudData

This blueprint acts as an automated data lakehouse optimization sentinel for Apache Iceberg tables using DuckDB. Scheduled weekly, it inspects Iceberg metadata.json manifests, tracks snapshot lineage accumulation, evaluates small manifest fragmentation, and delivers maintenance recommendations to Slack before planning latency degrades downstream analytical queries.

How it works

  1. Weekly Scheduled Trigger: The weekly_iceberg_metadata_audit trigger (io.kestra.plugin.core.trigger.Schedule) fires every Sunday at 04:00 UTC.
  2. DuckDB S3 Metadata Inspection: The inspect_iceberg_metadata task (io.kestra.plugin.scripts.python.Script) queries the table's metadata.json file on cloud object storage using DuckDB, extracting snapshot count, lineage length, and Iceberg format versions.
  3. Threshold Gate: The check_optimization_needs flowable task (io.kestra.plugin.core.flow.If) branches based on whether snapshot accumulation exceeded defined health thresholds.
  4. Slack Alert Dispatch: When snapshot bloat is detected, notify_slack_lakehouse (io.kestra.plugin.slack.notifications.SlackIncomingWebhook) dispatches maintenance recommendations to the lakehouse team.
  5. Nominal Confirmation: When metadata remains lean, log_lakehouse_nominal records confirmed status.
  6. Manifest Export: The export_lakehouse_manifest task tags the execution with searchable status labels.

Architecture diagram

flowchart TD
    A[Schedule: Sunday 04:00 UTC] --> B[inspect_iceberg_metadata: DuckDB S3 read_json_auto]
    B --> C{Snapshots > Threshold?}
    C -- Yes --> D[notify_slack_lakehouse: Slack Webhook]
    C -- No --> E[log_lakehouse_nominal: Log]
    D --> F[export_lakehouse_manifest: Execution Labels]
    E --> F

Use cases

  • Prevent Iceberg Metadata Latency Degradation: Prevent accumulated commit snapshots from swelling metadata.json into multi-megabyte files that slow query planning in Trino, DuckDB, or Athena.
  • Automate Snapshot Expiration Guidance: Alert data platform engineers when automated jobs need to run CALL system.expire_snapshots().
  • Cloud Storage Cost Governance: Eliminate orphan data and manifest files retained across obsolete historical snapshots.

Inputs

Name Type Default Description
iceberg_table_metadata_path STRING s3://lakehouse-data/tables/orders/metadata/v15.metadata.json S3 or cloud storage URI to the Apache Iceberg table metadata.json file.
max_snapshot_count_warning INT 50 Maximum permissible number of active table snapshots before alerting for snapshot expiration.
slack_channel STRING #lakehouse-alerts Slack channel destination for Iceberg metadata optimization alerts.

Expected outputs

  • {{ outputs.inspect_iceberg_metadata.vars.total_active_snapshots }}: Number of active snapshots in the Iceberg table metadata.
  • {{ outputs.inspect_iceberg_metadata.vars.iceberg_format_version }}: Table format version (e.g. 1 or 2).
  • {{ outputs.inspect_iceberg_metadata.vars.needs_optimization }}: Boolean flag indicating maintenance necessity.
  • {{ outputs.inspect_iceberg_metadata.outputFiles['iceberg_metadata_report.json'] }}: Full JSON metadata diagnostics payload.

Prerequisites

  • AWS S3 IAM credentials with read access to the target Iceberg data lake bucket.
  • Incoming Webhook URL configured for your data platform Slack channel.

Secrets

  • AWS_ACCESS_KEY_ID: AWS IAM access key ID.
  • AWS_SECRET_ACCESS_KEY: AWS IAM secret access key.
  • SLACK_WEBHOOK_URL: Slack Incoming Webhook endpoint URL.

Quick start

  1. Configure AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and SLACK_WEBHOOK_URL in your Kestra namespace secrets.
  2. Import this flow YAML into your Kestra workspace.
  3. Specify your Iceberg metadata file URI in iceberg_table_metadata_path.
  4. Click Execute in the UI to perform an immediate Iceberg table metadata health check.

Common pitfalls and troubleshooting

  • S3 Region Configuration: Ensure s3_region in DuckDB matches the bucket region to prevent HTTP 301 Moved Permanently redirects.
  • Metadata File Naming: In high-frequency streaming write workloads, locate the newest metadata.json via version numbering or Iceberg catalog pointers (e.g. Nessie, Polaris, AWS Glue).

How to extend

  • Add automated execution of Apache Spark or PyIceberg rewrite_data_files and rewrite_manifests tasks directly within Kestra.
  • Query Iceberg catalog APIs (e.g. Apache Polaris or AWS Glue) to iterate across all tables in a database namespace.
  • Route storage reclaim summaries to FinOps reporting dashboards.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.