Dataflow 中带有时间戳的过程字段

Process field with timestamp in Dataflow

我从 Google Cloud Pub/Sub 接收消息,格式如下:

{u'date': u'2019-03-26T09:57:52Z', 'field1': value1, u'field2': u'value2', u'field3': u'value3', u'field4': u'value4',...}

而且我希望在使用 window:

的管道中处理此消息时
| 'Window' >> beam.WindowInto(window.FixedWindows(1 * 10))

字段'date'将被处理为window的参考时间戳。

我是否需要自定义 WindowFn 或应该如何完成?

您需要像这样指定自定义时间戳:

def custom_timestamp(message):
    # assuming that message is already parsed JSON (dict)
    import datetime as dt
    import apache_beam as beam
    ts = dt.datetime.strptime(message["date"], "%Y-%m-%dT%H:%M:%SZ")
    return beam.window.TimestampedValue(message, ts.timestamp())

然后:

| 'CustomTimestamp' >> beam.Map(custom_timestamp)
| 'Window' >> beam.WindowInto(window.FixedWindows(1 * 10))

您可以在此处找到完整的详细信息:https://beam.apache.org/documentation/programming-guide/#adding-timestamps-to-a-pcollections-elements

但是您必须注意,Apache Beam 的 Streaming Python SDK 有很多缺失的部分,并且有些功能没有像您预期的那样工作。我想实现与您完全相同的情况,在添加自定义时间戳后,DataFlow Runner 丢弃了我的消息,因为他们称之为 droppedDueToLateness .我仍然不确定是否可以设置系统水印以使用 PubSub 和 Python.

处理历史数据