Skip to content

Commit dab5f5d

Browse files
authored
YAML example suite update (#35960)
* YAML example suite update * Update README and notebook * Update README
1 parent 372b25b commit dab5f5d

6 files changed

Lines changed: 98 additions & 31 deletions

File tree

examples/yaml/README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<!--
2+
Licensed to the Apache Software Foundation (ASF) under one
3+
or more contributor license agreements. See the NOTICE file
4+
distributed with this work for additional information
5+
regarding copyright ownership. The ASF licenses this file
6+
to you under the Apache License, Version 2.0 (the
7+
"License"); you may not use this file except in compliance
8+
with the License. You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing,
13+
software distributed under the License is distributed on an
14+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
KIND, either express or implied. See the License for the
16+
specific language governing permissions and limitations
17+
under the License.
18+
-->
19+
20+
## Example YAML Pipelines
21+
22+
A suite of YAML pipeline examples is currently located under the directory
23+
[sdks/python/apache_beam/yaml/examples](../../sdks/python/apache_beam/yaml/examples).
24+
25+
### [Aggregation](../../sdks/python/apache_beam/yaml/examples/transforms/aggregation)
26+
27+
These examples leverage the built-in `Combine` transform for performing simple
28+
aggregations including sum, mean, count, etc.
29+
30+
### [Blueprints](../../sdks/python/apache_beam/yaml/examples/transforms/blueprint)
31+
32+
These examples leverage DF or other existing templates and convert them to yaml
33+
blueprints.
34+
35+
### [Element-wise](../../sdks/python/apache_beam/yaml/examples/transforms/elementwise)
36+
37+
These examples leverage the built-in mapping transforms including `MapToFields`,
38+
`Filter` and `Explode`.
39+
40+
### [IO](../../sdks/python/apache_beam/yaml/examples/transforms/io)
41+
42+
These examples leverage the built-in IO transforms to read from and write to
43+
various sources and sinks, including Iceberg, Kafka and Spanner.
44+
45+
### [Jinja](../../sdks/python/apache_beam/yaml/examples/transforms/jinja)
46+
47+
These examples use Jinja [templatization](https://beam.apache.org/documentation/sdks/yaml/#jinja-templatization)
48+
to build off of different contexts and/or with different
49+
configurations.
50+
51+
### [ML](../../sdks/python/apache_beam/yaml/examples/transforms/ml)
52+
53+
These examples include built-in ML-specific transforms such as `RunInference`,
54+
`MLTransform` and `Enrichment`.

sdks/python/apache_beam/yaml/examples/transforms/ml/sentiment_analysis/README.md

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ https://www.kaggle.com/datasets/datasnaek/youtube?select=UScomments.csv).
3030

3131
Download the dataset and copy over to a GCS bucket:
3232
```sh
33-
gcloud storage cp /path/to/UScomments.csv gs://YOUR_BUCKET/UScomments.csv
33+
export GCS_PATH="gs://YOUR-BUCKET/USComments.csv"
34+
gcloud storage cp /path/to/UScomments.csv $GCS_PATH
3435
```
3536

3637
For setting up Kafka, an option is to use [Click to Deploy](
@@ -54,6 +55,7 @@ a BigQuery dataset:
5455
```sh
5556
bq --location=us-central1 mk \
5657
--dataset DATASET_ID
58+
export BQ_TABLE_ID="PROJECT_ID:DATASET_ID.TABLE_ID"
5759
```
5860
See also [here](
5961
https://cloud.google.com/bigquery/docs/datasets) for more details on
@@ -76,18 +78,23 @@ export TEMP_LOCATION="gs://YOUR-BUCKET/tmp"
7678
export REGION="us-central1"
7779
export JOB_NAME="streaming-sentiment-analysis-`date +%Y%m%d-%H%M%S`"
7880
export NUM_WORKERS="3"
81+
export KAFKA_BOOTSTRAP_SERVERS="BOOTSTRAP_IP_ADD:9092"
82+
export KAFKA_TOPIC="YOUR_TOPIC"
83+
export KAFKA_USERNAME="KAFKA_USERNAME"
84+
export KAFKA_PASSWORD="KAFKA_PASSWORD"
85+
export VERTEXAI_ENDPOINT="ENDPOINT_ID"
7986

8087
python -m apache_beam.yaml.main \
81-
--yaml_pipeline_file transforms/ml/sentiment_analysis/streaming_sentiment_analysis.yaml \
88+
--yaml_pipeline_file streaming_sentiment_analysis.yaml \
8289
--runner DataflowRunner \
8390
--temp_location $TEMP_LOCATION \
8491
--project $PROJECT \
8592
--region $REGION \
8693
--num_workers $NUM_WORKERS \
8794
--job_name $JOB_NAME \
88-
--jinja_variables '{ "GCS_PATH": "gs://YOUR-BUCKET/USComments.csv",
89-
"BOOTSTRAP_SERVERS": "BOOTSTRAP_IP_ADD:9092",
90-
"TOPIC": "YOUR_TOPIC", "USERNAME": "KAFKA_USERNAME", "PASSWORD": "KAFKA_PASSWORD",
91-
"ENDPOINT": "ENDPOINT_ID", "PROJECT": "PROJECT_ID", "LOCATION": "LOCATION",
92-
"DATASET": "DATASET_ID", "TABLE": "TABLE_ID" }'
95+
--jinja_variables '{ "GCS_PATH": "'$GCS_PATH'",
96+
"BOOTSTRAP_SERVERS": "'$KAFKA_BOOTSTRAP_SERVERS'",
97+
"TOPIC": "'$KAFKA_TOPIC'", "USERNAME": "'$KAFKA_USERNAME'", "PASSWORD": "'$KAFKA_PASSWORD'",
98+
"ENDPOINT": "'$VERTEXAI_ENDPOINT'", "PROJECT": "'$PROJECT'", "LOCATION": "'$REGION'",
99+
"BQ_TABLE": "'$BQ_TABLE_ID'" }'
93100
```

sdks/python/apache_beam/yaml/examples/transforms/ml/sentiment_analysis/streaming_sentiment_analysis.yaml

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,19 @@ pipeline:
4444
return int(x)
4545
except (ValueError):
4646
return None
47-
47+
4848
return (
49-
pcoll
49+
pcoll
5050
| beam.io.ReadFromCsv(
5151
file_pattern,
5252
names=['video_id', 'comment_text', 'likes', 'replies'],
5353
on_bad_lines='skip',
5454
converters={'likes': _to_int, 'replies': _to_int})
55-
| beam.Filter(lambda row:
55+
| beam.Filter(lambda row:
5656
None not in list(row._asdict().values()))
5757
| beam.Map(lambda row: beam.Row(
58-
video_id=row.video_id,
59-
comment_text=row.comment_text,
58+
video_id=str(row.video_id),
59+
comment_text=str(row.comment_text),
6060
likes=int(row.likes),
6161
replies=int(row.replies)))
6262
)
@@ -154,17 +154,16 @@ pipeline:
154154
comment_text:
155155
callable: |
156156
from transformers import AutoTokenizer
157-
157+
158158
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased", use_fast=True)
159-
160-
def truncate_sentence(row):
161-
tokens = tokenizer.tokenize(row.comment_text)
162-
if len(tokens) >= 250:
163-
tokens = tokens[:250]
164-
truncated_sentence = tokenizer.convert_tokens_to_string(tokens)
165-
else:
166-
truncated_sentence = row.comment_text
167159
160+
def truncate_sentence(row):
161+
tokens = tokenizer(
162+
row.comment_text,
163+
max_length=512,
164+
padding='max_length',
165+
truncation=True)
166+
truncated_sentence = tokenizer.decode(tokens["input_ids"], skip_special_tokens=True)
168167
return truncated_sentence
169168
likes: likes
170169
replies: replies
@@ -242,7 +241,7 @@ pipeline:
242241
name: WriteInferenceResultsToBQ
243242
input: Windowing
244243
config:
245-
table: "{{ PROJECT }}.{{ DATASET }}.{{ TABLE }}"
244+
table: "{{ BQ_TABLE }}"
246245
create_disposition: CREATE_IF_NEEDED
247246
write_disposition: WRITE_APPEND
248247

sdks/python/apache_beam/yaml/examples/transforms/ml/taxi_fare/README.md

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,17 @@ a BigQuery dataset:
4242
```sh
4343
bq --location=us-central1 mk \
4444
--dataset DATASET_ID
45+
export BQ_TABLE_ID="PROJECT_ID:DATASET_ID.TABLE_ID"
4546
```
4647
See also [here](
4748
https://cloud.google.com/bigquery/docs/datasets) for more details on
4849
how to create BigQuery datasets.
4950

5051
A trained model hosted on Vertex AI is needed before being able to use
5152
the Vertex AI model handler. To train and deploy a custom model for the
52-
taxi fare prediction problem, open and run this [notebook](
53-
custom_nyc_taxifare_model_deployment.ipynb) in Colab Enterprise.
53+
taxi fare prediction problem, open and run the
54+
[custom_nyc_taxifare_model_deployment](
55+
custom_nyc_taxifare_model_deployment.ipynb) notebook in Colab Enterprise.
5456

5557
The pipeline first reads the data stream of taxi rides events from the
5658
public PubSub topic and performs some transformations before writing it
@@ -68,17 +70,22 @@ export TEMP_LOCATION="gs://YOUR-BUCKET/tmp"
6870
export REGION="us-central1"
6971
export JOB_NAME="streaming-taxifare-prediction`date +%Y%m%d-%H%M%S`"
7072
export NUM_WORKERS="3"
73+
export KAFKA_BOOTSTRAP_SERVERS="BOOTSTRAP_IP_ADD:9092"
74+
export KAFKA_TOPIC="YOUR_TOPIC"
75+
export KAFKA_USERNAME="KAFKA_USERNAME"
76+
export KAFKA_PASSWORD="KAFKA_PASSWORD"
77+
export VERTEXAI_ENDPOINT="ENDPOINT_ID"
7178

7279
python -m apache_beam.yaml.main \
73-
--yaml_pipeline_file transforms/ml/taxi_fare/streaming_taxifare_prediction.yaml \
80+
--yaml_pipeline_file streaming_taxifare_prediction.yaml \
7481
--runner DataflowRunner \
7582
--temp_location $TEMP_LOCATION \
7683
--project $PROJECT \
7784
--region $REGION \
7885
--num_workers $NUM_WORKERS \
7986
--job_name $JOB_NAME \
80-
--jinja_variables '{ "BOOTSTRAP_SERVERS": "BOOTSTRAP_IP_ADD:9092",
81-
"TOPIC": "YOUR_TOPIC", "USERNAME": "KAFKA_USERNAME", "PASSWORD": "KAFKA_PASSWORD",
82-
"ENDPOINT": "ENDPOINT_ID", "PROJECT": "PROJECT_ID", "LOCATION": "LOCATION",
83-
"DATASET": "DATASET_ID", "TABLE": "TABLE_ID" }'
87+
--jinja_variables '{ "BOOTSTRAP_SERVERS": "'$KAFKA_BOOTSTRAP_SERVERS'",
88+
"TOPIC": "'$KAFKA_TOPIC'", "USERNAME": "'$KAFKA_USERNAME'", "PASSWORD": "'$KAFKA_PASSWORD'",
89+
"ENDPOINT": "'$VERTEXAI_ENDPOINT'", "PROJECT": "'$PROJECT'", "LOCATION": "'$REGION'",
90+
"BQ_TABLE": "'$BQ_TABLE_ID'" }'
8491
```

sdks/python/apache_beam/yaml/examples/transforms/ml/taxi_fare/custom_nyc_taxifare_model_deployment.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"\n",
3737
"<table><tbody><tr>\n",
3838
" <td style=\"text-align: center\">\n",
39-
" <a href=\"https://console.cloud.google.com/vertex-ai/colab/import/https:%2F%2Fraw.githubusercontent.com%2Fapache%2Fbeam%2Fblob%2Fmaster%2Fsdks%2Fpython%2Fapache_beam%2Fyaml%2Fexamples%2Ftransforms%2Fml%2Ftaxi_fare%2Fcustom_nyc_taxifare_model_deployment.ipynb\">\n",
39+
" <a href=\"https://console.cloud.google.com/vertex-ai/colab/import/https:%2F%2Fraw.githubusercontent.com%2Fapache%2Fbeam%2Frefs%2Fheads%2Fmaster%2Fsdks%2Fpython%2Fapache_beam%2Fyaml%2Fexamples%2Ftransforms%2Fml%2Ftaxi_fare%2Fcustom_nyc_taxifare_model_deployment.ipynb\">\n",
4040
" <img alt=\"Google Cloud Colab Enterprise logo\" src=\"https://lh3.googleusercontent.com/JmcxdQi-qOpctIvWKgPtrzZdJJK-J3sWE1RsfjZNwshCFgE_9fULcNpuXYTilIR2hjwN\" width=\"32px\"><br> Run in Colab Enterprise\n",
4141
" </a>\n",
4242
" </td>\n",

sdks/python/apache_beam/yaml/examples/transforms/ml/taxi_fare/streaming_taxifare_prediction.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ pipeline:
308308
name: WritePredictionsBQ
309309
input: FormatInferenceOutput
310310
config:
311-
table: "{{ PROJECT }}.{{ DATASET }}.{{ TABLE }}"
311+
table: "{{ BQ_TABLE }}"
312312
create_disposition: "CREATE_IF_NEEDED"
313313
write_disposition: "WRITE_APPEND"
314314

0 commit comments

Comments
 (0)