閱讀93 返回首頁    go 奇藝


如何提交作業__操作指南_批量計算-阿裏雲

如何提交一個作業

A君: 我有一個python程序,我在本地可以這麼運行:python test.py, 但是想要在雲上運行怎麼做?

test.py 內容如下:

  1. print('Hello, cloud!')

雲上運行大致過程: 您提交一個作業到BatchCompute,BatchCompute會按照你提供的配置去申請機器,啟動虛擬機,在虛擬機中運行: python test.py, 得到結果後自動上傳到OSS中。然後您可以去OSS中查看運行的結果。

提交作業的方法有很多,下麵列舉4種:

1. 使用命令行(一條命令提交作業):

  1. bcs sub "python test.py" -p ./test.py

搞定!

  • 這條命令會將test.py文件打包成 worker.tar.gz 上傳到指定位置,然後再提交作業運行。

bcs命令需要先安裝 batchcompute-cli工具才能使用, 請看這裏

2. 使用控製台提交作業:

下麵列舉詳細解釋步驟:

(1). 將test.py打包上傳到雲端OSS

在test.py所在目錄運行下麵的命令:

  1. tar -czf worker.tar.gz test.py # 將 test.py 打包到 worker.tar.gz

然後使用OSS控製台將 worker.tar.gz 上傳到OSS。

如果還沒有開通OSS,請先開通.

還需要創建Bucket,假設創建了Bucket名稱為 mybucket

然後在這個Bucket下創建一個目錄: test

假設您上傳到了mybucket這個Bucket下的test目錄下,則OSS路徑表示為: oss://mybucket/test/worker.tar.gz

(2). 使用控製台提交作業

打開 提交作業頁麵。

  • 按照表單提示,填寫作業名稱: first_job

第1步

  • 拖拽一個任務,按照下圖填寫表單, 其中ECS鏡像ID可以從這裏獲取: 鏡像

第2步

  • 然後點擊下麵的”提交作業”按鈕, 即可提交成功。

  • 提交成功後,自動跳轉到作業列表頁麵,您可以在這裏看到你提交的作業狀態。

  • 等待片刻後作業運行完成,即可查看結果。

3. 使用 Python SDK 提交作業

(1) 將test.py打包上傳到雲端OSS

同上一節。

(2) 提交作業

  1. from batchcompute import Client, ClientError
  2. from batchcompute import CN_SHENZHEN as REGION
  3. ACCESS_KEY_ID = 'your_access_key_id' #需要配置
  4. ACCESS_KEY_SECRET = 'your_access_key_secret' #需要配置
  5. job_desc = {
  6. "Name": "my_job_name",
  7. "Description": "hello test",
  8. "JobFailOnInstanceFail": true,
  9. "Priority": 0,
  10. "Type": "DAG",
  11. "DAG": {
  12. "Tasks": {
  13. "test": {
  14. "InstanceCount": 1,
  15. "MaxRetryCount": 0,
  16. "Parameters": {
  17. "Command": {
  18. "CommandLine": "python test.py",
  19. "PackagePath": "oss://mybucket/test/worker.tar.gz"
  20. },
  21. "StderrRedirectPath": "oss://mybucket/test/logs/",
  22. "StdoutRedirectPath": "oss://mybucket/test/logs/"
  23. },
  24. "Timeout": 21600,
  25. "AutoCluster": {
  26. "InstanceType": "bcs.a2.large",
  27. "ImageId": "img-ubuntu"
  28. }
  29. }
  30. },
  31. "Dependencies": {}
  32. }
  33. }
  34. client = Client(REGION, ACCESS_KEY_ID, ACCESS_KEY_SECRET)
  35. result = client.create_job(job_desc)
  36. job_id = result.Id
  37. ....

(3) 更多關於Python SDK內容

請看 Python SDK

4. 使用 Java SDK 提交作業

(1) 將test.py打包上傳到雲端OSS

同上一節。

(2) 提交作業

  1. import com.aliyuncs.batchcompute.main.v20151111.*;
  2. import com.aliyuncs.batchcompute.model.v20151111.*;
  3. import com.aliyuncs.batchcompute.pojo.v20151111.*;
  4. import com.aliyuncs.exceptions.ClientException;
  5. public class SubmitJob{
  6. String REGION = "cn-shenzhen";
  7. String ACCESS_KEY_ID = ""; //需要配置
  8. String ACCESS_KEY_SECRET = ""; //需要配置
  9. public static void main(String[] args) throws ClientException{
  10. JobDescription desc = new SubmitJob().getJobDesc();
  11. BatchCompute client = new BatchComputeClient(REGION, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
  12. CreateJobResponse res = client.createJob(desc);
  13. String jobId = res.getJobId();
  14. //...
  15. }
  16. private JobDescription getJobDesc() {
  17. JobDescription desc = new JobDescription();
  18. desc.setName("testJob");
  19. desc.setPriority(1);
  20. desc.setDescription("JAVA SDK TEST");
  21. desc.setType("DAG");
  22. desc.setJobFailOnInstanceFail(true);
  23. DAG dag = new DAG();
  24. dag.addTask(getTaskDesc());
  25. desc.setDag(dag);
  26. return desc;
  27. }
  28. private TaskDescription getTaskDesc() {
  29. TaskDescription task = new TaskDescription();
  30. task.setClusterId(gClusterId);
  31. task.setInstanceCount(1);
  32. task.setMaxRetryCount(0);
  33. task.setTaskName("test");
  34. task.setTimeout(10000);
  35. AutoCluster autoCluster = new AutoCluster();
  36. autoCluster.setImageId("img-ubuntu");
  37. autoCluster.setInstanceType("bcs.a2.large");
  38. // autoCluster.setResourceType("OnDemand");
  39. task.setAutoCluster(autoCluster);
  40. Parameters parameters = new Parameters();
  41. Command cmd = new Command();
  42. cmd.setCommandLine("python test.py");
  43. // cmd.addEnvVars("a", "b");
  44. cmd.setPackagePath("oss://mybucket/test/worker.tar.gz");
  45. parameters.setCommand(cmd);
  46. parameters.setStderrRedirectPath("oss://mybucket/test/logs/");
  47. parameters.setStdoutRedirectPath("oss://mybucket/test/logs/");
  48. // InputMappingConfig input = new InputMappingConfig();
  49. // input.setLocale("GBK");
  50. // input.setLock(true);
  51. // parameters.setInputMappingConfig(input);
  52. task.setParameters(parameters);
  53. // task.addInputMapping("oss://my-bucket/disk1/", "/home/admin/disk1/");
  54. // task.addOutputtMapping("/home/admin/disk2/", "oss://my-bucket/disk2/");
  55. // task.addLogMapping( "/home/admin/a.log","oss://my-bucket/a.log");
  56. return task;
  57. }
  58. }

(3) 更多關於Java SDK內容

請看 Java SDK

最後更新:2016-12-05 17:40:27

  上一篇:go Python快速開始__快速入門_批量計算-阿裏雲
  下一篇:go OSS掛載__操作指南_批量計算-阿裏雲