對於MapOnly的作業,Map直接將<Key,Value>資訊輸出到MaxCompute的表中。您只需要指定輸出表即可,無需指定Map輸出的Key/Value元資訊。
測試準備
- 準備好測試程式的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;
匯入wc_in表的資料檔案data的內容。hello,odps hello,odps
測試步驟
在MaxCompute用戶端中執行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 |
+------------+------------+
程式碼範例
Pom依賴資訊,請參見注意事項。
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);
/**設定輸入輸出的表資訊。*/
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中可以設定自訂的<key,value>值,在mapper中通過context的getJobConf可以擷取到相關的設定。*/
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);
}
}