Bal*_*alu 4 google-cloud-dataflow apache-beam apache-beam-io
我看过很多关于同一主题的问题。但是,我仍然遇到写入 GCS 的问题。我正在从 pubsub 阅读该主题并尝试将其推送到 GCS。我已经提到了这个链接。但是,在最新的束包中找不到 IOChannelUtils。
PCollection<String> details = pipeline
.apply(PubsubIO.readStrings().fromTopic("/topics/<project>/sampleTopic"));
PCollection<KV<String, String>> keyedStream = details.apply(WithKeys.of(new SerializableFunction<String, String>() {
public String apply(String s) {
return "constant";
}
}));
PCollection<KV<String, Iterable<String>>> keyedWindows = keyedStream.apply(Window.<KV<String, String>>into(FixedWindows.of(ONE_MIN)).withAllowedLateness(ONE_DAY)
.triggering(AfterWatermark.pastEndOfWindow().withEarlyFirings(AfterPane.elementCountAtLeast(10))
.withLateFirings(AfterFirst.of(AfterPane.elementCountAtLeast(10),
AfterProcessingTime.pastFirstElementInPane().plusDelayOf(TEN_SECONDS))))
.discardingFiredPanes()).apply(GroupByKey.create());
PCollection<Iterable<String>> windows = keyedWindows.apply(Values.create());
Run Code Online (Sandbox Code Playgroud)
这是我从堆栈溢出中的许多其他类似主题中获取的。现在,我明白了,TextIO 确实支持 withWindowedWrites 和 withNumShards 的无界 PCollection 写入选项。
参考:使用 DoFn 使用 Cloud Dataflow 从 PubSub 写入 Google Cloud Storage
但是,我不明白,我该怎么做。
我正在尝试按如下方式写入 GCS。
FilenamePolicy policy = DefaultFilenamePolicy.constructUsingStandardParameters(
StaticValueProvider.of(outputDirectory), DefaultFilenamePolicy.DEFAULT_SHARD_TEMPLATE, "");
details.apply(TextIO.write().to("gs://<bucket>/topicfile").withWindowedWrites()
.withFilenamePolicy(policy).withNumShards(4));
Run Code Online (Sandbox Code Playgroud)
我没有足够的点数来为 Stack Overflow 中的这些主题添加评论,因此我将其作为一个不同的问题提出。
我可以通过修改窗口来解决这个问题,如下所示
PCollection<String> streamedDataWindows = streamedData.apply(Window.<String>into(new GlobalWindows())
.triggering(Repeatedly
.forever(AfterProcessingTime
.pastFirstElementInPane()
.plusDelayOf(Duration.standardSeconds(30))
)).withAllowedLateness(Duration.standardDays(1)).discardingFiredPanes());
streamedDataWindows.apply(TextIO.write().to(CLOUD_STORAGE).withWindowedWrites().withNumShards(1).withFilenamePolicy(new PerWindowFiles()));
public static class PerWindowFiles extends FileBasedSink.FilenamePolicy {
public ResourceId windowedFilename(ResourceId outputDirectory, WindowedContext context, String extension) {
// OVERRIDE THE FILE NAME CREATION
}
}
Run Code Online (Sandbox Code Playgroud)
虽然我可以这样解决它,但我仍然不确定这里的窗口概念。当我找到它时,我会添加更多细节。如果有人有更多的理解,请补充更多细节。谢谢
| 归档时间: |
|
| 查看次数: |
4000 次 |
| 最近记录: |