This topic describes how to use MapReduce for data sorting.


  1. Prepare the JAR package of the test program. In this example, the JAR package is named mapreduce-examples.jar and saved in the data\resources directory.
  2. Prepare test tables and resources.
    1. Create tables.
      create table ss_in(key bigint, value bigint);
      create table ss_out(key bigint, value bigint);
    2. Add resources.
      add jar data\resources\mapreduce-examples.jar -f;
  3. Use Tunnel to import data.
    tunnel upload data ss_in;
    The following data is imported to the ss_in table:


Sort data on the MaxCompute client.
jar -resources mapreduce-examples.jar -classpath data\resources\mapreduce-examples.jar ss_in ss_out;

Expected results

If the job succeeds, the following result is returned:
| key        | value      |
| 1          | 1          |
| 2          | 1          |
| 3          | 1          |

Sample code

import java.util.Date;
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;
            public void setup(TaskContext context) throws IOException {
            key = context.createMapOutputKeyRecord();
            value = context.createMapOutputValueRecord();
            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();
        // For global sorting, the number of reducers is set to 1. All the data is transferred to the same reducer.
        // This method applies only to the scenarios when small amounts of data are processed. If large amounts of data need to be processed, use other methods, such as TeraSort.
        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);
        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.") ;