对于一个简单的概念证明,我尝试在两分钟的窗口中单击窗口数据。我只想从那里打印每个窗口的计数,以及窗口的边界到BigQuery。在运行管道时,我不断收到以下错误:
org.apache.beam.sdk.Pipeline$PipelineExecutionException: java.lang.RuntimeException: java.io.IOException: Insert failed: [{"errors":[{"debugInfo":"","location":"windowend","message":"This field is not a record.","reason":"invalid"}],"index":0}]
管道如下所示:
// Creating the pipeline
Pipeline p = Pipeline.create(options);
// Window items
PCollection<TableRow> counts = p.apply("ReadFromPubSub", PubsubIO.readStrings().fromTopic(options.getTopic()))
.apply("AddEventTimestamps", WithTimestamps.of(TotalCountPipeline::ExtractTimeStamp).withAllowedTimestampSkew(Duration.standardDays(10000)))
.apply("Window", Window.<String>into(
FixedWindows.of(Duration.standardHours(options.getWindowSize())))
.triggering(
AfterWatermark.pastEndOfWindow()
.withLateFirings(AfterPane.elementCountAtLeast(1)))
.withAllowedLateness(Duration.standardDays(10000))
.accumulatingFiredPanes())
.apply("CalculateSum", Combine.globally(Count.<String>combineFn()).withoutDefaults())
.apply("BigQueryFormat", ParDo.of(new FormatCountsFn()));
// Writing to BigQuery
counts.apply("WriteToBigQuery",BigQueryIO.writeTableRows()
.to(options.getOutputTable())
.withSchema(getSchema())
.withWriteDisposition(BigQueryIO.Write.WriteDisposition.WRITE_APPEND));
// Execute pipeline
p.run().waitUntilFinish();
static class FormatCountsFn extends DoFn<Long, TableRow> {
@ProcessElement
public void processElement(ProcessContext c, BoundedWindow window) {
TableRow row =
new TableRow()
.set("windowStart", window.maxTimestamp().toDateTime())
.set("count", c.element().intValue());
c.output(row);
}
}
灵感来自
this post