使用Apache Beam将流数据写入GCS

时间:2017-10-24 11:38:37

标签: google-cloud-storage google-cloud-dataflow apache-beam

如何使用Apache Beam中的TextIO将从PubSub接收的消息写入GCS中的文本文件?看到一些方法,比如withWindowedWrites()和withFilenamePolicy(),但在文档中找不到它的任何例子。

1 个答案:

答案 0 :(得分:4)

如果您使用的是Java SDK(BEAM 2.1.0),则可以使用以下示例。

PipelineOptions options = PipelineOptionsFactory.fromArgs(args)
                                                    .withValidation()
                                                    .as(PipelineOptions.class);

Pipeline pipeline = Pipeline.create(options);

pipeline.begin()
               .apply("PubsubIO",PubsubIO.readStrings()
                     .withTimestampAttribute("timestamp")
                     .fromSubscription("projects/YOUR-PROJECT/subscriptions/YOUR-SUBSCRIPTION"))
               .apply(Window.<String>into(FixedWindows.of(Duration.standardSeconds(30L))))
               .apply(TextIO.write().to("gs://YOUR-BUCKET").withWindowedWrites());

通过浏览TextIO.Write.expand(PCollection输入)中的“expand”方法,您可以看到SDK用于文件命名的默认值。具体来说,我将看一下DefaultFilenamePolicy.java