Java 使用Apache Beam将Kafka流输出写入多个目录

Java 使用Apache Beam将Kafka流输出写入多个目录,java,apache-kafka,google-cloud-dataflow,apache-beam,Java,Apache Kafka,Google Cloud Dataflow,Apache Beam,我想使用数据流将Kafka主题中的数据持久化到google存储中 我已经在本地写了一个示例代码,它工作得很好 public static void main(String[] args) { PipelineOptions options = PipelineOptionsFactory.create(); Pipeline p = Pipeline.create(options); p.apply(KafkaIO.<Long, String>read().w

我想使用数据流将Kafka主题中的数据持久化到google存储中

我已经在本地写了一个示例代码,它工作得很好

public static void main(String[] args) {
    PipelineOptions options = PipelineOptionsFactory.create();
    Pipeline p = Pipeline.create(options);
    p.apply(KafkaIO.<Long, String>read().withBootstrapServers("localhost:9092").withTopic("my-topic")
            .withKeyDeserializer(LongDeserializer.class).withValueDeserializer(StringDeserializer.class))
            .apply(Window
                    .<KafkaRecord<Long, String>>
                    into(FixedWindows.of(Duration.standardMinutes(1)))
            )
            .apply(FlatMapElements.into(TypeDescriptors.strings())
                    .via((KafkaRecord<Long, String> line) -> TextUtil.splitLine(line.getKV().getValue())))
            .apply(Filter.by((String word) -> StringUtils.isNotEmpty(word))).apply(Count.perElement())
            .apply(MapElements.into(TypeDescriptors.strings())
                    .via((KV<String, Long> lineCount) -> lineCount.getKey() + ": " + lineCount.getValue()))
            .apply(TextIO.write().withWindowedWrites().withNumShards(1)
                    .to("resources/temp/wc-kafka-op/wc"));

    p.run().waitUntilFinish();
}
publicstaticvoidmain(字符串[]args){
PipelineOptions=PipelineOptionsFactory.create();
Pipeline p=Pipeline.create(选项);
p、 apply(KafkaIO.read().withbootstrapserver(“localhost:9092”).withTopic(“我的主题”)
.withKeyDeserializer(LongDeserializer.class).withValueDeserializer(StringDeserializer.class))
.应用(窗口)
.
进入(固定窗口,共(持续时间,标准分钟(1)))
)
.apply(FlatMapElements.into(typedescriptor.strings())
.via((kafkarecordline)->TextUtil.splitLine(line.getKV().getValue()))
.apply(Filter.by((String-word)->StringUtils.isNotEmpty(word)).apply(Count.perElement())
.apply(MapElements.into(typeDescriptor.strings())
.via((KV lineCount)->lineCount.getKey()+“:“+lineCount.getValue()))
.apply(TextIO.write().withWindowedWrites().withNumShards(1)
。致(“资源/临时/卡夫卡工作室/工作室”);
p、 run().waitUntilFinish();
}
上面的代码工作得很好。但我想将每个窗口的输出保存在单独的目录中

e、 g.{BasePath}/{Window}/{prefix}{Suffice}


我无法使其工作。

TextIO支持windowedWrites,您可以指定名称的派生方式。请参阅。

如果需要多个输出,则需要使用多个
TextIO.write()
pipeline我认为应该有一种方法动态生成目录名来写入数据。有两个接口filenamepolicy和动态目标。但是,javadoc并没有提供如何将其写入多个文件的方法。我仍然无法让它工作。你能分享字数的代码行来提取窗口窗格并获得正确的输出文件名吗。我能够参考scio链接,并能够获得所需的输出。