Skip to content

Commit 6181acb

Browse files
committed
Add more examples for pyiceberg view
1 parent 55887b4 commit 6181acb

1 file changed

Lines changed: 70 additions & 5 deletions

File tree

mkdocs/docs/api.md

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,17 +1527,40 @@ def cleanup_old_snapshots(table_name: str, snapshot_ids: list[int]):
15271527
cleanup_old_snapshots("analytics.user_events", [12345, 67890, 11111])
15281528
```
15291529

1530-
## Views
1530+
## Create a view
15311531

1532-
PyIceberg supports view operations.
1533-
1534-
### Check if a view exists
1532+
To create a view from a catalog:
15351533

15361534
```python
1535+
import time
1536+
import pyarrow as pa
15371537
from pyiceberg.catalog import load_catalog
1538+
from pyiceberg.view import SQLViewRepresentation, ViewVersion
15381539

15391540
catalog = load_catalog("default")
1540-
catalog.view_exists("default.bar")
1541+
1542+
identifier = "default.some_view"
1543+
schema = pa.schema([pa.field("some_col", pa.int32())])
1544+
view_version = ViewVersion(
1545+
version_id=1,
1546+
schema_id=1,
1547+
timestamp_ms=int(time.time() * 1000),
1548+
summary={},
1549+
representations=[
1550+
SQLViewRepresentation(
1551+
type="sql",
1552+
sql="SELECT 1 as some_col",
1553+
dialect="spark",
1554+
)
1555+
],
1556+
default_namespace=["default"],
1557+
)
1558+
1559+
catalog.create_view(
1560+
identifier=identifier,
1561+
schema=schema,
1562+
view_version=view_version,
1563+
)
15411564
```
15421565

15431566
## Register a view
@@ -1551,6 +1574,48 @@ catalog.register_view(
15511574
)
15521575
```
15531576

1577+
## Load a view
1578+
1579+
Loading the `some_view` view:
1580+
1581+
```python
1582+
view = catalog.load_view("default.some_view")
1583+
# Equivalent to:
1584+
view = catalog.load_view(("default", "some_view"))
1585+
# The tuple syntax can be used if the namespace or view contains a dot.
1586+
```
1587+
1588+
This returns a `View` that represents an Iceberg view. You can access the SQL representation for a specific dialect:
1589+
1590+
```python
1591+
sql_representation = view.sql_for("spark")
1592+
print(sql_representation.sql)
1593+
```
1594+
1595+
## Check if a view exists
1596+
1597+
To check whether the `some_view` view exists:
1598+
1599+
```python
1600+
catalog.view_exists("default.some_view")
1601+
```
1602+
1603+
## List views
1604+
1605+
To list views in the `default` namespace:
1606+
1607+
```python
1608+
catalog.list_views("default")
1609+
```
1610+
1611+
## Drop a view
1612+
1613+
To drop a view:
1614+
1615+
```python
1616+
catalog.drop_view("default.some_view")
1617+
```
1618+
15541619
## Table Statistics Management
15551620

15561621
Manage table statistics with operations through the `Table` API:

0 commit comments

Comments
 (0)