如何在Apache Beam 2.4中替换为FilenamePolicy?

时间:2018-06-19 14:04:01

标签: apache-beam

我正在尝试从Kafka源中读取数据,按时间戳进行分区,并使用Apache Beam 2.4写入GCS。我想为输出文件应用自定义FilenamePolicy

根据我在Stackoverflow上发现的信息,通过谷歌搜索,过去可以通过使用

.apply(TextIO.write()
                    .to("gs://somebucket/")
                    .withFilenamePolicy(new PerWindowFiles(prefix))
                    .withWindowedWrites()
                    .withNumShards(1));

withFilenamePolicy选项不再可用。在Beam 2.4中如何完成?

我尝试使用documentation中示例中的writeDynamic()中的FileIO功能-但我不明白为什么我的TextIO不被接受为输入:

enter image description here

1 个答案:

答案 0 :(得分:1)

withFilenamePolicy()在2.2中已删除

您现在可以使用更简单的语法编写示例

pipeline.apply(Create.of(...))
  .apply(TextIO.write()
    .to(new PerWindowFiles("gs://somebucket/"))
    .withTempDirectory(
        FileBasedSink.convertToFileResourceIfPossible("gs://somebucket/tmp"))
    .withWindowedWrites()
    .withNumShards(1));

使用自定义FileNamePolicy,您还需要明确指定withTempDirectory

在第二个示例(屏幕快照)中,您使用默认的TextIO.sink(),它是FileIO.Sink<String>来下沉Event s。您需要Sink<Event>的一个实例(这也将实现任何自定义文件命名),或者将Event::getPayloadContextful包裹起来,如下所示:

.apply(FileIO.<String, Event>writeDynamic()
  .by(Event.getEventType)
  .via(Contextful.fn(Event::getPayload))