Beam:Metrics.counter 未能创建计数器

Beam: Metrics.counter failed to create counter

beam.DoFn 定义如下,在 example 之后是 Metrics.counter

from apache_beam.metrics import Metrics


class ParseAndFilterFn(beam.DoFn):
    def __init__(self):
        super(ParseAndFilterFn, self).__init__()
        self.num_parse_errors = Metrics.counter(self.__class__, 'num_parse_errors')

    def process(self, element):
        text_line = element.strip()
        data = {}
        try:
            data = json.loads(text_line.decode('utf-8'))
            yield data['id']
        except Exception as ex:
            print("Parse json exception of ParseAndFilterFn:", ex)
            self.num_parse_errors.inc()

json.loads出现一处错误时,出现此错误AttributeError: 'ParseAndFilterFn' object has no attribute 'num_parse_errors' [while running 'ParseAndFilterFn']

我的代码有什么问题或我遗漏了什么?

光束版本:2.14.0

在 DoFn 中进行设置的正确方法是使用 setup