python - Apache Beam/Dataflow 作业中是否可以有非并行步骤?

标签 python google-cloud-dataflow apache-beam

假设我在 GCP 中有一个 python 数据流作业,它执行以下两件事:

  • 从 BigQuery 获取一些数据

  • 调用外部 API 以获取特定值,并根据获取的值过滤来自 BigQuery 的数据

我能够做到这一点,但是对于第二步,我想出如何实现它的唯一方法是将其作为扩展 DoFn 的类,并稍后以并行方式调用它:

class CallExternalServiceAndFilter(beam.DoFn):
    def to_runner_api_parameter(self, unused_context):
        pass

    def process(self, element, **kwargs):
        # here I have to make the http call and figure out whether to yield the element or not,
        # however this happens for each element of the set, as expected.
        if element['property'] < response_body_parsed['some_other_property']:
            logging.info("Yielding element")
            yield element
        else:
            logging.info("Not yielding element")
with beam.Pipeline(options=PipelineOptions(), argv=argv) as p:
    rows = p | 'Read data' >> beam.io.Read(beam.io.BigQuerySource(
        dataset='test',
        project=PROJECT,
        query='Select * from test.table'
    ))

    rows = rows | 'Calling external service and filtering items' >> beam.ParDo(CallExternalServiceAndFilter())

    # ...

有什么方法可以让 API 调用一次,然后在并行过滤步骤中使用结果吗?

最佳答案

使用__init__函数。

class CallExternalServiceAndFilter(beam.DoFn):
    def __init__():
        self.response_body_parsed = call_api()

    def to_runner_api_parameter(self, unused_context):
        pass

    def process(self, element, **kwargs):
        # here I have to make the http call and figure out whether to yield the element or not,
        # however this happens for each element of the set, as expected.
        if element['property'] < self.response_body_parsed['some_other_property']:
            logging.info("Yielding element")
            yield element
        else:
            logging.info("Not yielding element")

或者更好的是,只需预先调用您的 API(在构建管道的本地计算机上),并在 __init__ 中分配值。

reponse_body_parsed = call_api()

class CallExternalServiceAndFilter(beam.DoFn):
    def __init__():
        self.response_body_parsed = reponse_body_parsed

    def to_runner_api_parameter(self, unused_context):
        pass

    def process(self, element, **kwargs):
        # here I have to make the http call and figure out whether to yield the element or not,
        # however this happens for each element of the set, as expected.
        if element['property'] < self.response_body_parsed['some_other_property']:
            logging.info("Yielding element")
            yield element
        else:
            logging.info("Not yielding element")

您说过使用setup仍然会进行多次调用。 __init__ 仍然是这种情况吗(如果您在 DoFn 中执行 API 调用,而不是事先执行)?我仍然不清楚 __init__ 和 setup 之间的区别。

关于python - Apache Beam/Dataflow 作业中是否可以有非并行步骤?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57568242/

相关文章:

python-3.x - 旁加载静态数据

java - 如何使用 Apache Beam 管理背压

python - Matplotlib 和 Pandas 改变负值的颜色

Python从时间戳获取小时

python - 如何在 django 模型中保持 Integerfield 的固定长度

python - 如何使用 Apache Beam 中的运行时值提供程序写入 Big Query?

google-cloud-dataflow - Dataflow 的高可用性和地理冗余

python - 导入错误 : cannot import name 'imshow'

python - 将 AWS 凭证传递给 Google Cloud Dataflow,Python

java - Apache 梁 WithTimestamps : Output timestamps must be no earlier than timestamp of current input