批量计算目前支持命令行,控制台和SDK提交作业,本文介绍将作业提交到批量计算的相关操作。
提交作业的方法
使用命令行
bcs sub "python test.py" -p ./test.py
该命令会将 test.py 文件打包成 worker.tar.gz 并上传到指定位置,然后再提交作业运行。 bcs命令需要先安装 batchcompute-cli 工具 才能使用。
bcs sub 命令格式为 bcs sub <commandLine> [job_name] [options]
,更多参数详情参考bcs sub -h
命令。
使用控制台提交作业
将 test.py 打包上传到 OSS
在 test.py 所在目录运行下面的命令:
tar -czf worker.tar.gz test.py # 将 test.py 打包到 worker.tar.gz
使用 OSS 控制台,上传 worker.tar.gz。
注意:如果还没有开通 OSS,请先开通;如果还需要创建 Bucket,假设创建了 Bucket 名称为 mybucket;然后在这个 Bucket 下创建一个目录:test。
假设您上传到了 mybucket 桶下的 test 目录下,则 OSS 路径表示为: oss://mybucket/test/worker.tar.gz。
使用控制台提交作业
打开 提交作业 页面。
按照表单提示,填写作业名称为 first_job
拖拽一个任务,按照下图填写表单, 指定 ECS 镜像 ID。
点击下面的“提交作业”按钮, 即可提交成功;提交成功后,自动跳转到作业列表页面,您可以在这里看到你提交的作业状态;等待片刻后作业运行完成,即可查看结果。
使用 Python SDK 提交作业
将 test.py 打包上传到云端 OSS
同上一节。
提交作业
from batchcompute import Client, ClientError
from batchcompute import CN_SHENZHEN as REGION
ACCESS_KEY_ID = 'your_access_key_id' #需要配置
ACCESS_KEY_SECRET = 'your_access_key_secret' #需要配置
job_desc = {
"Name": "my_job_name",
"Description": "hello test",
"JobFailOnInstanceFail": true,
"Priority": 0,
"Type": "DAG",
"DAG": {
"Tasks": {
"test": {
"InstanceCount": 1,
"MaxRetryCount": 0,
"Parameters": {
"Command": {
"CommandLine": "python test.py",
"PackagePath": "oss://mybucket/test/worker.tar.gz"
},
"StderrRedirectPath": "oss://mybucket/test/logs/",
"StdoutRedirectPath": "oss://mybucket/test/logs/"
},
"Timeout": 21600,
"AutoCluster": {
"InstanceType": "ecs.sn1.medium",
"ImageId": "img-ubuntu"
}
}
},
"Dependencies": {}
}
}
client = Client(REGION, ACCESS_KEY_ID, ACCESS_KEY_SECRET)
result = client.create_job(job_desc)
job_id = result.Id
....
更多关于Python SDK内容
请参考 Python SDK 。
使用 Java SDK 提交作业
将 test.py 打包上传到云端 OSS
同上一节。
提交作业
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 SubmitJob{
String REGION = "cn-shenzhen";
String ACCESS_KEY_ID = ""; //需要配置
String ACCESS_KEY_SECRET = ""; //需要配置
public static void main(String[] args) throws ClientException{
JobDescription desc = new SubmitJob().getJobDesc();
BatchCompute client = new BatchComputeClient(REGION, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
CreateJobResponse res = client.createJob(desc);
String jobId = res.getJobId();
//...
}
private JobDescription getJobDesc() {
JobDescription desc = new JobDescription();
desc.setName("testJob");
desc.setPriority(1);
desc.setDescription("JAVA SDK TEST");
desc.setType("DAG");
desc.setJobFailOnInstanceFail(true);
DAG dag = new DAG();
dag.addTask(getTaskDesc());
desc.setDag(dag);
return desc;
}
private TaskDescription getTaskDesc() {
TaskDescription task = new TaskDescription();
task.setClusterId(gClusterId);
task.setInstanceCount(1);
task.setMaxRetryCount(0);
task.setTaskName("test");
task.setTimeout(10000);
AutoCluster autoCluster = new AutoCluster();
autoCluster.setImageId("img-ubuntu");
autoCluster.setInstanceType("ecs.sn1.medium");
// autoCluster.setResourceType("OnDemand");
task.setAutoCluster(autoCluster);
Parameters parameters = new Parameters();
Command cmd = new Command();
cmd.setCommandLine("python test.py");
// cmd.addEnvVars("a", "b");
cmd.setPackagePath("oss://mybucket/test/worker.tar.gz");
parameters.setCommand(cmd);
parameters.setStderrRedirectPath("oss://mybucket/test/logs/");
parameters.setStdoutRedirectPath("oss://mybucket/test/logs/");
// InputMappingConfig input = new InputMappingConfig();
// input.setLocale("GBK");
// input.setLock(true);
// parameters.setInputMappingConfig(input);
task.setParameters(parameters);
// task.addInputMapping("oss://my-bucket/disk1/", "/home/admin/disk1/");
// task.addOutputtMapping("/home/admin/disk2/", "oss://my-bucket/disk2/");
// task.addLogMapping( "/home/admin/a.log","oss://my-bucket/a.log");
return task;
}
}
更多关于 Java SDK 内容
请参考 Java SDK。
批量计算中的 CommandLine
CommandLine 不等同于 SHELL,仅支持”程序+参数”方式,比如”python test.py”或”sh test.sh”。 如果你想要执行 SHELL,可以使用”/bin/bash -c ‘cd /home/xx/ && python a.py’” 或者将 Shell 写到一个 sh 脚本中如:test.sh, 然后用”sh test.sh”执行。
CommandLine 具体位置:
命令行工具中
bcs sub <cmd> [job_name] [options]
的 cmd。使用 Java sdk 时
cmd.setCommandLine(cmd)
中的 cmd。Python sdk 中的
taskName.Parameters.Command.CommandLine
。