-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Expand file tree
/
Copy pathpubsub_it_pipeline.py
More file actions
94 lines (80 loc) · 3.13 KB
/
pubsub_it_pipeline.py
File metadata and controls
94 lines (80 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#
# 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.
#
"""
Test pipeline for use by pubsub_integration_test.
"""
# pytype: skip-file
import argparse
import apache_beam as beam
from apache_beam.options.pipeline_options import PipelineOptions
from apache_beam.options.pipeline_options import StandardOptions
def run_pipeline(argv, with_attributes, id_label, timestamp_attribute):
"""Build and run the pipeline."""
parser = argparse.ArgumentParser()
parser.add_argument(
'--output_topic',
required=True,
help=(
'Output PubSub topic of the form '
'"projects/<PROJECT>/topic/<TOPIC>".'))
parser.add_argument(
'--input_subscription',
required=True,
help=(
'Input PubSub subscription of the form '
'"projects/<PROJECT>/subscriptions/<SUBSCRIPTION>."'))
known_args, pipeline_args = parser.parse_known_args(argv)
pipeline_options = PipelineOptions(pipeline_args)
pipeline_options.view_as(StandardOptions).streaming = True
p = beam.Pipeline(options=pipeline_options)
runner_name = type(p.runner).__name__
# Read from PubSub into a PCollection.
if runner_name == 'TestDirectRunner':
messages = p | beam.io.ReadFromPubSub(
subscription=known_args.input_subscription,
with_attributes=with_attributes,
timestamp_attribute=timestamp_attribute)
else:
messages = p | beam.io.ReadFromPubSub(
subscription=known_args.input_subscription,
id_label=id_label,
with_attributes=with_attributes,
timestamp_attribute=timestamp_attribute)
def add_attribute(msg, timestamp=beam.DoFn.TimestampParam):
msg.data += b'-seen'
msg.attributes['processed'] = 'IT'
if timestamp_attribute in msg.attributes:
msg.attributes[timestamp_attribute + '_out'] = timestamp.to_rfc3339()
return msg
def modify_data(data):
return data + b'-seen'
if with_attributes:
output = messages | 'add_attribute' >> beam.Map(add_attribute)
else:
output = messages | 'modify_data' >> beam.Map(modify_data)
# Write to PubSub.
if runner_name == 'TestDirectRunner':
_ = output | beam.io.WriteToPubSub(
known_args.output_topic, with_attributes=with_attributes)
else:
_ = output | beam.io.WriteToPubSub(
known_args.output_topic,
id_label=id_label,
with_attributes=with_attributes,
timestamp_attribute=timestamp_attribute)
result = p.run()
result.wait_until_finish()