最新版本的 TextIO(2.11 及更高版本)是否能够从文件中并行读取行?

Does the latest version of TextIO (2.11 and above) have ability to read lines in parallel from a file?

我通读了可拆分的 DoFn 博客,据我所知,此功能已在 TextIO(用于云数据流运行器)中可用。我不清楚的是 - 使用 TextIO 我将能够并行读取给定文件中的行。

仅Java,TextIO 源将自动并行读取未压缩的文件。

这没有正式记录,但 TextIO 源是允许搜索的 FileBaseSource 的子类。这意味着如果工人决定拆分工作,它可以这样做。看FileBasedSource拆分的代码here.

Cubez 的回答很好。我还想补充一点,作为 PTransform 和 I/O 连接器的 TextIO 实现了 expand() 方法:

@Override
public PCollection<String> expand(PCollection<FileIO.ReadableFile> input) {
  return input.apply(
      "Read all via FileBasedSource",
      new ReadAllViaFileBasedSource<>(
          getDesiredBundleSizeBytes(),
          new CreateTextSourceFn(getDelimiter()),
          StringUtf8Coder.of()));
}

如果我们进一步观察,我们可以看到 ReadAllViaFileBasedSource class 也有 expand() 方法定义如下:

@Override
public PCollection<T> expand(PCollection<ReadableFile> input) {
return input
    .apply("Split into ranges", ParDo.of(new SplitIntoRangesFn(desiredBundleSizeBytes)))
    .apply("Reshuffle", Reshuffle.viaRandomKey())
    .apply("Read ranges", ParDo.of(new ReadFileRangesFn<>(createSource)))
    .setCoder(coder);

}

这就是底层运行器如何在执行器之间分配 PCollection 并并行读取的方式。