All Products
Search
Document Center

Batch Compute:Variables

Last Updated:Apr 12, 2018

1. Variables for task programs

Batch Compute provides the following variables for task programs:

Variable Value
BATCH_COMPUTE_DAG_JOB_ID Job ID, depending on the actual situation
BATCH_COMPUTE_DAG_TASK_ID Task ID, depending on the actual situation
BATCH_COMPUTE_DAG_INSTANCE_ID Instance ID, depending on the actual situation
BATCH_COMPUTE_OSS_HOST OSS host name, depending on the actual situation
BATCH_COMPUTE_REGION Region where the host is located, depending on the actual situation
BATCH_COMPUTE_CLUSTER_ID cluster id
BATCH_COMPUTE_WORKER_ID worker id

See the following table for the difference of variables for task programs running in a Docker container:

Variable Value
USER root
PWD /batchcompute/workdir
PATH /sbin:/usr/sbin:/bin:/usr/bin. Note that the path /usr/local/bin does not exist. If you want to set a path, specify it in the EnvVars field when submitting the job.
HOME /root
BATCH_COMPUTE_DAG_JOB_ID Job ID, depending on the actual situation
BATCH_COMPUTE_DAG_TASK_ID Task ID, depending on the actual situation
BATCH_COMPUTE_DAG_INSTANCE_ID Instance ID, depending on the actual situation
BATCH_COMPUTE_OSS_HOST OSS host name, depending on the actual situation
BATCH_COMPUTE_REGION Region where the host is located, depending on the actual situation

2. How to use variables

You can obtain variables from EnvVars in task programs, for example:

2.1. Use variables in a Python program:

  1. task_id = os.environ['BATCH_COMPUTE_DAG_TASK_ID']
  2. instance_id = os.environ['BATCH_COMPUTE_DAG_INSTANCE_ID']

2.2. Use variables in a Java program:

  1. String taskId = System.getenv("BATCH_COMPUTE_DAG_TASK_ID");
  2. String instanceId = System.getenv("BATCH_COMPUTE_DAG_INSTANCE_ID");

3. Define variables

In addition to variables provided by the system, you can also define new variables when submitting a job.

3.1. Use Python SDK

Code snippet:

  1. env = {
  2. 'k1': 'v1',
  3. 'k2': 'v2'
  4. }
  5. ...
  6. job_desc['DAG']['Tasks']['my-task']['Parameters']['Command']['EnvVars']=env
  7. ...

3.2. Use Java SDK

Code snippet:

  1. Command cmd= new Command();
  2. cmd.addEnvVars("k1","v1");
  3. cmd.addEnvVars("k2","v2");
  4. ...
  5. TaskDescription desc = TaskDescription();
  6. Parameters parmas = new Parameters();
  7. params.setCommand(cmd);
  8. ...
  9. desc.setParameters(params);

3.3. Use command line tool

  1. bcs sub "python main.py" -e k1:v1,k2:v2