From 6181acbcb5c010ad967b3b0a4b7923175565933e Mon Sep 17 00:00:00 2001 From: Yong Date: Mon, 25 May 2026 13:47:08 -0500 Subject: [PATCH 1/2] Add more examples for pyiceberg view --- mkdocs/docs/api.md | 75 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 70 insertions(+), 5 deletions(-) diff --git a/mkdocs/docs/api.md b/mkdocs/docs/api.md index 4c1e0f58ce..a98f82e144 100644 --- a/mkdocs/docs/api.md +++ b/mkdocs/docs/api.md @@ -1527,17 +1527,40 @@ def cleanup_old_snapshots(table_name: str, snapshot_ids: list[int]): cleanup_old_snapshots("analytics.user_events", [12345, 67890, 11111]) ``` -## Views +## Create a view -PyIceberg supports view operations. - -### Check if a view exists +To create a view from a catalog: ```python +import time +import pyarrow as pa from pyiceberg.catalog import load_catalog +from pyiceberg.view import SQLViewRepresentation, ViewVersion catalog = load_catalog("default") -catalog.view_exists("default.bar") + +identifier = "default.some_view" +schema = pa.schema([pa.field("some_col", pa.int32())]) +view_version = ViewVersion( + version_id=1, + schema_id=1, + timestamp_ms=int(time.time() * 1000), + summary={}, + representations=[ + SQLViewRepresentation( + type="sql", + sql="SELECT 1 as some_col", + dialect="spark", + ) + ], + default_namespace=["default"], +) + +catalog.create_view( + identifier=identifier, + schema=schema, + view_version=view_version, +) ``` ## Register a view @@ -1551,6 +1574,48 @@ catalog.register_view( ) ``` +## Load a view + +Loading the `some_view` view: + +```python +view = catalog.load_view("default.some_view") +# Equivalent to: +view = catalog.load_view(("default", "some_view")) +# The tuple syntax can be used if the namespace or view contains a dot. +``` + +This returns a `View` that represents an Iceberg view. You can access the SQL representation for a specific dialect: + +```python +sql_representation = view.sql_for("spark") +print(sql_representation.sql) +``` + +## Check if a view exists + +To check whether the `some_view` view exists: + +```python +catalog.view_exists("default.some_view") +``` + +## List views + +To list views in the `default` namespace: + +```python +catalog.list_views("default") +``` + +## Drop a view + +To drop a view: + +```python +catalog.drop_view("default.some_view") +``` + ## Table Statistics Management Manage table statistics with operations through the `Table` API: From 47ed01ea2389d51679c18c321a43cb80dac25eb1 Mon Sep 17 00:00:00 2001 From: Yong Date: Tue, 26 May 2026 10:54:40 -0500 Subject: [PATCH 2/2] Update doc --- mkdocs/docs/api.md | 44 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/mkdocs/docs/api.md b/mkdocs/docs/api.md index a98f82e144..59124788e2 100644 --- a/mkdocs/docs/api.md +++ b/mkdocs/docs/api.md @@ -1529,23 +1529,41 @@ cleanup_old_snapshots("analytics.user_events", [12345, 67890, 11111]) ## Create a view -To create a view from a catalog: +If the REST server does not indicate support for view endpoints, you can enable it by setting `"view-endpoints-supported": "true"`: + +```python +from pyiceberg.catalog import load_catalog + +catalog = load_catalog( + "docs", + **{ + "uri": "http://127.0.0.1:8181", + "s3.endpoint": "http://127.0.0.1:9000", + "py-io-impl": "pyiceberg.io.pyarrow.PyArrowFileIO", + "s3.access-key-id": "admin", + "s3.secret-access-key": "password", + "view-endpoints-supported": "true", + } +) +``` + +To create a view from the catalog: ```python import time -import pyarrow as pa from pyiceberg.catalog import load_catalog +from pyiceberg.schema import Schema +from pyiceberg.types import IntegerType, NestedField from pyiceberg.view import SQLViewRepresentation, ViewVersion catalog = load_catalog("default") -identifier = "default.some_view" -schema = pa.schema([pa.field("some_col", pa.int32())]) +schema = Schema(NestedField(field_id=1, name="some_col", field_type=IntegerType(), required=False)) view_version = ViewVersion( version_id=1, schema_id=1, timestamp_ms=int(time.time() * 1000), - summary={}, + summary={"spark-version": "4.1"}, representations=[ SQLViewRepresentation( type="sql", @@ -1557,7 +1575,21 @@ view_version = ViewVersion( ) catalog.create_view( - identifier=identifier, + identifier="default.some_view", + schema=schema, + view_version=view_version, +) +``` + +`catalog.create_view` also accepts a PyArrow schema, so the following is equivalent: + +```python +import pyarrow as pa + +schema = pa.schema([pa.field("some_col", pa.int32())]) + +catalog.create_view( + identifier="default.some_view", schema=schema, view_version=view_version, )