-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Python examples to use IObase IOs sinks in streaming (and batch) #35811
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
11 commits
Select commit
Hold shift + click to select a range
453d338
examples to use IObase IOs sinks (TextIO, ParquetIO, AvroIO, TFRecord…
razvanculea f94e495
Merge branch 'master' into iobase_sinks_examples
razvanculea ab0b85f
add __init__.py for module import
razvanculea bc91f12
Fix lint (unused imports)
razvanculea 721dfda
move the samples in to sinks (as iobased_sinks might be too obscure f…
razvanculea fc57e25
fix imports
razvanculea 64aa2bc
formatter fix
razvanculea 7e0d46e
spaces
razvanculea 676bc2a
test pylint line length
razvanculea 736e518
add no qa
razvanculea eb69a3f
fix extra line
razvanculea 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| <!-- | ||
| 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. | ||
| --> | ||
|
|
||
| # Examples of writing to Sinks | ||
|
|
||
| This module contains example pipelines that use the [Beam IO connectors](https://beam.apache.org/documentation/io/connectors/) also known as Sinks to write in streaming and batch. | ||
|
|
||
| ## Batch | ||
|
|
||
| test_write_bounded.py - a simple pipeline taking a bounded PCollection | ||
| as input using the [Create](https://beam.apache.org/releases/pydoc/current/apache_beam.transforms.core.html#apache_beam.transforms.core.Create) | ||
| transform (useful for testing) and writing it to files using multiple IOs. | ||
|
|
||
| ### Running the pipeline | ||
|
|
||
| To run the pipeline locally: | ||
|
|
||
| ```sh | ||
| python -m apache_beam.examples.sinks.test_write_bounded | ||
| ``` | ||
|
|
||
| ## Streaming | ||
|
|
||
| Two example pipelines that use 2 different approches for creating the input. | ||
|
|
||
| test_write_unbounded.py uses [TestStream](https://beam.apache.org/releases/javadoc/current/org/apache/beam/sdk/testing/TestStream.html), | ||
| a method where you can control when data arrives and how watermark advances. | ||
| This is especially useful in unit tests. | ||
|
|
||
| test_periodicimpulse.py uses [PeriodicImpulse](https://beam.apache.org/releases/pydoc/current/apache_beam.transforms.periodicsequence.html#apache_beam.transforms.periodicsequence.PeriodicImpulse), | ||
| a method useful to test pipelines in realtime. You can run it to Dataflow as well. | ||
|
|
||
| ### Running the pipeline | ||
|
|
||
| To run the pipelines locally: | ||
|
|
||
| ```sh | ||
| python -m apache_beam.examples.sinks.test_write_unbounded | ||
| ``` | ||
|
|
||
| ```sh | ||
| python -m apache_beam.examples.sinks.test_periodicimpulse | ||
| ``` |
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,16 @@ | ||
| # | ||
| # 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. | ||
| # |
144 changes: 144 additions & 0 deletions
144
sdks/python/apache_beam/examples/sinks/generate_event.py
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,144 @@ | ||
| # 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. | ||
|
|
||
| from datetime import datetime | ||
|
|
||
| import pytz | ||
|
|
||
| import apache_beam as beam | ||
| from apache_beam.testing.test_stream import TestStream | ||
|
|
||
|
|
||
| class GenerateEvent(beam.PTransform): | ||
| # pylint: disable=line-too-long | ||
| """This class simulates streaming data. | ||
| It leverages [TestStream](https://beam.apache.org/releases/javadoc/current/org/apache/beam/sdk/testing/TestStream.html), | ||
| a method where you can control when data arrives and how watermark advances. | ||
| This is especially useful in unit tests.""" # noqa | ||
|
|
||
| @staticmethod | ||
| def sample_data(): | ||
| return GenerateEvent() | ||
|
|
||
| def expand(self, input): | ||
| # these are the elements that will arrive in the simulated TestStream | ||
| # at multiple timestamps | ||
| elem = [{'age': 10}, {'age': 20}, {'age': 30}] | ||
|
|
||
| # The simulated TestStream adds elements at specific timestamps | ||
| # using add_elements and advances the watermark after 1 or more | ||
| # elements are arrive using advance_watermark_to | ||
| return ( | ||
| input | ||
| | TestStream().add_elements( | ||
| elements=elem, | ||
| event_timestamp=datetime( | ||
| 2021, 3, 1, 0, 0, 1, 0, | ||
| tzinfo=pytz.UTC).timestamp()).add_elements( | ||
| elements=elem, | ||
| event_timestamp=datetime( | ||
| 2021, 3, 1, 0, 0, 2, 0, | ||
| tzinfo=pytz.UTC).timestamp()).add_elements( | ||
| elements=elem, | ||
| event_timestamp=datetime( | ||
| 2021, 3, 1, 0, 0, 3, 0, | ||
| tzinfo=pytz.UTC).timestamp()).add_elements( | ||
| elements=elem, | ||
| event_timestamp=datetime( | ||
| 2021, 3, 1, 0, 0, 4, 0, | ||
| tzinfo=pytz.UTC).timestamp()). | ||
| advance_watermark_to( | ||
| datetime(2021, 3, 1, 0, 0, 5, 0, | ||
| tzinfo=pytz.UTC).timestamp()).add_elements( | ||
| elements=elem, | ||
| event_timestamp=datetime( | ||
| 2021, 3, 1, 0, 0, 5, 0, | ||
| tzinfo=pytz.UTC).timestamp()). | ||
| add_elements( | ||
| elements=elem, | ||
| event_timestamp=datetime( | ||
| 2021, 3, 1, 0, 0, 6, | ||
| 0, tzinfo=pytz.UTC).timestamp()).add_elements( | ||
| elements=elem, | ||
| event_timestamp=datetime( | ||
| 2021, 3, 1, 0, 0, 7, 0, | ||
| tzinfo=pytz.UTC).timestamp()).add_elements( | ||
| elements=elem, | ||
| event_timestamp=datetime( | ||
| 2021, 3, 1, 0, 0, 8, 0, | ||
| tzinfo=pytz.UTC).timestamp()).add_elements( | ||
| elements=elem, | ||
| event_timestamp=datetime( | ||
| 2021, 3, 1, 0, 0, 9, 0, | ||
| tzinfo=pytz.UTC).timestamp()). | ||
| advance_watermark_to( | ||
| datetime(2021, 3, 1, 0, 0, 10, 0, | ||
| tzinfo=pytz.UTC).timestamp()).add_elements( | ||
| elements=elem, | ||
| event_timestamp=datetime( | ||
| 2021, 3, 1, 0, 0, 10, 0, | ||
| tzinfo=pytz.UTC).timestamp()).add_elements( | ||
| elements=elem, | ||
| event_timestamp=datetime( | ||
| 2021, 3, 1, 0, 0, 11, 0, | ||
| tzinfo=pytz.UTC).timestamp()). | ||
| add_elements( | ||
| elements=elem, | ||
| event_timestamp=datetime( | ||
| 2021, 3, 1, 0, 0, 12, 0, | ||
| tzinfo=pytz.UTC).timestamp()).add_elements( | ||
| elements=elem, | ||
| event_timestamp=datetime( | ||
| 2021, 3, 1, 0, 0, 13, 0, | ||
| tzinfo=pytz.UTC).timestamp()).add_elements( | ||
| elements=elem, | ||
| event_timestamp=datetime( | ||
| 2021, 3, 1, 0, 0, 14, 0, | ||
| tzinfo=pytz.UTC).timestamp()). | ||
| advance_watermark_to( | ||
| datetime(2021, 3, 1, 0, 0, 15, 0, | ||
| tzinfo=pytz.UTC).timestamp()).add_elements( | ||
| elements=elem, | ||
| event_timestamp=datetime( | ||
| 2021, 3, 1, 0, 0, 15, 0, | ||
| tzinfo=pytz.UTC).timestamp()).add_elements( | ||
| elements=elem, | ||
| event_timestamp=datetime( | ||
| 2021, 3, 1, 0, 0, 16, 0, | ||
| tzinfo=pytz.UTC).timestamp()). | ||
| add_elements( | ||
| elements=elem, | ||
| event_timestamp=datetime( | ||
| 2021, 3, 1, 0, 0, 17, 0, | ||
| tzinfo=pytz.UTC).timestamp()).add_elements( | ||
| elements=elem, | ||
| event_timestamp=datetime( | ||
| 2021, 3, 1, 0, 0, 18, 0, | ||
| tzinfo=pytz.UTC).timestamp()).add_elements( | ||
| elements=elem, | ||
| event_timestamp=datetime( | ||
| 2021, 3, 1, 0, 0, 19, 0, | ||
| tzinfo=pytz.UTC).timestamp()). | ||
| advance_watermark_to( | ||
| datetime(2021, 3, 1, 0, 0, 20, 0, | ||
| tzinfo=pytz.UTC).timestamp()).add_elements( | ||
| elements=elem, | ||
| event_timestamp=datetime( | ||
| 2021, 3, 1, 0, 0, 20, 0, | ||
| tzinfo=pytz.UTC).timestamp()).advance_watermark_to( | ||
| datetime( | ||
| 2021, 3, 1, 0, 0, 25, 0, tzinfo=pytz.UTC). | ||
| timestamp()).advance_watermark_to_infinity()) | ||
68 changes: 68 additions & 0 deletions
68
sdks/python/apache_beam/examples/sinks/test_periodicimpulse.py
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,68 @@ | ||
| # | ||
| # 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. | ||
| # | ||
|
|
||
| # To run the pipelines locally: | ||
| # python -m apache_beam.examples.sinks.test_periodicimpulse | ||
|
|
||
| # This file contains examples of writing unbounded PCollection using | ||
| # PeriodicImpulse to files | ||
|
|
||
| import argparse | ||
| import logging | ||
|
|
||
| import apache_beam as beam | ||
| from apache_beam.options.pipeline_options import PipelineOptions | ||
| from apache_beam.options.pipeline_options import SetupOptions | ||
| from apache_beam.runners.runner import PipelineResult | ||
| from apache_beam.transforms.window import FixedWindows | ||
|
|
||
|
|
||
| def run(argv=None, save_main_session=True) -> PipelineResult: | ||
| """Main entry point; defines and runs the wordcount pipeline.""" | ||
| parser = argparse.ArgumentParser() | ||
| _, pipeline_args = parser.parse_known_args(argv) | ||
|
|
||
| # We use the save_main_session option because one or more DoFn's in this | ||
| # workflow rely on global context (e.g., a module imported at module level). | ||
| pipeline_options = PipelineOptions(pipeline_args) | ||
| pipeline_options.view_as(SetupOptions).save_main_session = save_main_session | ||
|
|
||
| p = beam.Pipeline(options=pipeline_options) | ||
|
|
||
| _ = ( | ||
| p | ||
| | "Create elements" >> beam.transforms.periodicsequence.PeriodicImpulse( | ||
| start_timestamp=1, | ||
| stop_timestamp=100, | ||
| fire_interval=10, | ||
| apply_windowing=False) | ||
| | 'ApplyWindowing' >> beam.WindowInto(FixedWindows(20)) | ||
| | beam.io.WriteToText( | ||
| file_path_prefix="__output__/ouput_WriteToText", | ||
| file_name_suffix=".txt")) | ||
|
|
||
| # Execute the pipeline and return the result. | ||
| result = p.run() | ||
| result.wait_until_finish() | ||
| return result | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| logging.getLogger().setLevel(logging.INFO) | ||
| run() |
98 changes: 98 additions & 0 deletions
98
sdks/python/apache_beam/examples/sinks/test_write_bounded.py
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,98 @@ | ||
| # | ||
| # 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. | ||
| # | ||
|
|
||
| # To run the pipelines locally: | ||
| # python -m apache_beam.examples.sinks.test_write_bounded | ||
|
|
||
| # This file contains multiple examples of writing bounded PCollection to files | ||
|
|
||
| import argparse | ||
| import json | ||
| import logging | ||
|
|
||
| import pyarrow | ||
|
|
||
| import apache_beam as beam | ||
| from apache_beam.io.fileio import WriteToFiles | ||
| from apache_beam.io.textio import WriteToText | ||
| from apache_beam.options.pipeline_options import PipelineOptions | ||
| from apache_beam.options.pipeline_options import SetupOptions | ||
| from apache_beam.runners.runner import PipelineResult | ||
| from apache_beam.transforms.util import LogElements | ||
|
|
||
|
|
||
| def run(argv=None, save_main_session=True) -> PipelineResult: | ||
| """Main entry point; defines and runs the wordcount pipeline.""" | ||
| parser = argparse.ArgumentParser() | ||
| _, pipeline_args = parser.parse_known_args(argv) | ||
|
|
||
| # We use the save_main_session option because one or more DoFn's in this | ||
| # workflow rely on global context (e.g., a module imported at module level). | ||
| pipeline_options = PipelineOptions(pipeline_args) | ||
| pipeline_options.view_as(SetupOptions).save_main_session = save_main_session | ||
|
|
||
| p = beam.Pipeline(options=pipeline_options) | ||
|
|
||
| output = ( | ||
| p | beam.Create([{ | ||
| 'age': 10 | ||
| }, { | ||
| 'age': 20 | ||
| }, { | ||
| 'age': 30 | ||
| }]) | ||
| | beam.LogElements( | ||
| prefix='before write ', with_window=False, level=logging.INFO)) | ||
| #TextIO | ||
| output2 = output | 'Write to text' >> WriteToText( | ||
| file_path_prefix="__output_batch__/ouput_WriteToText", | ||
| file_name_suffix=".txt", | ||
| shard_name_template='-U-SSSSS-of-NNNNN') | ||
| _ = output2 | 'LogElements after WriteToText' >> LogElements( | ||
| prefix='after WriteToText ', with_window=False, level=logging.INFO) | ||
|
|
||
| #FileIO | ||
| output3 = ( | ||
| output | 'Serialize' >> beam.Map(json.dumps) | ||
| | 'Write to files' >> | ||
| WriteToFiles(path="__output_batch__/output_WriteToFiles")) | ||
| _ = output3 | 'LogElements after WriteToFiles' >> LogElements( | ||
| prefix='after WriteToFiles ', with_window=False, level=logging.INFO) | ||
|
|
||
| #ParquetIO | ||
| output4 = output | 'Write' >> beam.io.WriteToParquet( | ||
| file_path_prefix="__output_batch__/output_parquet", | ||
| schema=pyarrow.schema([('age', pyarrow.int64())])) | ||
| _ = output4 | 'LogElements after WriteToParquet' >> LogElements( | ||
| prefix='after WriteToParquet ', with_window=False, level=logging.INFO) | ||
| _ = output | 'Write parquet' >> beam.io.WriteToParquet( | ||
| file_path_prefix="__output_batch__/output_WriteToParquet", | ||
| schema=pyarrow.schema([('age', pyarrow.int64())]), | ||
| record_batch_size=10, | ||
| num_shards=0) | ||
|
|
||
| # Execute the pipeline and return the result. | ||
| result = p.run() | ||
| result.wait_until_finish() | ||
| return result | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| logging.getLogger().setLevel(logging.INFO) | ||
| run() |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
line to long