How to get PipelineOptions in composite PTransform in Beam 2.0?

纵饮孤独 提交于 2019-12-23 15:25:11

问题


After upgrading to Beam 2.0 the Pipeline class doesn't have getOptions() class anymore. I have a composite PTransform that relies on getting the options in the its expand method:

public class MyCompositeTransform extends PTransform<PBegin, PDone> {
    @Override
    public PDone expand(PBegin input) {
        Pipeline pipeline = input.getPipeline();
        MyPipelineOptions options = pipeline.getOptions().as(MyPipelineOptions.class);
        ...
    }
}

In Beam 2.0 there doesn't seem to be a way to access the PipelineOptions at all in the expand method.

What's the alternative?


回答1:


Pablo's answer is right on. I want to also clarify that there is a major change in how PipelineOptions are managed.

You can use them to parse and pass around the arguments to your main program (or whatever code builds your pipeline) but these are technically independent from the PipelineOptions that configure how your pipeline is run.

In Beam, the Pipeline is fully constructed, and only afterwards you choose a PipelineRunner and PipelineOptions to control how the pipeline is run. The pipeline itself does not actually have options.

If you do want the behavior of your PTransform (not its expansion) to use some option that is obtained dynamically, you should make your PTransform accept a ValueProvider like this example in WriteFiles and you can define a pipeline option that returns a ValueProvider like here in ValueProviderTest



来源:https://stackoverflow.com/questions/45019815/how-to-get-pipelineoptions-in-composite-ptransform-in-beam-2-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!