-
Notifications
You must be signed in to change notification settings - Fork 4.5k
[YAML]: add import jinja pipeline example #35945
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2a0b0e4
add import jinja pipeline example
derrickaw 4b16198
revert name change
derrickaw ad28c5d
update overall examples readme
derrickaw e7971df
fix lint issue
derrickaw b17b1f5
fix gemini small issue
derrickaw 964eb69
Update sdks/python/apache_beam/yaml/examples/transforms/jinja/import/…
tvalentyn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
sdks/python/apache_beam/yaml/examples/transforms/jinja/import/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| <!-- | ||
| Licensed to the Apache Software Foundation (ASF) under one | ||
| or more contributor license agreements. See the NOTICE file | ||
| distributed with this work for additional information | ||
| regarding copyright ownership. The ASF licenses this file | ||
| to you under the Apache License, Version 2.0 (the | ||
| "License"); you may not use this file except in compliance | ||
| with the License. You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, | ||
| software distributed under the License is distributed on an | ||
| "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| KIND, either express or implied. See the License for the | ||
| specific language governing permissions and limitations | ||
| under the License. | ||
| --> | ||
|
|
||
| ## Jinja % import Pipeline | ||
|
|
||
| This example leverages the `% import` Jinja directive by having one main | ||
| pipeline and then one macros file containing all the transforms and configs | ||
| used. | ||
|
|
||
| General setup: | ||
| ```sh | ||
| export PIPELINE_FILE=apache_beam/yaml/examples/transforms/jinja/import/wordCountImport.yaml | ||
| export KINGLEAR="gs://dataflow-samples/shakespeare/kinglear.txt" | ||
| export TEMP_LOCATION="gs://MY-BUCKET/wordCounts/" | ||
|
|
||
| cd <PATH_TO_BEAM_REPO>/beam/sdks/python | ||
| ``` | ||
|
|
||
| Multiline Run Example: | ||
| ```sh | ||
| python -m apache_beam.yaml.main \ | ||
| --yaml_pipeline_file="${PIPELINE_FILE}" \ | ||
| --jinja_variables='{ | ||
| "readFromTextTransform": {"path": "'"${KINGLEAR}"'"}, | ||
| "mapToFieldsSplitConfig": { | ||
| "language": "python", | ||
| "fields": { | ||
| "value": "1" | ||
| } | ||
| }, | ||
| "explodeTransform": {"fields": "word"}, | ||
| "combineTransform": { | ||
| "group_by": "word", | ||
| "combine": {"value": "sum"} | ||
| }, | ||
| "mapToFieldsCountConfig": { | ||
| "language": "python", | ||
| "fields": {"output": "word + \" - \" + str(value)"} | ||
| }, | ||
| "writeToTextTransform": {"path": "'"${TEMP_LOCATION}"'"} | ||
| }' | ||
| ``` | ||
|
|
||
| Single Line Run Example: | ||
| ```sh | ||
| python -m apache_beam.yaml.main --yaml_pipeline_file="${PIPELINE_FILE}" --jinja_variables='{"readFromTextTransform": {"path": "gs://dataflow-samples/shakespeare/kinglear.txt"}, "mapToFieldsSplitConfig": {"language": "python", "fields":{"value":"1"}}, "explodeTransform":{"fields":"word"}, "combineTransform":{"group_by":"word", "combine":{"value":"sum"}}, "mapToFieldsCountConfig":{"language": "python", "fields":{"output":"word + \" - \" + str(value)"}}, "writeToTextTransform":{"path":"${TEMP_LOCATION}"}}' | ||
| ``` | ||
64 changes: 64 additions & 0 deletions
64
sdks/python/apache_beam/yaml/examples/transforms/jinja/import/macros/wordCountMacros.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| # | ||
| # Licensed to the Apache Software Foundation (ASF) under one or more | ||
| # contributor license agreements. See the NOTICE file distributed with | ||
| # this work for additional information regarding copyright ownership. | ||
| # The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| # (the "License"); you may not use this file except in compliance with | ||
| # the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
|
|
||
| {%- macro readFromTextTransform(params) -%} | ||
|
|
||
| - name: Read from GCS | ||
| type: ReadFromText | ||
| config: | ||
| path: "{{ params.path }}" | ||
| {%- endmacro -%} | ||
|
|
||
| {%- macro mapToFieldsSplitConfig(params) -%} | ||
| language: "{{ params.language }}" | ||
| fields: | ||
| value: "{{ params.fields.value }}" | ||
| word: | ||
| callable: |- | ||
| import re | ||
| def my_mapping(row): | ||
| return re.findall(r'[A-Za-z\']+', row.line.lower()) | ||
| {%- endmacro -%} | ||
|
|
||
| {%- macro explodeTransform(params) -%} | ||
| - name: Explode word arrays | ||
| type: Explode | ||
| config: | ||
| fields: "{{ params.fields }}" | ||
| {%- endmacro -%} | ||
|
|
||
| {%- macro combineTransform(params) -%} | ||
| - name: Count words | ||
| type: Combine | ||
| config: | ||
| group_by: "{{ params.group_by }}" | ||
| combine: | ||
| value: "{{ params.combine.value }}" | ||
| {%- endmacro -%} | ||
|
|
||
| {%- macro mapToFieldsCountConfig(params) -%} | ||
| language: "{{ params.language }}" | ||
| fields: | ||
| output: '{{ params.fields.output }}' | ||
| {%- endmacro -%} | ||
|
|
||
| {%- macro writeToTextTransform(params) -%} | ||
| - name: Write to GCS | ||
| type: WriteToText | ||
| config: | ||
| path: "{{ params.path }}" | ||
| {%- endmacro -%} |
69 changes: 69 additions & 0 deletions
69
sdks/python/apache_beam/yaml/examples/transforms/jinja/import/wordCountImport.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| # coding=utf-8 | ||
| # | ||
| # Licensed to the Apache Software Foundation (ASF) under one or more | ||
| # contributor license agreements. See the NOTICE file distributed with | ||
| # this work for additional information regarding copyright ownership. | ||
| # The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| # (the "License"); you may not use this file except in compliance with | ||
| # the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
|
|
||
| # This examples reads from a public file stored on Google Cloud. This | ||
| # requires authenticating with Google Cloud, or setting the file in | ||
| #`ReadFromText` to a local file. | ||
| # | ||
| # To set up Application Default Credentials, | ||
| # see https://cloud.google.com/docs/authentication/external/set-up-adc. | ||
| # | ||
| # This pipeline reads in a text file, counts distinct words found in the text, | ||
| # then logs a row containing each word and its count. | ||
|
|
||
| {% import 'apache_beam/yaml/examples/transforms/jinja/import/macros/wordCountMacros.yaml' as macros %} | ||
|
|
||
| pipeline: | ||
| type: chain | ||
| transforms: | ||
|
|
||
| # Read in text file | ||
| {{ macros.readFromTextTransform(readFromTextTransform) | indent(4, true) }} | ||
|
|
||
| # Split words and count occurrences | ||
| - name: Split words | ||
| type: MapToFields | ||
| config: | ||
| {{ macros.mapToFieldsSplitConfig(mapToFieldsSplitConfig) | indent(8, true) }} | ||
|
|
||
| # Explode into individual words | ||
| {{ macros.explodeTransform(explodeTransform) | indent(4, true) }} | ||
|
|
||
| # Group by word | ||
| {{ macros.combineTransform(combineTransform) | indent(4, true) }} | ||
|
|
||
| # Format output to a single string consisting of `word - count` | ||
| - name: Format output | ||
| type: MapToFields | ||
| config: | ||
| {{ macros.mapToFieldsCountConfig(mapToFieldsCountConfig) | indent(8, true) }} | ||
|
|
||
| # Write to text file on GCS, locally, etc | ||
| {{ macros.writeToTextTransform(writeToTextTransform) | indent(4, true) }} | ||
|
|
||
| # Expected: | ||
| # Row(output='king - 311') | ||
| # Row(output='lear - 253') | ||
| # Row(output='dramatis - 1') | ||
| # Row(output='personae - 1') | ||
| # Row(output='of - 483') | ||
| # Row(output='britain - 2') | ||
| # Row(output='france - 32') | ||
| # Row(output='duke - 26') | ||
| # Row(output='burgundy - 20') | ||
| # Row(output='cornwall - 75') |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.