全部產品
Search
文件中心

MaxCompute:MapReduce作業提交

更新時間:Feb 28, 2024

本文向您介紹如何使用Jar命令在MaxCompute用戶端運行和提交MapReduce作業。

MaxCompute用戶端提供Jar命令用於運行MapReduce作業,舉例如下。
jar -conf \home\admin\myconf -resources a.txt,example.jar -classpath ..\lib\example.jar:.\other_lib.jar -D java.library.path=.\native;

文法介紹

jar [<GENERIC_OPTIONS>] <MAIN_CLASS> [ARGS];
        -conf <configuration_file>         Specify an application configuration file
        -resources <resource_name_list>    file\table resources used in mapper or reducer, seperate by comma
        -classpath <local_file_list>       classpaths used to run mainClass
        -D <name>=<value>                  Property value pair, which will be used to run mainClass
        -l                                 Run job in local mode

參數說明

<GENERIC_OPTIONS>包括(均為選擇性參數):
  • -conf <configuration file>:指定JobConf設定檔。該檔案可以影響SDK中JobConf的設定。
    JobConf設定檔的模板如下。
    <configuration>
           <property>
              <name>import.filename</name>
              <value>resource.txt</value>
           </property>
        </configuration>          

    在上述模板中,通過JobConf設定檔定義一個名為import.filename的變數,該變數的值為resource.txt

    您可以在MapReduce程式中通過JobConf介面擷取該變數的值。通過SDK中JobConf介面您可以達到相同的目的,詳情請參見資源使用樣本

    樣本如下。
    add jar data\mapreduce-examples.jar;
        jar -resources mapreduce-examples.jar -classpath data\mapreduce-examples.jar
            org.alidata.odps.mr.examples.WordCount wc_in wc_out;
        add file data\src.txt;
        add jar data\mapreduce-examples.jar;
        jar -resources src.txt,mapreduce-examples.jar -classpath data\mapreduce-examples.jar
            org.alidata.odps.mr.examples.WordCount wc_in wc_out;
        add file data\a.txt;
        add table wc_in as test_table;
        add jar data\work.jar;
        jar -conf odps-mapred.xml -resources a.txt,test_table,work.jar
            -classpath data\work.jar:otherlib.jar
            -D import.filename=resource.txt org.alidata.odps.mr.examples.WordCount args;
  • -resources <resource_name_list>:MapReduce作業運行時使用的資源聲明。一般情況下,resource_name_list中需要指定Map/Reduce函數所用的資源名稱。
    說明
    • 如果在Map/Reduce函數中讀取了其他MaxCompute資源,則這些資源名稱也需要被添加到resource_name_list中。
    • 資源之間使用逗號分隔,使用跨專案空間使用資源時,需要前面加上PROJECT/resources/。例如,-resources otherproject/resources/resfile
    • 在Map/Reduce函數中讀取資源的樣本,請參見資源使用樣本
  • -classpath <local_file_list>:本地執行時的classpath,主要用於指定main函數所在的Jar包的本地路徑(包含相對路徑和絕對路徑)。

    包名之間使用系統預設的檔案分割符作分割。通常情況下,Windows系統中使用分號(;),Linux系統中使用逗號(,)。如果您在雲端伺服器運行MapReduce任務,則使用逗號(,)進行分隔。

    說明 通常,您可能更習慣於將main函數與Map/Reduce函數編寫在一個包中,例如WordCount 程式碼範例。因此,在執行樣本程式時,-resources-classpath的參數中都出現了mapreduce-examples.jar。但二者意義不同,-resources引用的是Map/Reduce函數,運行於分布式環境中。而-classpath引用的是main函數,運行於本地,指定的Jar包路徑也是本地檔案路徑。
  • -D <prop_name>=<prop_value>:本地執行時,<mainClass>的Java屬性,可以定義多個。
  • -l:以本地模式執行MapReduce作業,主要用於程式調試。