Sleep example

更新时间:
复制 MD 格式

This topic walks you through running a Sleep MapReduce job on MaxCompute. The Sleep example has no input data — it sleeps for a specified number of seconds in each mapper's setup() method. Use it to verify that your environment is configured correctly and to understand how MapOnly jobs (jobs with no reducers) work.

Prerequisites

Before you begin, complete the environment configuration for testing. See Getting started.

Prepare resources

  1. Prepare the JAR package of the test program. In this topic, the JAR package is named mapreduce-examples.jar and is stored in the bin\data\resources directory under your local MaxCompute client installation path.

  2. Register the JAR as a MaxCompute resource. Run the following command in the MaxCompute client:

    -- Omit the -f flag the first time you add the JAR.
    add jar data\resources\mapreduce-examples.jar -f;

Run the Sleep job

Run the following commands in the MaxCompute client. The two commands test different sleep durations so you can compare their runtimes:

jar -resources mapreduce-examples.jar -classpath data\resources\mapreduce-examples.jar
com.aliyun.odps.mapred.open.example.Sleep 10;

jar -resources mapreduce-examples.jar -classpath data\resources\mapreduce-examples.jar
com.aliyun.odps.mapred.open.example.Sleep 100;

The argument after the class name is the number of seconds to sleep. The first command sleeps for 10 seconds; the second sleeps for 100 seconds.

Expected result

Both jobs run normally. The runtime of different sleep durations can be compared to determine the effect.

Sample code

For Project Object Model (POM) dependency configuration, see the Precautions section in the Getting started guide.

package com.aliyun.odps.mapred.open.example;
import java.io.IOException;
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 {
    @Override
    public void setup(TaskContext context) throws IOException {
      try {
        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>");
      System.exit(-1);
    }
    JobConf job = new JobConf();
    job.setMapperClass(MapperClass.class);
    job.setNumReduceTasks(0);
    job.setNumMapTasks(1);
    job.set(SLEEP_SECS, args[0]);
    JobClient.runJob(job);
  }
}

How the code works

Why `setup()` instead of `map()`

The Sleep job has no input table, so MapReduce never calls the map() method — there is no data to iterate over. The sleep logic goes into setup(), which runs once per mapper task regardless of input data.

Why it's a MapOnly job

job.setNumReduceTasks(0) disables the reduce phase entirely. The job only needs to sleep, not aggregate results, so reducers are unnecessary.

Why the mapper count is set manually

Normally, MaxCompute determines the number of mappers from the input table size. Because this job has no input table, job.setNumMapTasks(1) explicitly sets one mapper — otherwise the job would have nothing to run.

How the sleep duration is passed

The command-line argument (for example, 10) is stored in JobConf under the key sleep.secs. The mapper reads it with context.getJobConf().getInt(SLEEP_SECS, 1) and multiplies by 1,000 to convert seconds to milliseconds for Thread.sleep().