-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathside_input_example.py
More file actions
135 lines (115 loc) · 4.16 KB
/
side_input_example.py
File metadata and controls
135 lines (115 loc) · 4.16 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import json
import logging
import os
import time
from collections import namedtuple
from typing import Any
import apache_beam as beam
from apache_beam.io import fileio
from apache_beam import io, pipeline, pvalue
from apache_beam.options.pipeline_options import (
GoogleCloudOptions,
PipelineOptions,
StandardOptions,
TestDataflowOptions,
)
from testcontainers.google.pubsub import PubSubContainer
PROJECT = "test-project"
TOPIC = "test-topic"
SUBSCRIPTION = "test-subscription"
SIDE_INPUT_FILE = "pokemons.json"
logging.basicConfig(
format="%(asctime)s %(levelname)-8s %(message)s",
level=logging.INFO,
datefmt="%Y-%m-%d %H:%M:%S",
)
def setup_pubsub(pubsub):
pub = pubsub.get_publisher_client()
sub = pubsub.get_subscriber_client()
topic_path = pub.topic_path(PROJECT, TOPIC)
pub.create_topic(request={"name": pub.topic_path(PROJECT, TOPIC)})
with sub:
sub.create_subscription(
request={
"name": sub.subscription_path(PROJECT, SUBSCRIPTION),
"topic": topic_path,
}
)
return pub, sub
PokeTrainer = namedtuple("PokeTrainer", ["id", "name", "pokemons"])
PokeMon = namedtuple("PokeMon", ["name", "weight", "height"])
class Deserialize(beam.DoFn):
def process(self, element: bytes):
yield PokeTrainer(**json.loads(element.decode()))
class Debug(beam.DoFn):
def process(self, element):
logging.info(element)
yield element
class Lookup(beam.DoFn):
def process(self, element: PokeTrainer, side_input: dict[str, dict[str, Any]]):
pokemons = [side_input.get(p) for p in element.pokemons]
yield PokeTrainer(
element.id, element.name, [p for p in pokemons if p is not None]
)
if __name__ == "__main__":
with PubSubContainer() as pubsub:
os.environ["PUBSUB_EMULATOR_HOST"] = pubsub.get_pubsub_emulator_host()
pub, sub = setup_pubsub(pubsub)
subscription = sub.subscription_path(PROJECT, SUBSCRIPTION)
with open(SIDE_INPUT_FILE, "w") as file:
json.dump(
{
"pikachu": {"name": "pikachu", "weight": 1, "height": 1},
"charizard": {"name": "charizard", "weight": 2, "height": 2},
},
file,
)
input_data = [
{"id": 1, "name": "Ash Ketchum", "pokemons": ["pikachu", "charizard"]},
{"id": 3, "name": "Mike", "pokemons": ["charizard"]},
]
for item in input_data:
pub.publish(
topic=pub.topic_path(PROJECT, TOPIC),
data=json.dumps(item).encode(),
)
options = PipelineOptions()
options.view_as(StandardOptions).streaming = True
options.view_as(GoogleCloudOptions).no_auth = True
options.view_as(
TestDataflowOptions
).pubsubRootUrl = pubsub.get_pubsub_emulator_host()
p = pipeline.Pipeline(options=options)
pokemons = (
p
| fileio.MatchFiles(SIDE_INPUT_FILE)
| fileio.ReadMatches()
| beam.Map(lambda f: (json.loads(f.read_utf8())))
| "Debug2" >> beam.ParDo(Debug())
)
trainers = (
p
| "Read Events" >> io.ReadFromPubSub(subscription=subscription)
| "Deserialize" >> beam.ParDo(Deserialize())
| "Lookup" >> beam.ParDo(Lookup(), side_input=pvalue.AsSingleton(pokemons))
| "Debug1" >> beam.ParDo(Debug())
)
result = p.run()
for tick in range(1, 61):
time.sleep(1)
if tick == 30:
with open(SIDE_INPUT_FILE, "w") as file:
json.dump(
{
"onix": {"name": "onix", "weight": 3, "height": 3},
},
file,
)
pub.publish(
topic=pub.topic_path(PROJECT, TOPIC),
data=json.dumps(
{"id": 2, "name": "Rocko", "pokemons": ["onix"]},
).encode(),
)
logging.info("shutting down")
result.cancel()