繁体   English   中英

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

[英]Write Kafka Stream output to multiple directory using Apache Beam

我想使用数据流将Kafka主题的数据保存到谷歌存储。

我在本地写了一个示例代码,它运行良好。

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();
}

上面的代码完美无缺。 但我想将每个窗口的输出保存在单独的目录中。

例如{BasePath} / {Window} / {prefix} {Suffice}

我无法让它发挥作用。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM