Repository navigation
Create batch_process_storage.py sample #14993
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,63 @@ | ||||||||||||||||||||||||||
| #!/usr/bin/env python | ||||||||||||||||||||||||||
| # Copyright 2026 Google LLC | ||||||||||||||||||||||||||
| # | ||||||||||||||||||||||||||
| # Licensed 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. | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| # [START dataflow_batch_process_storage] | ||||||||||||||||||||||||||
| import argparse | ||||||||||||||||||||||||||
| from typing import List, Optional, Tuple | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| import apache_beam as beam | ||||||||||||||||||||||||||
| from apache_beam.options.pipeline_options import PipelineOptions | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def parse_record(line: str) -> Tuple[str, float]: | ||||||||||||||||||||||||||
| """Parses a CSV line into a (product, amount) tuple.""" | ||||||||||||||||||||||||||
| _, product, amount = line.split(",") | ||||||||||||||||||||||||||
| return product, float(amount) | ||||||||||||||||||||||||||
|
Comment on lines
+24
to
+27
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Parsing CSV lines directly using Using a generator that yields results only on successful parsing, combined with
Suggested change
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def run(argv: Optional[List[str]] = None) -> None: | ||||||||||||||||||||||||||
| parser = argparse.ArgumentParser() | ||||||||||||||||||||||||||
| parser.add_argument( | ||||||||||||||||||||||||||
| "--input", | ||||||||||||||||||||||||||
| required=True, | ||||||||||||||||||||||||||
| help="Bucket path or glob, such as gs://bucket/input/*.csv", | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
| parser.add_argument( | ||||||||||||||||||||||||||
| "--output", | ||||||||||||||||||||||||||
| required=True, | ||||||||||||||||||||||||||
| help="Bucket path prefix, such as gs://bucket/output/sales", | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
| known_args, pipeline_args = parser.parse_known_args(argv) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| with beam.Pipeline(options=PipelineOptions(pipeline_args)) as pipeline: | ||||||||||||||||||||||||||
| ( | ||||||||||||||||||||||||||
| pipeline | ||||||||||||||||||||||||||
| | "ReadFromGCS" | ||||||||||||||||||||||||||
| >> beam.io.ReadFromText(known_args.input, skip_header_lines=1) | ||||||||||||||||||||||||||
| | "Parse" >> beam.Map(parse_record) | ||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||||||||||||||||||
| | "SumByProduct" >> beam.CombinePerKey(sum) | ||||||||||||||||||||||||||
| | "Format" >> beam.MapTuple(lambda product, total: f"{product},{total:.2f}") | ||||||||||||||||||||||||||
| | "WriteToGCS" | ||||||||||||||||||||||||||
| >> beam.io.WriteToText( | ||||||||||||||||||||||||||
| known_args.output, | ||||||||||||||||||||||||||
| file_name_suffix=".csv", | ||||||||||||||||||||||||||
| header="product,total_sales", | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if __name__ == "__main__": | ||||||||||||||||||||||||||
| run() | ||||||||||||||||||||||||||
| # [END dataflow_batch_process_storage] | ||||||||||||||||||||||||||
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.
Import
Iterablefromtypingto support the updated type signature forparse_recordwhen yielding elements.