Skip to content

Commit 1693526

Browse files
docs(samples): Add flex template "getting started" sample (GoogleCloudPlatform#10918)
1 parent d308cc6 commit 1693526

4 files changed

Lines changed: 110 additions & 0 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Dataflow flex template: Getting started sample
2+
3+
## Before you begin
4+
5+
Make sure you have followed the
6+
[Dataflow setup instructions](../../README.md).
7+
8+
## Create a Cloud Storage bucket
9+
10+
```sh
11+
export BUCKET="your--bucket"
12+
gsutil mb gs://$BUCKET
13+
```
14+
15+
## create an Artifact Registry repository
16+
17+
```sh
18+
export REGION="us-central1"
19+
export REPOSITORY="your-repository"
20+
21+
gcloud artifacts repositories create $REPOSITORY \
22+
--repository-format=docker \
23+
--location=$REGION
24+
```
25+
26+
## Build the template
27+
28+
```sh
29+
export PROJECT="project-id"
30+
31+
gcloud dataflow flex-template build gs://$BUCKET/getting_started_py.json \
32+
--image-gcr-path "$REGION-docker.pkg.dev/$PROJECT/$REPOSITORY/getting-started-py:latest" \
33+
--sdk-language "PYTHON" \
34+
--flex-template-base-image "PYTHON3" \
35+
--py-path "." \
36+
--metadata-file "metadata.json" \
37+
--env "FLEX_TEMPLATE_PYTHON_PY_FILE=getting_started.py" \
38+
--env "FLEX_TEMPLATE_PYTHON_REQUIREMENTS_FILE=requirements.txt"
39+
```
40+
41+
## Run the template
42+
43+
```sh
44+
gcloud dataflow flex-template run "flex-`date +%Y%m%d-%H%M%S`" \
45+
--template-file-gcs-location "gs://$BUCKET/getting_started_py.json" \
46+
--region $REGION \
47+
--parameters output="gs://$BUCKET/output-"
48+
```
49+
50+
## What's next?
51+
52+
For more information about building and running flex templates, see
53+
📝 [Use Flex Templates](https://cloud.google.com/dataflow/docs/guides/templates/using-flex-templates).
54+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env python
2+
# Copyright 2023 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
import apache_beam as beam
17+
from apache_beam.io.textio import WriteToText
18+
from apache_beam.options.pipeline_options import PipelineOptions
19+
20+
21+
def write_to_cloud_storage(argv=None):
22+
# Parse the pipeline options passed into the application.
23+
class MyOptions(PipelineOptions):
24+
@classmethod
25+
# Define a custom pipeline option that specfies the Cloud Storage bucket.
26+
def _add_argparse_args(cls, parser):
27+
parser.add_argument("--output", required=True)
28+
29+
wordsList = ["1", "2", "3", "4"]
30+
options = MyOptions()
31+
32+
with beam.Pipeline(options=options) as pipeline:
33+
(
34+
pipeline
35+
| "Create elements" >> beam.Create(wordsList)
36+
| "Write Files" >> WriteToText(options.output, file_name_suffix=".txt")
37+
)
38+
39+
40+
if __name__ == "__main__":
41+
write_to_cloud_storage()
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "Getting started Python flex template",
3+
"description": "An example flex template for Python.",
4+
"parameters": [
5+
{
6+
"name": "output",
7+
"label": "Output destination",
8+
"helpText": "The path and filename prefix for writing output files. Example: gs://your-bucket/your-path",
9+
"regexes": [
10+
"^gs:\\/\\/[^\\n\\r]+$"
11+
]
12+
}
13+
]
14+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
apache-beam[gcp]==2.48.0

0 commit comments

Comments
 (0)