前提条件
- テストプログラムの Jar パッケージを準備します。 パッケージの名前を "mapreduce-examples.jar"、ローカルストレージパスを "data\resources" とします。
- SORT をテストするためのテーブルとリソースを準備します。
- テーブルを作成します。
create table ss_in(key bigint, value bigint); create table ss_out(key bigint, value bigint);
- リソースを追加します。
add jar data\resources\mapreduce-examples.jar -f;
- テーブルを作成します。
- tunnel コマンドを使用してデータをインポートします。
tunnel upload data ss_in;
テーブル "ss_in" のデータファイルの内容は次のとおりです。2,1 1,1 3,1
手順
次のように、odpscmd で Sort
を実行します。
jar -resources mapreduce-examples.jar -classpath data\resources\mapreduce-examples.jar
com.aliyun.odps.mapred.open.example.Sort ss_in ss_out;
予想される出力
出力テーブル "ss_out" の内容は以下のとおりです。
+------------+------------+
| key | value |
+------------+------------+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
+------------+------------+
サンプルコード
package com.aliyun.odps.mapred.open.example;
import java.io.IOException;
import java.util.Date;
import com.aliyun.odps.data.Record;
import com.aliyun.odps.data.TableInfo;
import com.aliyun.odps.mapred.JobClient;
import com.aliyun.odps.mapred.MapperBase;
import com.aliyun.odps.mapred.TaskContext;
import com.aliyun.odps.mapred.conf.JobConf;
import com.aliyun.odps.mapred.example.lib.IdentityReducer;
import com.aliyun.odps.mapred.utils.InputUtils;
import com.aliyun.odps.mapred.utils.OutputUtils;
import com.aliyun.odps.mapred.utils.SchemaUtils;
/**
* This is the trivial map/reduce program that does absolutely nothing other
* than use the framework to fragment and sort the input values.
*
**/
public class Sort {
static int printUsage() {
System.out.println("sort <input> <output>");
return -1;
}
/**
* Implements the identity function, mapping record's first two columns to
* outputs.
**/
public static class IdentityMapper extends MapperBase {
private Record key;
private Record value;
@Override
public void setup(TaskContext context) throws IOException{
key = context.createMapOutputKeyRecord();
value = context.createMapOutputValueRecord();
}
@Override
public void map(long recordNum, Record record, TaskContext context)
Throws IOException {
Key.set (new object [] {(long) record.get (0 )});
value.set(new Object[] { (Long) record.get(1) });
context.write(key, value);
}
}
/**
* The main driver for sort program. Invoke this method to submit the
* map/reduce job.
*
* @throws IOException
* When there is communication problems with the job tracker.
**/
public static void main(String[] args) throws Exception {
JobConf jobConf = new JobConf();
jobConf.setMapperClass(IdentityMapper.class);
jobConf.setReducerClass(IdentityReducer.class);
// グローバルオーダーの場合、reducers の数は 1 に設定されています。すべてのデータは reducer 上で連結されます。
// 少量のデータの場合にのみ使用可能です。terasort など、他の方法で考える必要があります。
jobConf.setNumReduceTasks(1);
Jobconf.setmapoutputkeyschema schemautils schemeiutils.fromstring ("key: bigint "));
jobConf.setMapOutputValueSchema(SchemaUtils.fromString("value:bigint"));
InputUtils.addTable(TableInfo.builder().tableName(args[0]).build(), jobConf);
OutputUtils.addTable(TableInfo.builder().tableName(args[1]).build(), jobConf);
Date starttime = new date ();
System.out.println("Job started: " + startTime);
JobClient.runJob(jobConf);
Date end_time = new Date();
System.out.println("Job ended: " + end_time);
System.out.println("The job took "
+ (end_time.getTime() - startTime.getTime()) / 1000 + " seconds.") ;
}
}