MapOnly ジョブの場合、Map は <キー、値> ペアを MaxCompute のテーブルに直接送信します。 必要なのは、出力テーブルの指定だけです。 ただし、Map によって出力される キーおよび値のメタデータの指定はスキップできます。
前提条件
- テストプログラムの JAR パッケージを準備します。 パッケージの名前を "mapreduce-examples.jar" 、ローカルストレージパスを "data\resources" とします。
- MapOnly をテストするためのテーブルとリソースを準備します。
- テーブルを作成します。
create table wc_in (key string, value string); create table wc_out(key string, cnt bigint);
- リソースを追加します。
add jar data\resources\mapreduce-examples.jar -f;
- テーブルを作成します。
- tunnel コマンドを使用してデータをインポートします。
tunnel upload data wc_in;
データファイルの内容は “mr_src” テーブルにインポートされます。hello,odps hello,odps
手順
odpscmd で MapOnly を実行します。
jar -resources mapreduce-examples.jar -classpath data\resources\mapreduce-examples.jar
com.aliyun.odps.mapred.open.example.MapOnly wc_in wc_out map
予想される出力
出力テーブル wc_out
の内容は次のとおりです。
+------------+------------+
| key | cnt |
+------------+------------+
| hello | 1 |
| hello | 1 |
+------------+------------+
サンプルコード
package com.aliyun.odps.mapred.open.example;
import java.io.IOException;
import com.aliyun.odps.data.Record;
import com.aliyun.odps.mapred.JobClient;
Import com. aliyun. ODPS. mapred. mapperbase;
import com.aliyun.odps.mapred.conf.JobConf;
import com.aliyun.odps.mapred.utils.SchemaUtils;
import com.aliyun.odps.mapred.utils.InputUtils;
import com.aliyun.odps.mapred.utils.OutputUtils;
import com.aliyun.odps.data.TableInfo;
public class MapOnly {
public static class MapperClass extends MapperBase {
@Override
public void setup(TaskContext context) throws IOException{
boolean is = context.getJobConf().getBoolean("option.mapper.setup", false);
// Main 関数は、jobconf で option.mapper.setup を true に設定し、次のロジックを実行します。
if (is) {
Record result = context.createOutputRecord();
result.set(0, "setup");
result.set(1, 1L);
context.write(result);
}
}
@Override
public void map(long key, Record record, TaskContext context) throws IOException {
boolean is = context.getJobConf().getBoolean("option.mapper.map", false);
// Main 関数は、jobconf で option.mapper.map を true に設定し、次のロジックを実行します。
if (is) {
Record result = context.createOutputRecord();
result.set(0, record.get(0));
result.set(1, 1L);
context.write(result);
}
}
@Override
public void cleanup(TaskContext context) throws IOException {
boolean is = context.getJobConf().getBoolean("option.mapper.cleanup", false);
// Main 関数は、jobconf で option.mapper.cleanup を true に設定し、次のロジックを実行します。
if (is) {
Record result = context.createOutputRecord();
result.set(0, "cleanup");
result.set(1, 1L);
context.write(result);
}
}
}
public static void main(String[] args) throws Exception {
if (args.length ! = 2 && args.length ! = 3) {
System.err.println("Usage: OnlyMapper <in_table> <out_table> [setup|map|cleanup]");
System.exit(2);
}
JobConf job = new JobConf();
job.setMapperClass(MapperClass.class);
// maponly ジョブの場合、reducer の数は明示的に 0 を設定する必要があります。
job.setNumReduceTasks(0);
// Input Output のテーブル情報の設定
InputUtils.addTable(TableInfo.builder().tableName(args[0]).build(), job);
OutputUtils.addTable(TableInfo.builder().tableName(args[1]).build(), job);
if (args.length == 3) {
String options = new String(args[2]);
// Jobconf はカスタムキーと値を設定でき、getJobConf は getJobConf のコンテキストを介して mapper の関連設定を取得できます。
if (options.contains("setup")) {
job.setBoolean("option.mapper.setup", true);
}
if (options.contains("map")) {
job.setBoolean("option.mapper.map", true);
}
if (options.contains("cleanup")) {
job.setBoolean("option.mapper.cleanup", true);
}
}
Jobclient. runjob (job );
}
}