【问题标题】:Apache Beam explaination of ParDo behaviourParDo 行为的 Apache Beam 解释
【发布时间】:2018-07-19 10:33:27
【问题描述】:

采用 ndjson 格式的文本文件,以下代码会产生我所期望的结果。一个ndjson 文件,其中quotes.USD 字典未嵌套,原始quotes 元素已删除。

  def unnest_quotes(element):
      element['USDquotes'] = element['quotes']['USD']
      del element['quotes']
      return element

  p = beam.Pipeline(options=pipeline_options)
  ReadJson = p | ReadFromText(known_args.input,coder=JsonCoder())
  MapFormattedJson = ReadJson | 'Map Function' >> beam.Map(unnest_quotes)
  MapFormattedJson | 'Write Map Output' >> WriteToText(known_args.output,coder=JsonCoder())

但是,当我尝试使用 ParDo 实现相同的目标时,我不理解这种行为。

  class UnnestQuotes(beam.DoFn):
    def process(self,element):
      element['USDquotes'] = element['quotes']['USD']
      del element['quotes']
      return element

  p = beam.Pipeline(options=pipeline_options)
  ReadJson = p | ReadFromText(known_args.input,coder=JsonCoder())
  ClassFormattedJson = ReadJson | 'Pardo' >> beam.ParDo(UnnestQuotes())
  ClassFormattedJson | 'Write Class Output' >> WriteToText(known_args.output,coder=JsonCoder())

这会生成一个文件,其中 dict 的每个键位于单独的行上,没有值,如下所示。

"last_updated"
"name"
"symbol"
"rank"
"total_supply"
"max_supply"
"circulating_supply"
"website_slug"
"id"
"USDquotes"

就好像 Map 函数生成的 PCollection 是完整的字典,而 Pardo 为每个键生成一个 PCollection。

我知道我可以只使用 map 函数,但我需要了解这种行为,以备将来需要使用 ParDo 时使用。

【问题讨论】:

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


    【解决方案1】:

    我在这个答案的帮助下想通了。 apache beam flatmap vs map

    正如我所经历的,与 FlatMap 和 Map 之间的区别相同。为了获得所需的行为,我需要做的就是将 Pardo 的返回值包装在一个列表中。

      class UnnestQuotes(beam.DoFn):
        def process(self,element):
          element['USDquotes'] = element['quotes']['USD']
          del element['quotes']
          return [element]
    

    【讨论】:

    • 您愿意接受您自己的答案吗?供社区日后参考。我相信你必须在发布后等待 2 天。
    • 有关于这个问题的更新吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多