This topic describes an example of using Sleep in MapReduce.


  1. Prepare the JAR package of the test program. In this topic, the JAR package is named mapreduce-examples.jar and stored in the local path data\resources.
  2. Prepare test resources for SleepJob.
    add jar data\resources\mapreduce-examples.jar -f;


Run Sleep on the MaxCompute client.
jar -resources mapreduce-examples.jar -classpath data\resources\mapreduce-examples.jar 10;
jar -resources mapreduce-examples.jar -classpath data\resources\mapreduce-examples.jar 100;

Expected result

The job runs normally. The runtime of different sleep durations can be compared to determine the effect.

Sample code

import com.aliyun.odps.mapred.JobClient;
import com.aliyun.odps.mapred.MapperBase;
import com.aliyun.odps.mapred.conf.JobConf;
public class Sleep {
  private static final String SLEEP_SECS = "sleep.secs";
  public static class MapperClass extends MapperBase {
    /** No data is entered, the map function is not executed, and the related logic can be written only into setup. */
    public void setup(TaskContext context) throws IOException {
      try {
        /** Obtain the number of sleep seconds set in JobConf. */
        Thread.sleep(context.getJobConf().getInt(SLEEP_SECS, 1) * 1000);
      } catch (InterruptedException e) {
        throw new RuntimeException(e);
  public static void main(String[] args) throws Exception {
    if (args.length != 1) {
      System.err.println("Usage: Sleep <sleep_secs>");
    JobConf job = new JobConf();
    /** This instance is also a MapOnly job and the number of reducers must be set to 0. */
    /** The number of mappers must be specified by the user because no input table is provided. */
    job.set(SLEEP_SECS, args[0]);