Skip to content

Commit ad959fb

Browse files
authored
Add PyIceberg example (#2315)
It is not obvious how to connect PyIceberg to a Polaris catalog. This PR clears that up by providing an example in the getting-started section of the documentation.
1 parent ee04df4 commit ad959fb

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

site/content/in-dev/unreleased/getting-started/using-polaris.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,51 @@ SELECT * FROM iceberg.quickstart_schema.quickstart_table;
285285
org.apache.iceberg.exceptions.ForbiddenException: Forbidden: Principal 'quickstart_user' with activated PrincipalRoles '[]' and activated grants via '[quickstart_catalog_role, quickstart_user_role]' is not authorized for op LOAD_TABLE_WITH_READ_DELEGATION
286286
```
287287

288+
### Connecting with PyIceberg
289+
290+
#### Using Credentials
291+
292+
```python
293+
from pyiceberg.catalog import load_catalog
294+
295+
catalog = load_catalog(
296+
type='rest',
297+
uri='http://localhost:8181/api/catalog',
298+
warehouse='quickstart_catalog',
299+
scope="PRINCIPAL_ROLE:ALL",
300+
credential=f"{CLIENT_ID}:{CLIENT_SECRET}",
301+
)
302+
```
303+
304+
If the `load_catalog` function is used with credentials, then PyIceberg will automatically request an authorization token from the `v1/oauth/tokens` endpoint, and will later use this token to prove its identity to the Polaris Catalog.
305+
306+
#### Using a Token
307+
308+
```python
309+
from pyiceberg.catalog import load_catalog
310+
import requests
311+
312+
# Step 1: Get OAuth token
313+
response = requests.post(
314+
"http://localhost:8181/api/catalog/v1/oauth/tokens",
315+
auth =(CLIENT_ID, CLIENT_SECRET),
316+
data = {
317+
"grant_type": "client_credentials",
318+
"scope": "PRINCIPAL_ROLE:ALL"
319+
})
320+
token = response.json()["access_token"]
321+
322+
# Step 2: Load the catalog using the token
323+
catalog = load_catalog(
324+
type='rest',
325+
uri='http://localhost:8181/api/catalog',
326+
warehouse='quickstart_catalog',
327+
token=token,
328+
)
329+
```
330+
331+
It is possible to use `load_catalog` function by providing an authorization token directly. This method is useful when using an external identity provider (e.g. Google Identity).
332+
288333
### Connecting Using REST APIs
289334

290335
To access Polaris from the host machine, first request an access token:

0 commit comments

Comments
 (0)