DoFn的处理功能未执行

时间:2019-06-10 21:13:53

标签: apache-beam

我正在尝试编写诸如

的波束变换

util.py

class GroupIntoBatches(PTransform):
  def __init__(self, batch_size):
    self.batch_size = batch_size

  @staticmethod
  def of_size(batch_size):
    return GroupIntoBatches(batch_size)

  def expand(self, pcoll):
    input_coder = coders.registry.get_coder(pcoll)
    if not input_coder.is_kv_coder():
          raise ValueError(
            'coder specified in the input PCollection is not a KvCoder')
    key_coder = input_coder.key_coder()
    value_coder = input_coder.value_coder()

    return pcoll | ParDo(_GroupIntoBatchesDoFn(self.batch_size, key_coder, value_coder))


class _GroupIntoBatchesDoFn(DoFn):
    def __init__(self, batch_size, input_key_coder, input_value_coder):
      self.batch_size = batch_size
      self.batch_spec = BagStateSpec("GroupIntoBatches", input_value_coder)

    def process(self, element):
      raise Exception("Not getting to this point") # This is not working
      print element

尝试通过测试用例执行此转换

util_test.py

class GroupIntoBatchesTest(unittest.TestCase):
  NUM_ELEMENTS = 10
  BATCH_SIZE = 5

  @staticmethod
  def _create_test_data():
    scientists = [
      "Einstein",
      "Darwin",
      "Copernicus",
      "Pasteur",
      "Curie",
      "Faraday",
      "Newton",
      "Bohr",
      "Galilei",
      "Maxwell"
    ]

    data = []
    for i in range(GroupIntoBatchesTest.NUM_ELEMENTS):
      index = i % len(scientists)
      data.append(("key", scientists[index]))
    return data

  def test_in_global_window(self):
    pipeline = TestPipeline()
    collection = pipeline | beam.Create(GroupIntoBatchesTest._create_test_data()) | util.GroupIntoBatches.of_size(GroupIntoBatchesTest.BATCH_SIZE)

我的问题是process函数未在我的_GroupIntoBatchesDoFn上调用的原因是什么

我在运行测试用例时得到了这个结果

  

test_in_global_window   (apache_beam.transforms.util_test.GroupIntoBatchesTest)...确定

1 个答案:

答案 0 :(得分:1)

您的测试正在构建管道,但实际上并未执行。您需要写

pipeline = TestPipeline()
collection = pipeline | ...
pipeline.run()

或者,或者

with TestPipeline() as pipeline:
    collection = pipeline | ...
# run is implicitly called on exit of the with block

(您可能还对BatchElements转换感兴趣。)

相关问题