在Apache Beam(Python)中将ToList输出用作AsSingleton或AsList的输入

时间:2019-04-14 14:29:23

标签: python-3.x apache-beam

当尝试使用beam.combiners.ToList的输出作为beam.pvalue.AsSingleton或beam.pvalue.AsList的输入以尝试侧面输入时,出现了意外错误。我可以使用单个数字(例如:列表的均值)作为辅助输入,但是对于列表和字典,我遇到了例外。对于beam.pvalue.AsSingleton,我得到:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-4-0c1df7400a03> in <module>
     15 chain_total = chain_1 | chain_2
     16 
---> 17 chain_1 | beam.Map(m, beam.pvalue.AsList(chain_2))
     18 
     19 chain_total | beam.Map(print)

~/.cache/pypoetry/virtualenvs/prototyping-with-tensorflow-py3.6/lib/python3.6/site-packages/apache_beam/pvalue.py in __init__(self, pcoll)
    297     self.pvalue = pcoll
    298     self._window_mapping_fn = sideinputs.default_window_mapping_fn(
--> 299         pcoll.windowing.windowfn)
    300 
    301   def _view_options(self):

AttributeError: '_ChainedPTransform' object has no attribute 'windowing'

对于beam.pvalue.AsList,我得到:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-7-0c1df7400a03> in <module>
     15 chain_total = chain_1 | chain_2
     16 
---> 17 chain_1 | beam.Map(m, beam.pvalue.AsList(chain_2))
     18 
     19 chain_total | beam.Map(print)

~/.cache/pypoetry/virtualenvs/prototyping-with-tensorflow-py3.6/lib/python3.6/site-packages/apache_beam/pvalue.py in __init__(self, pcoll)
    297     self.pvalue = pcoll
    298     self._window_mapping_fn = sideinputs.default_window_mapping_fn(
--> 299         pcoll.windowing.windowfn)
    300 
    301   def _view_options(self):

AttributeError: '_ChainedPTransform' object has no attribute 'windowing'

这是我正在运行的代码

import apache_beam as beam


def m(x, u):
    print(u)
    return x


p = beam.Pipeline()

data_beam = Create(['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c'])

chain_1 = p | data_beam | beam.combiners.Count.PerElement()
chain_2 = beam.Map(lambda x: x[0]) | beam.combiners.ToList()

chain_total = chain_1 | chain_2

chain_1 | beam.Map(m, beam.pvalue.AsSingleton(chain_2))

chain_total | beam.Map(print)

p.run()

将beam.pvalue.AsSingleton替换为beam.pvalue.AsList以获得另一个错误。我正在使用Apache Beam python SDK版本2.11.0。

1 个答案:

答案 0 :(得分:1)

PCollections是Beam中的名词,PTransforms是动词。 当您开始管道时,p = beam.Pipeline()是您唯一的名词。 (注:Create是动词。)

通过对该名词应用各种动词,可以根据以下规则创建其他名词:

  • new_noun = existing_noun | verb

出现混乱的主要原因是因为您还可以将动词链接在一起:

  • fancy_verb = verb1 | verb2

虽然这些示例中的语法看起来非常相似,但是返回的值具有不同的类型。

这里的主要问题是只有名词可以被视为SideInputs。

在提供的示例中,chain_2是通过组合两个动词创建的动词,并且错误消息确认_ChainedPTransform(实际上是kind of PTransform,顾名思义)无法通过到任何AsSideInput函数。