【问题标题】:How does one access an apache_beam.io.fileio.ReadableFile() object?如何访问 apache_beam.io.fileio.ReadableFile() 对象?
【发布时间】:2020-11-19 13:38:12
【问题描述】:

我正在尝试使用 apache_beam.io.fileio 模块来读取文件 lines.txt 并将其合并到我的管道中。

lines.txt有以下内容:

line1
line2
line3

当我运行以下管道代码时:

with beam.Pipeline(options=pipeline_options) as p:

     lines = (
         p
         | beam.io.fileio.MatchFiles(file_pattern="lines.txt")
         | beam.io.fileio.ReadMatches()
     )
     # print file contents to screen
     lines | 'print to screen' >> beam.Map(print)

我得到以下输出:

<apache_beam.io.fileio.ReadableFile object at 0x000001A8C6C55F08>

我期待

line1
line2
line3

我怎样才能产生我的预期结果?

【问题讨论】:

    标签: python google-cloud-platform google-cloud-dataflow apache-beam


    【解决方案1】:

    PCollection 来自

    p
    | beam.io.fileio.MatchFiles(file_pattern="lines.txt")
    | beam.io.fileio.ReadMatches()
    

    是一个ReadableFile 对象。为了访问这个对象,我们可以使用apache beam pydoc中记录的各种函数。

    下面我们实现read_utf8()

    with beam.Pipeline(options=pipeline_options) as p:
    
        lines = (
            p
            | beam.io.fileio.MatchFiles(file_pattern="lines.txt")
            | beam.io.fileio.ReadMatches()
            | beam.Map(lambda file: file.read_utf8())
        )
        # print file contents to screen
        lines | 'print to screen' >> beam.Map(print)
    

    我们得到了预期的结果:

    line1
    line2
    line3
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-07
      • 2012-06-06
      相关资源
      最近更新 更多