将 PCollection 分配回全局 window
Assign PCollection back to global window
我有一个管道,它接受一个有界的 PCollection,为其分配时间戳,然后 "windows" 将其放入 Sliding Windows。分组转换后,我想将生成的 PCollection 分配回全局 window。我一直无法弄清楚如何做到这一点。请参阅下面的示例光束伪代码:
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
打印 window 信息:
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?
完整代码here
我有一个管道,它接受一个有界的 PCollection,为其分配时间戳,然后 "windows" 将其放入 Sliding Windows。分组转换后,我想将生成的 PCollection 分配回全局 window。我一直无法弄清楚如何做到这一点。请参阅下面的示例光束伪代码:
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
打印 window 信息:
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?
完整代码here