The following example uses Polars ↗︎ to create, read, and query an Iceberg table in Basin Catalog. For more information on Polars and Iceberg, refer to the Polars documentation ↗︎.
- Sign up for a Cloudflare account ↗︎.
- Create an R2 bucket and enable Basin Catalog on it.
- Install Polars ↗︎ 2.2.0 or later with the
icebergextra:pip install 'polars[iceberg]'.
import polars as pl
from pyiceberg.catalog.rest import RestCatalog
# Connect to Basin Catalog (replace the placeholders)
catalog = RestCatalog(
name="my_catalog",
warehouse="<WAREHOUSE>",
uri="<CATALOG_URI>",
token="<TOKEN>",
)
# Create an Iceberg table (and the namespace) from a Polars DataFrame
df = pl.DataFrame({
"id": [1, 2, 3],
"name": ["Alice", "Bob", "Charlie"],
})
df.lazy().sink_iceberg(
"default.my_table",
catalog=catalog,
mode="append",
create_if_not_exists=True,
create_namespace_if_not_exists=True,
)
# Read the table back lazily and filter it
print(
pl.scan_iceberg("default.my_table", catalog=catalog)
.filter(pl.col("id") > 1)
.collect()
)
# Or query it with SQL
ctx = pl.SQLContext()
ctx.register_catalog("my_catalog", catalog, default_namespace="default", default=True)
print(ctx.execute("SELECT name FROM my_table WHERE id = 2").collect())