首页 > 解决方案 > 基于窗口和元素计数从数据流写入 GCS

问题描述

我正在尝试实现一个解决方案,我需要使用数据流将来自 pubsub 的数据(json)消息写入 GCS。我的问题与这个完全相似

我需要根据窗口或元素计数来编写。以下是上述问题中写入的代码示例:

windowedValues.apply(FileIO.<String, String>writeDynamic()
        .by(Event::getKey)
        .via(TextIO.sink())
        .to("gs://data_pipeline_events_test/events/")
        .withDestinationCoder(StringUtf8Coder.of())
        .withNumShards(1)
        .withNaming(key -> FileIO.Write.defaultNaming(key, ".json")));

该解决方案建议使用 FileIO.WriteDynamic 函数。但我无法理解 .by(Event::getKey) 的作用以及它的来源。非常感谢您对此的任何帮助。

标签: javagoogle-cloud-storagegoogle-cloud-dataflowgoogle-cloud-pubsubdataflow

解决方案


它根据事件的键将元素分成组。

据我了解,事件来自使用 KV 类的 PCollection,因为它具有 getKey 方法。

请注意 :: 是 Java 8 中包含的新运算符,用于引用类的方法。


推荐阅读