【问题标题】:Does the latest version of TextIO (2.11 and above) have ability to read lines in parallel from a file?最新版本的 TextIO(2.11 及更高版本)是否能够从文件中并行读取行?
【发布时间】:2020-02-05 00:38:47
【问题描述】:

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

【问题讨论】:

    标签: google-cloud-dataflow apache-beam


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      Cubez 的回答很好。我还想补充一点,TextIO 既是 PTransform 又是 I/O 连接器,实现了 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 类也有如下定义的 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 并并行读取。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-11-19
        • 1970-01-01
        • 2018-05-01
        • 2017-09-15
        • 2014-03-10
        • 1970-01-01
        • 2022-11-23
        相关资源
        最近更新 更多