无法使用Dataflow REST API传递作业参数

时间:2019-02-05 21:02:45

标签: google-cloud-dataflow apache-beam dataflow

我试图通过REST API调用数据流模板,但是,在正文中指定参数时,将引发INVALID_ARGUMENT错误。删除参数字段后,它仍然可以正常工作。我已经尝试了Javascript和Python,但得到了完全相同的错误。

from googleapiclient.discovery import build
from oauth2client.client import GoogleCredentials

def invoke_dataflow_job():
    credentials = GoogleCredentials.get_application_default()
    service = build('dataflow', 'v1b3', credentials=credentials)

    TEMPLATE_LOCATION = 'gs://xxx';
    PROJECT_ID = 'xxx';
    JOB_NAME = 'xxx';
    LOCATION = 'europe-west1';
    ZONE = 'europe-west1-b';
    TEMP_LOCATION = 'xxx';

    BODY = {
        'jobName': JOB_NAME,
        'gcsPath': TEMPLATE_LOCATION,
        'parameters': {
           'messageToPrint': 'Blue sky'
        },
        'environment': {
           'tempLocation': TEMP_LOCATION,
           'zone': ZONE,
        }
    }

    request = service.projects().locations().templates().create(projectId=PROJECT_ID, location=LOCATION, body=BODY)
    response = request.execute()
    return response

REST API是否支持传递参数?看着example代码,我相信应该如此。

有什么建议吗?

错误消息:

The workflow could not be created. Causes: (4adc3dbfd180d8a2): Found unexpected parameters: ['messageToPrint' (perhaps you meant 'userAgent')]

更新1:

DoFn示例:

public static class BuildPathFn extends DoFn<String, Void> {
    private static final long serialVersionUID = -2815123956194177539L;
    private final ValueProvider<String> baseDir; 
    private final ValueProvider<String> year; 
    private final ValueProvider<String> month; 
    private final ValueProvider<String> day; 
    private final ValueProvider<String> hour; 
    private final ValueProvider<String> filePattern; 

    public BuildPathFn (ValueProvider<String> baseDir, ValueProvider<String> year, 
                            ValueProvider<String> month, ValueProvider<String> day, 
                            ValueProvider<String> hour, ValueProvider<String> filePattern) {
        this.baseDir = baseDir;
        this.year = year;
        this.month = month;
        this.day = day;
        this.hour = hour;
        this.filePattern = filePattern;
    }

    @ProcessElement
    public void processElement(@Element String element, OutputReceiver<Void> receiver) {
        try {
            String path = baseDir.get() + "/" + year.get() + "/" + month.get() + "/" + day.get() + "/" + hour.get() + "/" + filePattern.get();
            LOG.info("BuildPathFn >> Path: {}", path);
        } catch (Exception e) {
            LOG.error("BuildPathFn >> Exception: {}", e.getMessage()); 

        }
    }
}

运行时输出:

BuildPathFn >> Path: true/true/true/true/true/true

用于构建模板的mvn命令的一部分:

 ".... --baseDir --year --month --day --hour --filePattern"

1 个答案:

答案 0 :(得分:0)

代码很好,这是传递参数的正确方法。请记住,该选项需要在模板中定义为ValueProvider。当staging时,模板不会传递您要在运行时解析的任何参数。然后,当invoking模板中包含描述中已经存在的代码(即在请求正文中使用parameters)时,应该评估选项值。