全部產品
Search
文件中心

MaxCompute:開發MapReduce

更新時間:Aug 22, 2026

本文為您介紹如何在MaxCompute Studio上開發MapReduce,包括編寫MapReduce、調試MapReduce、打包、上傳和運行MapReduce。

前提條件

您需要完成以下操作:

編寫MapReduce

  1. Project地區,按右鍵Module的源碼目錄(即src > main > java),選擇new > MaxCompute Java

  2. 填寫Name並選擇類型為Driver,按下Enter鍵。

    • Name:建立的MaxCompute Java Class名稱。如果還沒有建立Package,在此處填寫packagename.classname,會自動產生Package。

    • 選擇類型為DriverMapperReducer

      說明

      您可以根據需要選擇Driver、Mapper或Reducer類型:

      • Driver: MapReduce作業中的驅動程式類。負責構建一個MapReduce Job來運行,可以在Driver中指定啟動並執行Mapper和Reducer類以及進行任務的各種資訊配置,其可以看做Mapreduce作業的入口類。

      • Mapper: MapReduce資料處理的第一個階段,在這裡處理每條記錄並產生相應的索引值對。

      • Reducer: Mapper產生的中間輸出到Reducer,Reducer對其進行處理並產生最終輸出,然後將其儲存在MaxCompute表中。

  3. 建立成功後,在編輯介面開發Java程式。

    Java模板已自動填滿架構代碼,您只需設定輸入表、輸出表、Mapper和Reducer類等資訊。

    
    package mymr.myudf;
    
    import ...
    
    public class HelloDriver {
    
        public static void main(String[] args) throws OdpsException {
    
            JobConf job = new JobConf();
    
            // TODO: specify map output types
            job.setMapOutputKeySchema(SchemaUtils.fromString(?));
            job.setMapOutputValueSchema(SchemaUtils.fromString(?));
    
            // TODO: specify input and output tables
            InputUtils.addTable(TableInfo.builder().tableName(?).build(), job);
            OutputUtils.addTable(TableInfo.builder().tableName(?).build(), job);
    
            // TODO: specify a mapper
            job.setMapperClass(?);
            // TODO: specify a reducer
            job.setReducerClass(?);
    
            RunningJob rj = JobClient.runJob(job);
            rj.waitForCompletion();
    
        }
    }
    

通過本地運行調試MapReduce

通過本地運行方式測試,查看MapReduce的運行結果是否符合預期。

  1. 按右鍵編寫完成的Java指令碼,選擇Run

  2. Run/Debug Configurations頁面上選擇此次啟動並執行MaxCompute專案名稱。

    Run/Debug Configurations 對話方塊中,選中左側 JUnit 下的 WordCountTest,右側配置面板中 Test kind 選擇 Class,Class 填寫 com.aliyun.odps.examples.mr.test.WordCountTest,Use classpath of module 選擇 MyFristModule

  3. 單擊OK,開始運行。

    說明
    • 本地運行會讀取warehouse中指定的表資料作為輸入,您可以在控制台查看日誌輸出。

通過單元測試調試MapReduce

您可以參照examples目錄下的WordCount單元測試樣本,編寫測試案例。


package com.aliyun.odps.examples.mr.test;

import ...

public class WordCountTest extends M...

    // 定義輸入輸出表的 schema
    private final static String INPUT_...
    private final static String OUTPUT_...
    private JobConf job;

    public WordCountTest() throws Excep...
        TestUtil.initWarehouse();

        // 準備作業配置
        job = new JobConf();

        job.setMapperClass(WordCount_Token...
        job.setCombinerClass(WordCount_Sum...
        job.setReducerClass(WordCount_SumR...

        job.setMapOutputKeySchema(SchemaUti...
        job.setMapOutputValueSchema(SchemaU...

        InputUtils.addTable(TableInfo.build...
        OutputUtils.addTable(TableInfo.buil...
    }

    @SuppressWarnings("deprecation")
    @Test
    public void testMap() throws IOExce...
        MapUTContext mapContext = new MapUT...
        mapContext.setInputSchema(INPUT_SC...
        mapContext.setOutputSchema(OUTPUT_...

        // 準備測試資料
        Record record = mapContext.createIn...
        record.set(new Text[] {new Text(...

打包上傳

調試成功之後,將Java程式打成JAR包,並作為資源上傳至MaxCompute服務端。詳情請參見打包、上傳和註冊

運行MapReduce

通過MaxCompute用戶端運行MapReduce。

  1. 在左側導覽列,單擊Project Explorer

  2. 按右鍵專案名稱,選擇Open in Console

  3. Console地區,執行如下命令運行MapReduce。

    更多命令請參見JAR命令

    jar -resources wordcount.jar -classpath D:\odps\clt\wordcount.jar com.aliyun.odps.examples.mr.WordCount wc_in wc_out;