All Products
Search
Document Center

How to submit a job

Last Updated: May 10, 2018

This section explains your questions such as how can you run your Python program python test.py on the cloud.

python test.py:

  1. print('Hello, cloud!')

You firstly submit a job to Batch Compute which applies for a machine based on your configuration. Then start the VM and run python test.py on the VM. The running result is automatically uploaded to your OSS bucket. You can view the running result in your OSS bucket.

1. You can submit a job in multiple methods. The following describes four methods

1.1. Use a command line tool (a command) to submit a job

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

The job is submitted.

  • When this command is run, the file test.py is packed into worker.tar.gz, uploaded to the specified place, and then submitted to a job for running.

  • To run the bcs command, install the Batch Compute-cli tool first. For more information, see Here.

  • bcs sub command:
  1. bcs sub <commandLine> [job_name] [options]

To view more parameter details, run bcs sub -h.

1.2. Use console to submit a job

The detailed steps are as follows:

1.2.1. Pack and upload test.py to OSS

Run the following command in the directory of test.py:

  1. tar -czf worker.tar.gz test.py # Packs test.py into worker.tar.gz.

Use the OSS console to upload worker.tar.gz to your OSS bucket.

If you must have signed up OSS service.

You must have created a bucket. Assume that the bucket name is mybucket.

Create a directory named test in this bucket.

Assume that you upload the file to the directory test in mybucket. Then the file path in your OSS instance is oss://mybucket/test/worker.tar.gz.

1.2.2. Use the console to submit a job

Go to the Submit job page.

  • Enter the job name first_job as prompted.

Step 1

  • Drag a job and enter the fields as follows. The ECS image ID can be obtained from Image.

step 2

  • Click Submit Job to submit the job.

  • After the job is successfully submitted, the page automatically jumps to the Job List page where you can view the status of the job you submitted.

  • Wait a moment. You can view the result after the job running is finished.

1.3. Use Python SDK to submit a job

1.3.1. Pack and upload test.py to OSS.

Same to the previous section.

1.3.2. Submit a job.

  1. from batchcompute import Client, ClientError
  2. from batchcompute import CN_SHENZHEN as REGION
  3. ACCESS_KEY_ID = 'your_access_key_id' #This parameter needs to be configured.
  4. ACCESS_KEY_SECRET = 'your_access_key_secret' #This parameter needs to be configured.
  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": "ecs.sn1.medium",
  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. ....

For more information about the Python SDK, see Python SDK.

1.4. Use Java SDK to submit a job

1.4.1. Pack and upload test.py to OSS.

Same to the previous section.

1.4.2. Submit a job.

  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 = ""; //This parameter needs to be configured.
  8. String ACCESS_KEY_SECRET = ""; //This parameter needs to be configured.
  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("ecs.sn1.medium");
  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. }

For more information about the Java SDK, see Java SDK.

2. CommandLine in Batch Compute:

  • CommandLine is different from Shell. It supports only the form of program + parameter, for example, python test.py or sh test.sh.

  • To run a Shell, run /bin/bash -c 'cd /home/xx/ && python a.py'.

  • To write a Shell to an SH script such as test.sh, run sh test.sh.

CommandLine location:

  • cmd of bcs sub <cmd> [job_name] [options] in the command line tool.

  • When the Java SDK is used, cmd in cmd.setCommandLine(cmd).

  • taskName.Parameters.Command.CommandLine in the Python SDK.