在 DataFlow 中索引 PCollection

Indexing a PCollection in DataFlow

我在 Cloud Dataflow 中构建了一个 PCollection,我将按原样写入磁盘。我想构建另一个集合,通过索引引用第一个集合中的项目。例如

PC1:

strings go here
some other string here
more strings

PC2:

0,1
1,1
0,2

我不确定如何在不编写整个管道并启动另一个管道的情况下获取 PC1 中的索引,即便如此我也不确定如何记录正在读取的 line/record 编号。简单地使用静态变量是否安全?我假设不是基于平台的一般并行性质。

PCollection 本质上是无序的,因此没有 "index of an item in a collection" 这样的东西 - 但是,您可以在元素本身中包含行号:让 PC1 成为 PCollection<KV<Integer, String>> 其中 Integer 是行号——基本上是从文本文件中读取与其行号配对的行。

我们目前不提供执行此操作的内置源 - 您最好的选择是编写一个简单的 DoFn<String, KV<Integer, String>> 将文件名作为输入并使用 IOChannelFactory 打开文件并逐行读取并发出带有行号的内容以生成 PC1.