This topic describes an example of using Sleep in MapReduce.
Preparations
- 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.
- Prepare test resources for SleepJob.
add jar data\resources\mapreduce-examples.jar -f;
Procedure
Run Sleep on the MaxCompute client.
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;
Expected result
The job runs normally. The runtime of different sleep durations can be compared to determine the effect.
Sample code
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 {
/** No data is entered, the map function is not executed, and the related logic can be written only into setup. */
@Override
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>");
System.exit(-1);
}
JobConf job = new JobConf();
job.setMapperClass(MapperClass.class);
/** This instance is also a MapOnly job and the number of reducers must be set to 0. */
job.setNumReduceTasks(0);
/** The number of mappers must be specified by the user because no input table is provided. */
job.setNumMapTasks(1);
job.set(SLEEP_SECS, args[0]);
JobClient.runJob(job);
}
}