接口
createJob
參數
參數 | 類型 | 是否必需 | 說明 |
jobDescription | JobDescription | 是 | Job對象中有各個任務的描述信息,和任務的DAG依賴。 |
jobDescription 的具體屬性信息參考DAG作業和APP作業。
返回值
創建成功后返回一個 CreateJobResponse
實例,可以通過 response.getJobId()
獲取創建的作業ID。創建失敗后,拋出異常: ClientException
。
例子
Java 源碼
import com.aliyuncs.batchcompute.main.v20151111.*;
import com.aliyuncs.batchcompute.model.v20151111.*;
import com.aliyuncs.batchcompute.pojo.v20151111.*;
import com.aliyuncs.exceptions.ClientException;
public class CreateAppJob {
static String ACCESS_KEY_ID = "xxx"; //這里填寫您的 AccessKeyId
static String ACCESS_KEY_SECRET = "xxx"; //這里填寫您的 AccessKeySecret
static String REGION_ID = "cn-xxx"; //這里填寫 region
static String ClusterId = "cls-xxx"; //提交DAG固定集群作業需要修改其他場景不需要修改
static boolean IS_DAG_JOB = true; //APP作業和DAG作業開關,默認提交DAG作業
static boolean IS_AUTO_CLUSTER = true; //固定集群和非固定集群作業開關,默認提交固定集群作業
public static void main(String[] args) {
BatchCompute client = new BatchComputeClient(REGION_ID, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
try {
JobDescription jobDescription = getJobDesc();
CreateJobResponse response = client.createJob(jobDescription);
String jobId = response.getJobId();
//創建成功
System.out.println("jobId:" + jobId);
System.out.println("RequestId: " + response.getRequestId());
System.out.println("StatusCode: " + response.getStatusCode());
} catch (ClientException e) {
e.printStackTrace();
//創建失敗
}
}
private static JobDescription getJobDesc() {
JobDescription desc = new JobDescription();
desc.setName("javaSdkJob");
desc.setDescription("javaSdkJob");
//設置作業優先級
desc.setPriority(1);
desc.setJobFailOnInstanceFail(true);
desc.setAutoRelease(false);
if (IS_DAG_JOB) {
//設置 DAG task
desc.setType("DAG");
desc.setDag(getDagDesc());
}else{
//設置 APP task
desc.setType("App");
desc.setApp(getAppJobDescription());
}
//根據業務需要設置訂閱作業實踐
// Notification noti = new Notification();
// Topic topic = new Topic();
// topic.addEvent(Topic.ON_JOB_FAILED);
// topic.addEvent(Topic.ON_JOB_FINISHED);
// noti.setTopic(topic);
// topic.setName("tp_n1");
// topic.setEndpoint("xxxxx");
// desc.setNotification(noti);
return desc;
}
private static AppJobDescription getAppJobDescription() {
AppJobDescription appJobDescription = new AppJobDescription();
appJobDescription.setAppName("JavaSdkApp");
appJobDescription.addInputs("inputFile", "oss://test/input/cromwell_app.txt");
appJobDescription.addOutputs("outputFile", "oss://test/output/ret/");
AppJobDescription.Logging logging = new AppJobDescription.Logging();
logging.setStderrPath("oss://test/output/error/");
logging.setStdoutPath("oss://test/output/log/");
appJobDescription.setLogging(logging);
appJobDescription.addConfig("ResourceType", "OnDemand");
appJobDescription.addConfig("InstanceType", "ecs.sn2ne.large");
appJobDescription.addConfig("InstanceCount", 1);
appJobDescription.addConfig("MinDiskSize", 40);
appJobDescription.addConfig("DiskType", "cloud_efficiency");
appJobDescription.addConfig("MaxRetryCount", 1);
appJobDescription.addConfig("Timeout", 1000);
appJobDescription.addConfig("ReserveOnFail", true);
appJobDescription.addConfig("ClassicNetwork", false);//設置集群網絡方式,false為VPC組網
appJobDescription.addConfig("MinDataDiskSize", 40);
//注意磁盤類型和 DiskType 保持一致
appJobDescription.addConfig("DataDiskType", "cloud_efficiency");
//掛載點根據需要做修改,windows 為 “E:, F:, G: 等”
appJobDescription.addConfig("DataDiskMountPoint", "/home/mount/");
return appJobDescription;
}
private static DAG getDagDesc() {
DAG dag = new DAG();
TaskDescription task = new TaskDescription();
task.setTaskName("javaSdkTask");
//設置實例信息
task.setInstanceCount(1);
if (IS_AUTO_CLUSTER){
//設置Auto cluster
task.setAutoCluster(getAutoCluster());
}else{
//設置固定集群信息
task.setClusterId(ClusterId);
}
task.setMaxRetryCount(2);
task.setTimeout(10000);
Parameters parameters = new Parameters();
Command cmd = new Command();
//設置程序啟動命令
cmd.setCommandLine("python runtask.py 頓靂意當更囅");
//設置程序啟動腳本或者執行文件地址
cmd.setPackagePath("oss://yuanhyyshenzhen/test/installpackage/runtask.tar.gz");
//docker 鏡像設置方式:推薦使用容器鏡像模式
//1、鏡像在oss registry上,設置docker的方式
//oss registry模式,參數設置好后自行打開注釋
//cmd.addEnvVars("BATCH_COMPUTE_DOCKER_IMAGE", "localhost:5000/yuorBucket/dockers:0.1");//鏡像名稱;
//cmd.addEnvVars("BATCH_COMPUTE_DOCKER_REGISTRY_OSS_PATH", "oss://your-bucket/dockers");//設置OSS地址
//2、鏡像在容器鏡像倉庫,設置docker方式
//Command.Docker docker = new Command.Docker();
//docker.setImage("registry.cn-beijing.aliyuncs.com/demotest/test:0.1");
//cmd.setDocker(docker);
parameters.setCommand(cmd);
//設置標準輸出,上傳的OSS路徑
parameters.setStderrRedirectPath("oss://test/output/error/");
parameters.setStdoutRedirectPath("oss://test/output/log/");
InputMappingConfig input = new InputMappingConfig();
input.setLocale("GBK");
input.setLock(true);
parameters.setInputMappingConfig(input);
task.setParameters(parameters);
//設置輸入OSS路徑和本地路徑關系
task.addInputMapping("oss://test/input/", "/home/admin/disk1/");
//設置輸出本地路徑和OSS地址
task.addOutputMapping("/home/admin/disk2/", "oss://test/output/ret/");
//設置掛載信息
Mounts mounts = new Mounts();
MountEntry mountEntry = new MountEntry();
mountEntry.setDestination("/home/mount");
mountEntry.setSource("oss://test/mount/");
mountEntry.setWriteSupport(false);
mounts.setCacheSupport(false);
//windows set GBK; Liux set utf-8
//mounts.setLocale("GBK");
mounts.setLock(false);
mounts.addEntries(mountEntry);
//task.setMounts(mounts);
dag.addTask(task);
return dag;
}
private static AutoCluster getAutoCluster() {
AutoCluster autoCluster = new AutoCluster();
//設置集群鏡像信息ECSImageId 在不同region可能會發生變化
//autoCluster.setECSImageId("m-wz9dk5nao5z3fw6bo9k6");
//建議使用setImageId接口設置
autoCluster.setImageId("img-ubuntu");
autoCluster.setInstanceType("ecs.s3.large");
autoCluster.setReserveOnFail(true);
//設置資源類型只有ResourceType為Spot的情況下后面兩項有效
autoCluster.setResourceType("OnDemand");
//autoCluster.setSpotPriceLimit(5.6f);
//autoCluster.setSpotStrategy("Spot");
//設置config信息
autoCluster.setConfigs(getConfigDesc());
return autoCluster;
}
private static Configs getConfigDesc() {
Configs configs = new Configs();
//設置系統磁盤類型以及大型
Disks disks = new Disks();
SystemDisk systemDisk = new SystemDisk();
systemDisk.setSize(40);//GB
systemDisk.setType("cloud_efficiency");
disks.setSystemDisk(systemDisk);
DataDisk dataDisk = new DataDisk();
dataDisk.setMountPoint("/home/dataDisk/");
dataDisk.setSize(40);
dataDisk.setType("cloud_efficiency");
disks.setDataDisk(dataDisk);
configs.setDisks(disks);
//設置網絡類型
Networks networks = new Networks();
VPC vpc = new VPC();
vpc.setCidrBlock("10.0.0.0/12");
networks.setVpc(vpc);
configs.setNetworks(networks);
return configs;
}
}
執行結果:
```JSON
{
jobId: job-000000005BE3E897000007FA00114EE9
RequestId: null
StatusCode: 201
}
注意
本實例代碼支持提交 APP 和 DAG 類型作業,支持 AutoCluster 和固定集群類型的作業,提交作業之前根據業務需要修改開關(
IS_DAG_JOB
和IS_AUTO_CLUSTER
)即可。若是提交 APP 類型作業,需要在提交作業之前創建 APP,然后根據 APP 的創建參數做對應修改作業參數,最后進行作業提交。
提交固定集群作業之前需要先創建集群,修改
ClusterId
為新創建的集群,然后提交作業。提交作業前,請確保 OSS 地址填寫正確并且已經上傳輸入或者執行文件到對應的 OSS 路徑
文檔內容是否對您有幫助?