Google cloud dataflow 将时间戳从内部对象分配给GenericRecord

Google cloud dataflow 将时间戳从内部对象分配给GenericRecord,google-cloud-dataflow,apache-beam,apache-beam-io,Google Cloud Dataflow,Apache Beam,Apache Beam Io,由于windows的原因,处理流式事件和在每小时存储桶中写入文件是一项挑战,因为来自即将到来的一小时的一些事件可能会进入以前的事件等等 我一直在研究ApacheBeam及其触发器,但我很难用时间戳管理触发器,如下所示 Window.<GenericRecord>into(FixedWindows.of(Duration.standardMinutes(1))) .triggering(AfterProcessingTime

由于windows的原因,处理流式事件和在每小时存储桶中写入文件是一项挑战,因为来自即将到来的一小时的一些事件可能会进入以前的事件等等

我一直在研究ApacheBeam及其触发器,但我很难用时间戳管理触发器,如下所示

Window.<GenericRecord>into(FixedWindows.of(Duration.standardMinutes(1)))
                    .triggering(AfterProcessingTime
                     .pastFirstElementInPane()
                     .plusDelayOf(Duration.standardSeconds(1)))
                    .withAllowedLateness(Duration.ZERO)
                    .discardingFiredPanes())
将POJO类与
long
字段一起使用不会触发任何事件,但是如果我将其交换为
Instant
类并正确地重新创建对象,则每当读取PubSub消息时都会引发以下错误

原因:java.lang.ClassCastException:org.apache.avro.generic.GenericData$Record无法强制转换为java.lang.Long

我还一直在考虑围绕GenericRecord创建一种包装器类,它包含一个时间戳,但需要在GenericRecord部分准备好用FileIO写入.parquet后使用它

我还必须使用哪些其他方式来使用水印触发器

编辑:在@Anton评论之后,我尝试了以下方法

.apply("Apply timestamps", WithTimestamps.of(
            (SerializableFunction<GenericRecord, Instant>) item -> new Instant(Long.valueOf(item.get("timestamp").toString())))
        .withAllowedTimestampSkew(Duration.standardSeconds(30)))

如果我理解正确的话,您希望根据作为对象一部分的时间戳打开窗口并触发。您可以查看
上下文。outputWithTimestamp()
对输入进行预处理,并在打开窗口之前分配自定义时间戳:或者可以使用
WithTimestamps
:@Anton感谢您的回复!尝试了这两种方法,但都不起作用,不知何故触发器可能仍在丢弃事件?对于带有
outputWithTimestamp
的错误,您可以更改
GetAllowedTimestamp
,就像我在本文中所做的那样,如果我理解正确,您希望基于对象的时间戳打开窗口并触发。您可以查看
上下文。outputWithTimestamp()
对输入进行预处理,并在打开窗口之前分配自定义时间戳:或者可以使用
WithTimestamps
:@Anton感谢您的回复!尝试了这两种方法,但都不起作用,不知何故,触发器可能仍在丢弃事件?对于带有
outputWithTimestamp
的错误,您可以更改
GetAllowedTimestamp
,就像我在本文中所做的那样
"{ \"name\": \"timestamp\", \"type\": \"long\", \"logicalType\": \"timestamp-micros\" },"
.apply("Apply timestamps", WithTimestamps.of(
            (SerializableFunction<GenericRecord, Instant>) item -> new Instant(Long.valueOf(item.get("timestamp").toString())))
        .withAllowedTimestampSkew(Duration.standardSeconds(30)))
Caused by: java.lang.IllegalArgumentException: Cannot output with timestamp 2019-06-12T18:59:58.609Z. Output timestamps must be no earlier than the timestamp of the current input (2019-06-12T18:59:59.848Z) minus the allowed skew (0 milliseconds). See the DoFn#getAllowedTimestampSkew() Javadoc for details on changing the allowed skew.