|
| 1 | +# Copyright 2019 Google LLC. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +# [START pubsub_to_gcs] |
| 16 | +import argparse |
| 17 | +import datetime |
| 18 | +import json |
| 19 | +import logging |
| 20 | + |
| 21 | +import apache_beam as beam |
| 22 | +import apache_beam.transforms.window as window |
| 23 | +from apache_beam.options.pipeline_options import PipelineOptions |
| 24 | + |
| 25 | + |
| 26 | +class GroupWindowsIntoBatches(beam.PTransform): |
| 27 | + """A composite transform that groups Pub/Sub messages based on publish |
| 28 | + time and outputs a list of dictionaries, where each contains one message |
| 29 | + and its publish timestamp. |
| 30 | + """ |
| 31 | + |
| 32 | + def __init__(self, window_size): |
| 33 | + # Convert minutes into seconds. |
| 34 | + self.window_size = int(window_size * 60) |
| 35 | + |
| 36 | + def expand(self, pcoll): |
| 37 | + return (pcoll |
| 38 | + # Assigns window info to each Pub/Sub message based on its |
| 39 | + # publish timestamp. |
| 40 | + | 'Window into Fixed Intervals' >> beam.WindowInto( |
| 41 | + window.FixedWindows(self.window_size)) |
| 42 | + | 'Add timestamps to messages' >> (beam.ParDo(AddTimestamps())) |
| 43 | + # Use a dummy key to group the elements in the same window. |
| 44 | + # Note that all the elements in one window must fit into memory |
| 45 | + # for this. If the windowed elements do not fit into memory, |
| 46 | + # please consider using `beam.util.BatchElements`. |
| 47 | + # https://beam.apache.org/releases/pydoc/current/apache_beam.transforms.util.html#apache_beam.transforms.util.BatchElements |
| 48 | + | 'Add Dummy Key' >> beam.Map(lambda elem: (None, elem)) |
| 49 | + | 'Groupby' >> beam.GroupByKey() |
| 50 | + | 'Abandon Dummy Key' >> beam.MapTuple(lambda _, val: val)) |
| 51 | + |
| 52 | + |
| 53 | +class AddTimestamps(beam.DoFn): |
| 54 | + |
| 55 | + def process(self, element, publish_time=beam.DoFn.TimestampParam): |
| 56 | + """Processes each incoming windowed element by extracting the Pub/Sub |
| 57 | + message and its publish timestamp into a dictionary. `publish_time` |
| 58 | + defaults to the publish timestamp returned by the Pub/Sub server. It |
| 59 | + is bound to each element by Beam at runtime. |
| 60 | + """ |
| 61 | + |
| 62 | + yield { |
| 63 | + 'message_body': element.decode('utf-8'), |
| 64 | + 'publish_time': datetime.datetime.utcfromtimestamp( |
| 65 | + float(publish_time)).strftime("%Y-%m-%d %H:%M:%S.%f"), |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | +class WriteBatchesToGCS(beam.DoFn): |
| 70 | + |
| 71 | + def __init__(self, output_path): |
| 72 | + self.output_path = output_path |
| 73 | + |
| 74 | + def process(self, batch, window=beam.DoFn.WindowParam): |
| 75 | + """Write one batch per file to a Google Cloud Storage bucket. """ |
| 76 | + |
| 77 | + ts_format = '%H:%M' |
| 78 | + window_start = window.start.to_utc_datetime().strftime(ts_format) |
| 79 | + window_end = window.end.to_utc_datetime().strftime(ts_format) |
| 80 | + filename = '-'.join([self.output_path, window_start, window_end]) |
| 81 | + |
| 82 | + with beam.io.gcp.gcsio.GcsIO().open(filename=filename, mode='w') as f: |
| 83 | + for element in batch: |
| 84 | + f.write('{}\n'.format(json.dumps(element)).encode('utf-8')) |
| 85 | + |
| 86 | + |
| 87 | +def run(input_topic, output_path, window_size=1.0, pipeline_args=None): |
| 88 | + # `save_main_session` is set to true because some DoFn's rely on |
| 89 | + # globally imported modules. |
| 90 | + pipeline_options = PipelineOptions( |
| 91 | + pipeline_args, streaming=True, save_main_session=True) |
| 92 | + |
| 93 | + with beam.Pipeline(options=pipeline_options) as pipeline: |
| 94 | + (pipeline |
| 95 | + | 'Read PubSub Messages' >> beam.io.ReadFromPubSub(topic=input_topic) |
| 96 | + | 'Window into' >> GroupWindowsIntoBatches(window_size) |
| 97 | + | 'Write to GCS' >> beam.ParDo(WriteBatchesToGCS(output_path))) |
| 98 | + |
| 99 | + |
| 100 | +if __name__ == '__main__': # noqa |
| 101 | + logging.getLogger().setLevel(logging.INFO) |
| 102 | + |
| 103 | + parser = argparse.ArgumentParser() |
| 104 | + parser.add_argument( |
| 105 | + '--input_topic', |
| 106 | + help='The Cloud Pub/Sub topic to read from.\n' |
| 107 | + '"projects/<PROJECT_NAME>/topics/<TOPIC_NAME>".') |
| 108 | + parser.add_argument( |
| 109 | + '--window_size', |
| 110 | + type=float, |
| 111 | + default=1.0, |
| 112 | + help='Output file\'s window size in number of minutes.') |
| 113 | + parser.add_argument( |
| 114 | + '--output_path', |
| 115 | + help='GCS Path of the output file including filename prefix.') |
| 116 | + known_args, pipeline_args = parser.parse_known_args() |
| 117 | + |
| 118 | + run(known_args.input_topic, known_args.output_path, known_args.window_size, |
| 119 | + pipeline_args) |
| 120 | +# [END pubsub_to_gcs] |
0 commit comments