Pubsub 消息缺少时间戳属性

vdo*_*lez 0 google-cloud-platform google-cloud-pubsub google-cloud-dataflow apache-beam

我正在尝试从 Apache Beam 读取 Pubsub 消息的时间戳值。

p.apply("Read PubSub messages", PubsubIO.readMessagesWithAttributes()
    .withIdAttribute("msg_id")
    .withTimestampAttribute("timestamp")
    .fromSubscription(options.getPubsubSubscription()))
Run Code Online (Sandbox Code Playgroud)

但不幸的是,我收到了以下错误,这真的让我感到惊讶,因为我认为每条消息都有一个默认时间戳。

An exception occured while executing the Java class. 
PubSub message is missing a value for timestamp attribute timestamp
Run Code Online (Sandbox Code Playgroud)

为什么我的消息没有时间戳?是因为我通过 Pubsub UI 发布了它吗?

Gui*_*ins 5

如果您省略 ,则每条 Pub/Sub 消息都将分配默认时间戳.withTimestampAttribute()。添加时.withTimestampAttribute("timestamp")意味着您将在每条消息的属性中提供时间戳timestamp。例如,使用用户界面:

在此输入图像描述

然后,窗口将与这些时间戳相关,如果您需要从管道内访问它,则可以使用ProcessContext.timestamp()(更多详细信息请参见此处)。