Apache Iceberg is an open table format designed for big data workloads. It enables ACID transactions, schema evolution, and efficient querying for large datasets. Working with Iceberg tables traditionally required a heavy setup, such as a Spark cluster or a Trino engine. PyIceberg changes that by providing a lightweight Python-native API for working with Iceberg tables without needing Spark or Flink for basic operations.
This guide walks you through setting up PyIceberg, configuring catalogs, creating tables, querying data, and managing schema evolution.
Why Use PyIceberg?
As Python is one of the most popular programming languages for data engineering, PyIceberg offers a convenient and accessible way to work with Iceberg. It provides a simple and intuitive API that allows developers to create, read, update, and delete data in Iceberg tables and perform schema evolution and partitioning operations.
With PyIceberg, you can leverage the power of Iceberg’s features without the complexity of setting up and managing a distributed processing cluster. This makes it ideal for Python developers who want to integrate Iceberg easily into their data pipelines and applications.
Advantages of PyIceberg
Lightweight and Modular: Unlike Spark or Flink, PyIceberg does not require a distributed processing engine to interact with Iceberg tables.
Pythonic API: Designed for Python developers, PyIceberg provides a straightforward API to manage Iceberg tables.
Interoperability: Works seamlessly with query engines like DuckDB, making ad-hoc querying much easier.
Schema Evolution and Partitioning: PyIceberg fully supports schema evolution and flexible partitioning strategies, allowing incremental changes without table recreation.
Decoupled Storage and Compute: Iceberg’s design allows storage and compute to scale independently, making it more cost-efficient than traditional data warehouses.
Comparing PyIceberg to Alternatives
Feature
PyIceberg
Apache Spark
Trino/Presto
DuckDB
Lightweight
✅ Yes
❌ No
❌ No
✅ Yes
Schema Evolution
✅ Yes
✅ Yes
✅ Yes
❌ No
ACID Transactions
✅ Yes
✅ Yes
✅ Yes
❌ No
Query Engine Dependency
❌ No
✅ Yes
✅ Yes
❌ No
Supports Iceberg Tables
✅ Yes
✅ Yes
✅ Yes
❌ No (extension)
1. Setting Up PyIceberg
Before we start querying Iceberg tables, we need to install and configure PyIceberg.
Installing PyIceberg
We can install PyIceberg using pip, including necessary extras for SQL-based catalogs and local file storage:
pipinstall"pyiceberg[sql-sqlite,pyarrow]"
pipinstall"pyiceberg[sql-sqlite,pyarrow]"
pipinstall"pyiceberg[sql-sqlite,pyarrow]"
pipinstall"pyiceberg[sql-sqlite,pyarrow]"
pipinstall"pyiceberg[sql-sqlite,pyarrow]"
sql-sqlite enables using SQLite as a lightweight metadata catalog for Iceberg tables.
pyarrow provides support for working with Iceberg tables using Apache Arrow, enabling efficient in-memory data operations.
2. Configuring an Iceberg Catalog
Creating a Storage Directory
Iceberg tables require a catalog to store metadata. For this example, we’ll create a local SQLite-backed Iceberg catalog and persist data on the filesystem.
mkdir -piceberg_catalog
mkdir -piceberg_catalog
mkdir -piceberg_catalog
mkdir -piceberg_catalog
mkdir -piceberg_catalog
Configuring PyIceberg
Next, we define the catalog configuration in a .pyiceberg.yaml file:
PyIceberg provides powerful querying capabilities for Iceberg tables, enabling flexible data exploration using predicate filtering and column selection. Let’s walk through some practical examples.
1. Query By Name (String Matching)
Suppose we want to retrieve all records where the name column contains “metric”. We can use the StartsWith expression.
frompyiceberg.expressionsimportStartsWith# Query rows where the 'name' column starts with 'metric'result = iceberg_table \
.scan(row_filter=StartsWith('name','metric')) \
.to_arrow()print(result.to_string(preview_cols=10))
frompyiceberg.expressionsimportStartsWith# Query rows where the 'name' column starts with 'metric'result = iceberg_table \
.scan(row_filter=StartsWith('name','metric')) \
.to_arrow()print(result.to_string(preview_cols=10))
frompyiceberg.expressionsimportStartsWith# Query rows where the 'name' column starts with 'metric'result = iceberg_table \
.scan(row_filter=StartsWith('name','metric')) \
.to_arrow()print(result.to_string(preview_cols=10))
frompyiceberg.expressionsimportStartsWith# Query rows where the 'name' column starts with 'metric'result = iceberg_table \
.scan(row_filter=StartsWith('name','metric')) \
.to_arrow()print(result.to_string(preview_cols=10))
frompyiceberg.expressionsimportStartsWith# Query rows where the 'name' column starts with 'metric'result = iceberg_table \
.scan(row_filter=StartsWith('name','metric')) \
.to_arrow()print(result.to_string(preview_cols=10))
Let’s assume our table has a column created_at that stores timestamps. We can retrieve all records created in the last 7 days using GreaterThan.
frompyiceberg.expressionsimportGreaterThanimportdatetime# Get timestamp for 7 days agoseven_days_ago = (datetime.datetime.utcnow() - datetime.timedelta(days=7)).isoformat()# Query rows where 'created_at' is greater than seven days agoresult = iceberg_table \
.scan(row_filter=GreaterThan('created_at',seven_days_ago)) \
.to_arrow()print(result.to_string(preview_cols=10))
frompyiceberg.expressionsimportGreaterThanimportdatetime# Get timestamp for 7 days agoseven_days_ago = (datetime.datetime.utcnow() - datetime.timedelta(days=7)).isoformat()# Query rows where 'created_at' is greater than seven days agoresult = iceberg_table \
.scan(row_filter=GreaterThan('created_at',seven_days_ago)) \
.to_arrow()print(result.to_string(preview_cols=10))
frompyiceberg.expressionsimportGreaterThanimportdatetime# Get timestamp for 7 days agoseven_days_ago = (datetime.datetime.utcnow() - datetime.timedelta(days=7)).isoformat()# Query rows where 'created_at' is greater than seven days agoresult = iceberg_table \
.scan(row_filter=GreaterThan('created_at',seven_days_ago)) \
.to_arrow()print(result.to_string(preview_cols=10))
frompyiceberg.expressionsimportGreaterThanimportdatetime# Get timestamp for 7 days agoseven_days_ago = (datetime.datetime.utcnow() - datetime.timedelta(days=7)).isoformat()# Query rows where 'created_at' is greater than seven days agoresult = iceberg_table \
.scan(row_filter=GreaterThan('created_at',seven_days_ago)) \
.to_arrow()print(result.to_string(preview_cols=10))
frompyiceberg.expressionsimportGreaterThanimportdatetime# Get timestamp for 7 days agoseven_days_ago = (datetime.datetime.utcnow() - datetime.timedelta(days=7)).isoformat()# Query rows where 'created_at' is greater than seven days agoresult = iceberg_table \
.scan(row_filter=GreaterThan('created_at',seven_days_ago)) \
.to_arrow()print(result.to_string(preview_cols=10))
3. Query with Multiple Conditions (Category & Value Filtering)
Imagine we have a category column, and we want to retrieve all records where category is “A” and value is greater than 20.
frompyiceberg.expressionsimportAnd,EqualTo,GreaterThan# Query rows where 'category' is "A" and 'value' is greater than 20result = iceberg_table \
.scan(row_filter=And(EqualTo('category','A'),GreaterThan('value',20))) \
.to_arrow()print(result.to_string(preview_cols=10))
frompyiceberg.expressionsimportAnd,EqualTo,GreaterThan# Query rows where 'category' is "A" and 'value' is greater than 20result = iceberg_table \
.scan(row_filter=And(EqualTo('category','A'),GreaterThan('value',20))) \
.to_arrow()print(result.to_string(preview_cols=10))
frompyiceberg.expressionsimportAnd,EqualTo,GreaterThan# Query rows where 'category' is "A" and 'value' is greater than 20result = iceberg_table \
.scan(row_filter=And(EqualTo('category','A'),GreaterThan('value',20))) \
.to_arrow()print(result.to_string(preview_cols=10))
frompyiceberg.expressionsimportAnd,EqualTo,GreaterThan# Query rows where 'category' is "A" and 'value' is greater than 20result = iceberg_table \
.scan(row_filter=And(EqualTo('category','A'),GreaterThan('value',20))) \
.to_arrow()print(result.to_string(preview_cols=10))
frompyiceberg.expressionsimportAnd,EqualTo,GreaterThan# Query rows where 'category' is "A" and 'value' is greater than 20result = iceberg_table \
.scan(row_filter=And(EqualTo('category','A'),GreaterThan('value',20))) \
.to_arrow()print(result.to_string(preview_cols=10))
4. Retrieve Specific Columns for Performance Optimization
Instead of scanning the entire dataset, select only the necessary columns to improve performance.
frompyiceberg.expressionsimportEqualTo# Retrieve only 'id' and 'value' for records where 'id' = 3result = iceberg_table \
.scan(row_filter=EqualTo('id',3),selected_fields=['id','value']) \
.to_arrow()print(result.to_string(preview_cols=10))
frompyiceberg.expressionsimportEqualTo# Retrieve only 'id' and 'value' for records where 'id' = 3result = iceberg_table \
.scan(row_filter=EqualTo('id',3),selected_fields=['id','value']) \
.to_arrow()print(result.to_string(preview_cols=10))
frompyiceberg.expressionsimportEqualTo# Retrieve only 'id' and 'value' for records where 'id' = 3result = iceberg_table \
.scan(row_filter=EqualTo('id',3),selected_fields=['id','value']) \
.to_arrow()print(result.to_string(preview_cols=10))
frompyiceberg.expressionsimportEqualTo# Retrieve only 'id' and 'value' for records where 'id' = 3result = iceberg_table \
.scan(row_filter=EqualTo('id',3),selected_fields=['id','value']) \
.to_arrow()print(result.to_string(preview_cols=10))
frompyiceberg.expressionsimportEqualTo# Retrieve only 'id' and 'value' for records where 'id' = 3result = iceberg_table \
.scan(row_filter=EqualTo('id',3),selected_fields=['id','value']) \
.to_arrow()print(result.to_string(preview_cols=10))
5. Find Null or Missing Values
If a dataset has missing or null values in a column, we can use IsNull to find them.
frompyiceberg.expressionsimportIsNull# Find rows where 'value' is NULLresult = iceberg_table \
.scan(row_filter=IsNull('value')) \
.to_arrow()print(result.to_string(preview_cols=10))
frompyiceberg.expressionsimportIsNull# Find rows where 'value' is NULLresult = iceberg_table \
.scan(row_filter=IsNull('value')) \
.to_arrow()print(result.to_string(preview_cols=10))
frompyiceberg.expressionsimportIsNull# Find rows where 'value' is NULLresult = iceberg_table \
.scan(row_filter=IsNull('value')) \
.to_arrow()print(result.to_string(preview_cols=10))
frompyiceberg.expressionsimportIsNull# Find rows where 'value' is NULLresult = iceberg_table \
.scan(row_filter=IsNull('value')) \
.to_arrow()print(result.to_string(preview_cols=10))
frompyiceberg.expressionsimportIsNull# Find rows where 'value' is NULLresult = iceberg_table \
.scan(row_filter=IsNull('value')) \
.to_arrow()print(result.to_string(preview_cols=10))
6. Query Using a List of IDs (IN Clause Alternative)
To retrieve records matching multiple IDs at once, use In.
frompyiceberg.expressionsimportIn# Query rows where 'id' is in [2, 4, 6]result = iceberg_table \
.scan(row_filter=In('id',[2,4,6])) \
.to_arrow()print(result.to_string(preview_cols=10))
frompyiceberg.expressionsimportIn# Query rows where 'id' is in [2, 4, 6]result = iceberg_table \
.scan(row_filter=In('id',[2,4,6])) \
.to_arrow()print(result.to_string(preview_cols=10))
frompyiceberg.expressionsimportIn# Query rows where 'id' is in [2, 4, 6]result = iceberg_table \
.scan(row_filter=In('id',[2,4,6])) \
.to_arrow()print(result.to_string(preview_cols=10))
frompyiceberg.expressionsimportIn# Query rows where 'id' is in [2, 4, 6]result = iceberg_table \
.scan(row_filter=In('id',[2,4,6])) \
.to_arrow()print(result.to_string(preview_cols=10))
frompyiceberg.expressionsimportIn# Query rows where 'id' is in [2, 4, 6]result = iceberg_table \
.scan(row_filter=In('id',[2,4,6])) \
.to_arrow()print(result.to_string(preview_cols=10))
This example updates the updated_at column for the row where id = 4. The new value is the current UTC timestamp in ISO 8601 format.
We use pa.string() to ensure compatibility if the column is stored as a string. If it’s stored as a timestamp, we would use pa.timestamp(‘us’).
importpyarrowaspafrompyiceberg.expressionsimportEqualToimportdatetime# Query for the row to update.filtered_table = iceberg_table \
.scan(row_filter=EqualTo('id',4)) \
.to_arrow()# Get the index of the 'updated_at' column and retrieve its fieldupdated_at_col_index = filtered_table.column_names.index('updated_at')updated_at_col_field = filtered_table.field(updated_at_col_index)# Generate the current timestamp in ISO 8601 formatcurrent_utc_timestamp = datetime.datetime.utcnow().isoformat()# Replace the 'updated_at' column with the new timestamp valueupdated_table = filtered_table.set_column(updated_at_col_index,updated_at_col_field,pa.array([current_utc_timestamp],type=pa.string())# Ensure the data type aligns)# Overwrite the existing row in the Iceberg tableiceberg_table.overwrite(df=updated_table,overwrite_filter=EqualTo('id',4))
importpyarrowaspafrompyiceberg.expressionsimportEqualToimportdatetime# Query for the row to update.filtered_table = iceberg_table \
.scan(row_filter=EqualTo('id',4)) \
.to_arrow()# Get the index of the 'updated_at' column and retrieve its fieldupdated_at_col_index = filtered_table.column_names.index('updated_at')updated_at_col_field = filtered_table.field(updated_at_col_index)# Generate the current timestamp in ISO 8601 formatcurrent_utc_timestamp = datetime.datetime.utcnow().isoformat()# Replace the 'updated_at' column with the new timestamp valueupdated_table = filtered_table.set_column(updated_at_col_index,updated_at_col_field,pa.array([current_utc_timestamp],type=pa.string())# Ensure the data type aligns)# Overwrite the existing row in the Iceberg tableiceberg_table.overwrite(df=updated_table,overwrite_filter=EqualTo('id',4))
importpyarrowaspafrompyiceberg.expressionsimportEqualToimportdatetime# Query for the row to update.filtered_table = iceberg_table \
.scan(row_filter=EqualTo('id',4)) \
.to_arrow()# Get the index of the 'updated_at' column and retrieve its fieldupdated_at_col_index = filtered_table.column_names.index('updated_at')updated_at_col_field = filtered_table.field(updated_at_col_index)# Generate the current timestamp in ISO 8601 formatcurrent_utc_timestamp = datetime.datetime.utcnow().isoformat()# Replace the 'updated_at' column with the new timestamp valueupdated_table = filtered_table.set_column(updated_at_col_index,updated_at_col_field,pa.array([current_utc_timestamp],type=pa.string())# Ensure the data type aligns)# Overwrite the existing row in the Iceberg tableiceberg_table.overwrite(df=updated_table,overwrite_filter=EqualTo('id',4))
importpyarrowaspafrompyiceberg.expressionsimportEqualToimportdatetime# Query for the row to update.filtered_table = iceberg_table \
.scan(row_filter=EqualTo('id',4)) \
.to_arrow()# Get the index of the 'updated_at' column and retrieve its fieldupdated_at_col_index = filtered_table.column_names.index('updated_at')updated_at_col_field = filtered_table.field(updated_at_col_index)# Generate the current timestamp in ISO 8601 formatcurrent_utc_timestamp = datetime.datetime.utcnow().isoformat()# Replace the 'updated_at' column with the new timestamp valueupdated_table = filtered_table.set_column(updated_at_col_index,updated_at_col_field,pa.array([current_utc_timestamp],type=pa.string())# Ensure the data type aligns)# Overwrite the existing row in the Iceberg tableiceberg_table.overwrite(df=updated_table,overwrite_filter=EqualTo('id',4))
importpyarrowaspafrompyiceberg.expressionsimportEqualToimportdatetime# Query for the row to update.filtered_table = iceberg_table \
.scan(row_filter=EqualTo('id',4)) \
.to_arrow()# Get the index of the 'updated_at' column and retrieve its fieldupdated_at_col_index = filtered_table.column_names.index('updated_at')updated_at_col_field = filtered_table.field(updated_at_col_index)# Generate the current timestamp in ISO 8601 formatcurrent_utc_timestamp = datetime.datetime.utcnow().isoformat()# Replace the 'updated_at' column with the new timestamp valueupdated_table = filtered_table.set_column(updated_at_col_index,updated_at_col_field,pa.array([current_utc_timestamp],type=pa.string())# Ensure the data type aligns)# Overwrite the existing row in the Iceberg tableiceberg_table.overwrite(df=updated_table,overwrite_filter=EqualTo('id',4))
Deleting Data from an Iceberg Table
Let’s take a look at how we can delete a record where the id field is equal to 1.
PyIceberg simplifies working with Apache Iceberg by providing a Python-native API without needing Spark or Trino. You can manage Iceberg tables, insert data with PySpark, and query efficiently with DuckDB. Compared to DuckDB, Iceberg provides better support for large-scale data processing, schema evolution, and ACID compliance, making it ideal for long-term data storage and processing needs.
Dani is a data professional with a rich background in data engineering and real-time data platforms. At Estuary, Daniel focuses on promoting cutting-edge streaming solutions, helping to bridge the gap between technical innovation and developer adoption. With deep expertise in cloud-native and streaming technologies, Dani has successfully supported startups and enterprises in building robust data solutions.