Skip to content

Commit 683b700

Browse files
committed
Small getting started guide on writes
1 parent cd7fb50 commit 683b700

File tree

3 files changed

+137
-28
lines changed

3 files changed

+137
-28
lines changed

mkdocs/docs/SUMMARY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
<!-- prettier-ignore-start -->
1919

20-
- [Home](index.md)
20+
- [Getting started](index.md)
2121
- [Configuration](configuration.md)
2222
- [CLI](cli.md)
2323
- [API](api.md)

mkdocs/docs/contributing.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,22 @@ For IDEA ≤2021 you need to install the [Poetry integration as a plugin](https:
5858

5959
Now you're set using Poetry, and all the tests will run in Poetry, and you'll have syntax highlighting in the pyproject.toml to indicate stale dependencies.
6060

61+
## Installation from source
62+
63+
Clone the repository for local development:
64+
65+
```sh
66+
git clone https://github.com/apache/iceberg-python.git
67+
cd iceberg-python
68+
pip3 install -e ".[s3fs,hive]"
69+
```
70+
71+
Install it directly for GitHub (not recommended), but sometimes handy:
72+
73+
```
74+
pip install "git+https://github.com/apache/iceberg-python.git#egg=pyiceberg[s3fs]"
75+
```
76+
6177
## Linting
6278

6379
`pre-commit` is used for autoformatting and linting:

mkdocs/docs/index.md

Lines changed: 120 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ hide:
2020
- limitations under the License.
2121
-->
2222

23-
# PyIceberg
23+
# Getting started with PyIceberg
2424

2525
PyIceberg is a Python implementation for accessing Iceberg tables, without the need of a JVM.
2626

27-
## Install
27+
## Installation
2828

2929
Before installing PyIceberg, make sure that you're on an up-to-date version of `pip`:
3030

@@ -38,36 +38,129 @@ You can install the latest release version from pypi:
3838
pip install "pyiceberg[s3fs,hive]"
3939
```
4040

41-
Install it directly for GitHub (not recommended), but sometimes handy:
41+
You can mix and match optional dependencies depending on your needs:
42+
43+
| Key | Description: |
44+
| ------------ | -------------------------------------------------------------------- |
45+
| hive | Support for the Hive metastore |
46+
| glue | Support for AWS Glue |
47+
| dynamodb | Support for AWS DynamoDB |
48+
| sql-postgres | Support for SQL Catalog backed by Postgresql |
49+
| sql-sqlite | Support for SQL Catalog backed by SQLite |
50+
| pyarrow | PyArrow as a FileIO implementation to interact with the object store |
51+
| pandas | Installs both PyArrow and Pandas |
52+
| duckdb | Installs both PyArrow and DuckDB |
53+
| ray | Installs PyArrow, Pandas, and Ray |
54+
| s3fs | S3FS as a FileIO implementation to interact with the object store |
55+
| adlfs | ADLFS as a FileIO implementation to interact with the object store |
56+
| snappy | Support for snappy Avro compression |
57+
| gcs | GCS as the FileIO implementation to interact with the object store |
58+
59+
You either need to install `s3fs`, `adlfs`, `gcs`, or `pyarrow` to be able to fetch files from an object store.
60+
61+
## Connecting to a catalog
62+
63+
Iceberg leverages the [catalog to have one centralized place to organize the tables](https://iceberg.apache.org/catalog/). This can be a traditional Hive catalog to store your Iceberg tables next to the rest, a vendor solution like the AWS Glue catalog, or an implementation of Icebergs' own [REST protocol](https://github.com/apache/iceberg/tree/main/open-api). Checkout the [configuration](configuration.md) page to find all the configuration details.
4264

65+
## Write a PyArrow dataframe
66+
67+
Let's take the Taxi dataset, and write this to an Iceberg table.
68+
69+
First download one month of data:
70+
71+
```shell
72+
curl https://d37ci6vzurychx.cloudfront.net/trip-data/yellow_tripdata_2023-01.parquet -o /tmp/yellow_tripdata_2023-01.parquet
4373
```
44-
pip install "git+https://github.com/apache/iceberg-python.git#egg=pyiceberg[s3fs]"
74+
75+
Load it into your PyArrow dataframe:
76+
77+
```python
78+
import pyarrow.parquet as pq
79+
80+
df = pq.read_table("/tmp/yellow_tripdata_2023-01.parquet")
4581
```
4682

47-
Or clone the repository for local development:
83+
Create a new Iceberg table:
4884

49-
```sh
50-
git clone https://github.com/apache/iceberg-python.git
51-
cd iceberg-python
52-
pip3 install -e ".[s3fs,hive]"
85+
```python
86+
from pyiceberg.catalog import load_catalog
87+
88+
catalog = load_catalog("default")
89+
90+
table = catalog.create_table(
91+
"default.taxi_dataset",
92+
schema=df.schema, # Blocked by https://github.com/apache/iceberg-python/pull/305
93+
)
5394
```
5495

55-
You can mix and match optional dependencies depending on your needs:
96+
Append the dataframe to the table:
97+
98+
```python
99+
table.append(df)
100+
len(table.scan().to_arrow())
101+
```
102+
103+
3066766 rows have been written to the table.
104+
105+
Now generate a tip-per-mile feature to train the model on:
106+
107+
```python
108+
import pyarrow.compute as pc
109+
110+
df = df.append_column("tip_per_mile", pc.divide(df["tip_amount"], df["trip_distance"]))
111+
```
112+
113+
Evolve the schema of the table with the new column:
114+
115+
```python
116+
from pyiceberg.catalog import Catalog
117+
118+
with table.update_schema() as update_schema:
119+
# Blocked by https://github.com/apache/iceberg-python/pull/305
120+
update_schema.union_by_name(Catalog._convert_schema_if_needed(df.schema))
121+
```
122+
123+
And now we can write the new dataframe to the Iceberg table:
124+
125+
```python
126+
table.overwrite(df)
127+
print(table.scan().to_arrow())
128+
```
129+
130+
And the new column is there:
131+
132+
```
133+
taxi_dataset(
134+
1: VendorID: optional long,
135+
2: tpep_pickup_datetime: optional timestamp,
136+
3: tpep_dropoff_datetime: optional timestamp,
137+
4: passenger_count: optional double,
138+
5: trip_distance: optional double,
139+
6: RatecodeID: optional double,
140+
7: store_and_fwd_flag: optional string,
141+
8: PULocationID: optional long,
142+
9: DOLocationID: optional long,
143+
10: payment_type: optional long,
144+
11: fare_amount: optional double,
145+
12: extra: optional double,
146+
13: mta_tax: optional double,
147+
14: tip_amount: optional double,
148+
15: tolls_amount: optional double,
149+
16: improvement_surcharge: optional double,
150+
17: total_amount: optional double,
151+
18: congestion_surcharge: optional double,
152+
19: airport_fee: optional double,
153+
20: tip_per_mile: optional double
154+
),
155+
```
156+
157+
And we can see that 2371784 rows have a tip-per-mile:
158+
159+
```python
160+
df = table.scan(row_filter="tip_per_mile > 0").to_arrow()
161+
len(df)
162+
```
163+
164+
## More details
56165

57-
| Key | Description: |
58-
| -------- | -------------------------------------------------------------------- |
59-
| hive | Support for the Hive metastore |
60-
| glue | Support for AWS Glue |
61-
| dynamodb | Support for AWS DynamoDB |
62-
| pyarrow | PyArrow as a FileIO implementation to interact with the object store |
63-
| pandas | Installs both PyArrow and Pandas |
64-
| duckdb | Installs both PyArrow and DuckDB |
65-
| ray | Installs PyArrow, Pandas, and Ray |
66-
| s3fs | S3FS as a FileIO implementation to interact with the object store |
67-
| adlfs | ADLFS as a FileIO implementation to interact with the object store |
68-
| snappy | Support for snappy Avro compression |
69-
| gcs | GCS as the FileIO implementation to interact with the object store |
70-
71-
You either need to install `s3fs`, `adlfs`, `gcs`, or `pyarrow` for fetching files.
72-
73-
There is both a [CLI](cli.md) and [Python API](api.md) available.
166+
For the details, please check the [CLI](cli.md) or [Python API](api.md) page.

0 commit comments

Comments
 (0)