如何在java类(lambda函数)中使用AWS批处理来提交批处理作业

时间:2017-06-22 03:09:16

标签: aws-java-sdk aws-batch

我使用envContainerOverrides读取stepfunctions的输出然后我用这个nodejscode调用我的批处理作业:这里我正在读取传递给batchjob的环境变量。

步进功能输出:

{"bucketName":"bucketName","filesList":["filelist-bucket/filelist1.txt","filelist-bucket/filelist2.txt"]}

如果该节点的数组为[filelist-bucket / filelist1.txt"," filelist-bucket / filelist2.txt],则该节点代码无法获取,如果它是单个值,则它可以正常工作。

我想将此nodejs代码转换为java:

'use strict';

const AWS = require('aws-sdk');

console.log('Loading function');

exports.handler = (event, context, callback) => {
    // Take the data from step 1 and modify, send to standard output
    var comment = event.Comment ;

    var envContainerOverrides ={
        "environment" :[
          {
           "name":"s3Bucket",
           "value":event.bucketName
          },
          {
           "name":"s3FileList",
           "value":event.filesListUrl
          }
        ]
    };
    const params = {
        jobDefinition: process.env.JOB_DEFINITION,
        jobName: process.env.JOB_NAME,
        jobQueue:process.env.JOB_QUEUE,
        containerOverrides: envContainerOverrides || null,
        parameters: event.parameters || null,
    };


    // Submit the Batch Job
    new AWS.Batch().submitJob(params, (err, data) => {
        if (err) {
            console.error(err);
            const message = `Error calling SubmitJob for: ${event.jobName}`;
            console.error(message);
            callback(message);
        } else {
            const jobId = data.jobId;
            console.log('jobId:', jobId);
            callback(null, "Job Id : "+jobId);
        }
    });



};

我正在做这样的事情:

public class InitiateBatchJob1 {

    public static BatchJobRequest process(BatchJobRequest batchJobRequest) throws Exception {


        String s3Bucket = batchJobRequest.getBucketName();
        List<String> s3FileList = batchJobRequest.getFilesListUrl();

        Job job = new Job();
        job.setJobDefinition("testbatchjobenv:2");
        job.setJobQueue("nbatchjobqueue");
        job.setJobName("Filedownload");


        /*// Submit the Batch Job
        new AWS.Batch().submitJob(params, (err, data) => {
            if (err) {
                console.error(err);
                const message = `Error calling SubmitJob for: ${event.jobName}`;
                console.error(message);
                callback(message);
            } else {
                const jobId = data.jobId;
                console.log('jobId:', jobId);
                callback(null, "Job Id : "+jobId);
            }
        });
*/



        return null;
    }


}

BatchJobRequest.java

import java.util.List;

public class BatchJobRequest {

    private String bucketName;

    private List<String> filesListUrl;

    public String getBucketName() {
        return bucketName;
    }

    public void setBucketName(String bucketName) {
        this.bucketName = bucketName;
    }

    public List<String> getFilesListUrl() {
        return filesListUrl;
    }

    public void setFilesListUrl(List<String> filesListUrl) {
        this.filesListUrl = filesListUrl;
    }

}

Job.java:

public class Job {

    private String jobDefinition;
    private String jobName;
    private String jobQueue;
    private String containerOverrides;
    private String parameters;
    public String getJobDefinition() {
        return jobDefinition;
    }
    public void setJobDefinition(String jobDefinition) {
        this.jobDefinition = jobDefinition;
    }
    public String getJobName() {
        return jobName;
    }
    public void setJobName(String jobName) {
        this.jobName = jobName;
    }
    public String getJobQueue() {
        return jobQueue;
    }
    public void setJobQueue(String jobQueue) {
        this.jobQueue = jobQueue;
    }
    public String getContainerOverrides() {
        return containerOverrides;
    }
    public void setContainerOverrides(String containerOverrides) {
        this.containerOverrides = containerOverrides;
    }
    public String getParameters() {
        return parameters;
    }
    public void setParameters(String parameters) {
        this.parameters = parameters;
    }


}

我已经为AWSBatch找到了这个api http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/batch/AWSBatch.html#cancelJob-com.amazonaws.services.batch.model.CancelJobRequest-

但不确定它是否在我的班级中使用正确的api。我在网上搜索了在java中使用aws批处理的链接。

基本上我需要知道如何使用java类设置作业denfition,jobName,作业队列和提交batchjob。任何人都可以帮助我。

1 个答案:

答案 0 :(得分:0)

你必须做这样的事情: http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/batch/AWSBatch.html#submitJob-com.amazonaws.services.batch.model.SubmitJobRequest-

AWSBatch client = AWSBatchClientBuilder.standard().build();
    SubmitJobRequest request = new SubmitJobRequest().withJobName("some-name")
            .withJobQueue("job-queue-name")
            .withJobDefinition("job-definition-name-with-revision-number:1");
    SubmitJobResult response = client.submitJob(request);

我将此代码与lambda函数和我在aws控制台中创建的AWSBatchFullAccess的IAM角色一起使用。因此,在aws上构建并加载jar之后,使用从lambda获取的数据初始化了我的“客户端”。在您的应用程序中,您似乎还需要添加数据来初始化客户端。你必须使用方法

AWSBatch build(AwsSyncClientParams params)

查看AWSBatchClientBuilder类。

您仍需要创建作业队列和作业定义。 另外我建议你开始使用aws控制台。以下是了解aws批处理工作流程http://technology.finra.org/code/enjoying-auto-scaling-integrated-authentication-low-host-cost.html http://www.awsomeblog.com/analysing-exif-data-with-aws-batch/的好教程。在本教程之后,您可以添加用于在myHandler方法中提交作业的代码,并查看作业是如何开始的。