このトピックでは、MapReduceでスリープを使用する例について説明します。
前提条件
テスト用の環境設定を完了します。「はじめに」をご参照ください。
準備
テストプログラムの JAR パッケージを準備します。 このトピックでは、JARパッケージの名前はmapreduce-examples.jarで、MaxComputeのローカルインストールパスのbin\data\resourcesディレクトリに格納されます。
SleepJobのテストリソースを準備します。
-- When adding the JAR package for the first time, you can ignore the -f flag. add jar data\resources\mapreduce-examples.jar -f;
手順
MaxComputeクライアントでSleepを実行します。
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;
期待される結果
ジョブは正常に実行されます。 異なる睡眠持続時間のランタイムは、効果を決定するために比較され得る。
サンプルコード
プロジェクトオブジェクトモデル (POM) の依存関係については、「注意事項」をご参照ください。
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);
}
}