将 PCollection 分配回全局窗口



我有一个管道,它采用有界的PCollection,为其分配时间戳并将其"窗口"到滑动窗口中。分组转换后,我想将生成的 PCollection 分配回全局窗口。我一直无法弄清楚如何做到这一点。请参阅下面的示例光束伪代码:

import apache_beam as beam
with beam.Pipeline() as p:
(
p
| beam.io.ReadFromText()
| beam.ParDo(AddTimestampDoFn())
| beam.WindowInto(beam.window.SlidingWindows(60, 60))
| beam.GroupByKey()
| beam.ParDo(SomethingElse()
| beam.WindowInto(GlobalWindow()) # Here is where I want to bring back to global window
)

关于如何去做的任何想法?

使用beam.WindowInto(window.GlobalWindows())应该有效。例如,通过此快速测试:

data = [{'message': 'Hi', 'timestamp': time.time()}]
events = (p
| 'Create Events' >> beam.Create(data) 
| 'Add Timestamps' >> beam.Map(lambda x: beam.window.TimestampedValue(x, x['timestamp'])) 
| 'Sliding Windows'   >> beam.WindowInto(beam.window.SlidingWindows(60, 60)) 
| 'First window' >> beam.ParDo(DebugPrinterFn()) 
| 'global Window'   >> beam.WindowInto(window.GlobalWindows()) 
| 'Second window'   >> beam.ParDo(DebugPrinterFn()))

其中DebugPrinterFn打印窗口信息:

class DebugPrinterFn(beam.DoFn):
"""Just prints the element and window"""
def process(self, element, window=beam.DoFn.WindowParam):
logging.info("Received message %s in window=%s", element['message'], window)
yield element

我得到以下输出:

INFO:root:Received message Hi in window=[1575565500.0, 1575565560.0)
INFO:root:Received message Hi in window=GlobalWindow

使用DirectRunner和 2.16.0 SDK 进行测试。如果它不适合您:

  • 你得到任何错误吗?
  • 您使用的是哪种运行器和 SDK?

完整代码在这里

相关内容

  • 没有找到相关文章

最新更新