Skip to content

Instantly share code, notes, and snippets.

@PlugaruT
Created March 26, 2021 10:18
Show Gist options
  • Save PlugaruT/9af53bcb54ece94523a386147392ab4c to your computer and use it in GitHub Desktop.
Save PlugaruT/9af53bcb54ece94523a386147392ab4c to your computer and use it in GitHub Desktop.
import apache_beam as beam
from apache_beam.transforms.window import FixedWindows, GlobalWindows
events = [
("k1", {"a": 1, "b":2}),
("k1", {"a": 1, "b":2}),
("k2", {"a": 1, "b":2}),
("k2", {"a": 1, "b":2}),
("k3", {"a": 1, "b":2}),
("k4", {"a": 1, "b":2}),
]
class ToGlobalWindow(beam.DoFn):
def process(self, element):
yield GlobalWindows.windowed_value(element)
def main():
with beam.Pipeline() as p:
(
p | beam.Create(events) | beam.WindowInto(windowfn=FixedWindows(size=60)) | beam.GroupByKey() | beam.ParDo(ToGlobalWindow()) | beam.Map(print)
)
p.run()
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment