Skip to main content

Query Apache Iceberg tables with DuckDB

DuckDB reads Iceberg tables with the iceberg extension. Attach a REST catalog to query the tables you can access by name. If you have the metadata file for a specific table version, pass its path to iceberg_scan. Both routes need read access to the underlying files.

What you need​

SettingExampleWhere it comes from
Catalog URIhttps://catalog.example.com/api/catalogThe catalog's REST endpoint, without /v1
WarehouseanalyticsThe warehouse name or location the catalog expects
Catalog credentialA bearer token, or an OAuth client ID and secretYour catalog's admin console
Storage credentialAn S3 key, or none if the catalog vends credentialsYour cloud account

Use DuckDB 2.0 or later. Use a read-only token and storage key.

Attach a REST catalog​

INSTALL httpfs; LOAD httpfs; INSTALL iceberg; LOAD iceberg;

CREATE SECRET storage (TYPE s3, KEY_ID '<access_key_id>', SECRET '<secret_access_key>', REGION '<region>');
CREATE SECRET catalog (TYPE iceberg, TOKEN '<catalog_token>');

ATTACH '<warehouse>' AS lake (TYPE iceberg, ENDPOINT '<catalog_uri>', SECRET catalog);
SHOW ALL TABLES;
  • OAuth. For a client ID and secret, create the secret as (TYPE iceberg, CLIENT_ID '<client_id>', CLIENT_SECRET '<client_secret>', OAUTH2_SERVER_URI '<catalog_uri>/v1/oauth/tokens').
  • Vended credentials. If the catalog issues storage credentials, skip the storage secret.
  • S3-compatible storage. For Cloudflare R2, MinIO or another endpoint, add ENDPOINT '<host>' to the storage secret, URL_STYLE 'path' if the store needs path-style URLs, and USE_SSL false for a plain-HTTP endpoint.

Amazon S3 Tables​

CREATE SECRET storage (TYPE s3, PROVIDER credential_chain, REGION '<region>');

ATTACH 'arn:aws:s3tables:<region>:<account_id>:bucket/<table_bucket>' AS lake
(TYPE iceberg, ENDPOINT_TYPE s3_tables);

The credentials need s3tables:Get* and s3tables:List* on the table bucket and s3:GetObject on its data.

Read a table without a catalog​

To read a specific table version without attaching a catalog, pass its committed metadata file to iceberg_scan. Get the exact path from the catalog or the writer that committed that version.

SELECT count(*)
FROM iceberg_scan('s3://<bucket>/<table_path>/metadata/<version>.metadata.json');

A metadata file is a fixed version of the table: commits after it are invisible. Use a catalog when you need the latest data.

Snapshots and time travel​

Every Iceberg commit is a snapshot. List them, then query the table as of one:

SELECT snapshot_id, timestamp_ms, sequence_number
FROM iceberg_snapshots(lake.<namespace>.<table>)
ORDER BY sequence_number;

SELECT count(*) FROM lake.<namespace>.<table> AT (VERSION => <snapshot_id>);
SELECT count(*) FROM lake.<namespace>.<table> AT (TIMESTAMP => now() - INTERVAL 1 HOUR);

AT (TIMESTAMP => …) fails with Could not find snapshot at or before timestamp when the table has no snapshot that old. iceberg_metadata(lake.<namespace>.<table>) lists the data and delete files behind the current snapshot, with their record counts, which helps when a query is slower than expected.

Keep results​

DuckDB re-reads the table on every query. For a dashboard or notebook, copy what you need to Parquet or a local table:

COPY (SELECT * FROM lake.<namespace>.<table> WHERE <filter>) TO 'extract.parquet' (FORMAT parquet);

Where Send2Bucket fits​

Send2Bucket commits events, logs, spans and metrics to an Iceberg table in your REST catalog or S3 table bucket, one snapshot per delivery batch. The table has five columns, listed in Delivery format; records are JSON in payload:

SELECT signal_type, count(*) AS records
FROM lake.<namespace>.<table>
GROUP BY signal_type
ORDER BY records DESC;

SELECT payload::JSON->>'$.data.event' AS event,
count(*) AS events,
sum((payload::JSON->>'$.data.cents')::BIGINT) / 100 AS revenue_usd
FROM lake.<namespace>.<table>
WHERE signal_type = 'event'
GROUP BY event
ORDER BY events DESC;

DuckDB has queries for logs by service and the slowest spans, over a view that works for every destination.

Notes​

  • The REST catalog, iceberg_scan, snapshot, time travel, COPY and Send2Bucket snippets ran on DuckDB 2.0.1 against an Iceberg REST catalog and S3-compatible storage. The S3 Tables and OAuth variants follow the DuckDB Iceberg documentation and were not run.
  • Writes are possible through an attached catalog, but do not write to a table Send2Bucket owns.
  • To read the same table from other engines, see Snowflake, or Trino with an Iceberg REST catalog.