首页 > 解决方案 > DataflowPipelineOptions:在 arg 解析后使用 .setRunner 显式设置运行器

问题描述

在数据流 SDK 2.4.0 中,我曾经能够运行:

 PipelineOptionsFactory.register(MyPipelineOptions.class);
 MyPipelineOptions options = PipelineOptionsFactory.fromArgs(args).withValidation().create().as(MyPipelineOptions.class);
 options.setRunner(DataflowRunner.class);

但是,在 Apache Beam SDK 2.9.0 中,我需要使用 args 设置运行器--runner=DataflowRunner,否则它会失败create()(第二行)。setRunner这是期望的行为,还是在从 args 读取其他选项后仍然可以设置跑步者使用?

使用的 Maven 依赖项:

<dependencies>
    <dependency>
        <groupId>org.apache.beam</groupId>
        <artifactId>beam-sdks-java-core</artifactId>
        <version>2.9.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.beam</groupId>
        <artifactId>beam-runners-google-cloud-dataflow-java</artifactId>
        <version>2.9.0</version>
    </dependency>
</dependencies>

标签: google-cloud-dataflowapache-beam

解决方案


显然,如果在创建选项对象时没有指定运行器,则默认为 DirectRunner。因此,即使稍后使用 setRunner 定义另一个运行器,只是为了能够创建未指定运行器的选项对象,DirectRunner 也需要在类路径中。

要使其工作,您只需添加依赖项即可在您的类路径中包含 DirectRunner

<dependency>
  <groupId>org.apache.beam</groupId>
  <artifactId>beam-runners-direct-java</artifactId>
  <version>2.9.0</version>
</dependency>

推荐阅读