Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

為 Oozie 的 Java 節點指定 Hadoop 屬性

曾遇到這樣的場景:整個需求本來一個 Java 節點就能搞定,無奈要調整 Hadoop 的屬性值 mapreduce.task.classpath.user.precedence。查了半天找不到配置入口,最後只好退而求其次,硬寫了一個 MapReduce 作業去跑那段 Java 邏輯。

後來翻源碼才發現,Oozie 在啟動 javaAction 時,其實是允許你注入 Hadoop 屬性的。

從呼叫鏈看下去

請求從 servlet 一路打到後端,最終進入:

org.apache.oozie.action.hadoop.JavaActionExecutor.submitLauncher(Context context, WorkflowAction action);

在這個方法裡,關鍵是最後提交作業的那一行:

runningJob = jobClient.submitJob(launcherJobConf);

提交的 JobConf 就是 launcherJobConf,而它由下面這行生成:

JobConf launcherJobConf = createLauncherConf(context, action, actionXml, actionConf);

注意這裡用到了 workflow.xml 中該節點的資訊 actionXml。再往下,createLauncherConf 內部會呼叫:

setupLauncherConf(launcherConf, actionXml, appPathRoot, context);

核心:setupLauncherConf 的解析邏輯

Configuration setupLauncherConf(Configuration conf, Element actionXml, Path appPath, Context context) throws ActionExecutorException {
    try {
        Namespace ns = actionXml.getNamespace();
        Element e = actionXml.getChild("configuration", ns);
        if (e != null) {
            String strConf = XmlUtils.prettyPrint(e).toString();
            XConfiguration inlineConf = new XConfiguration(new StringReader(strConf));

            XConfiguration launcherConf = new XConfiguration();
            for (Map.Entry<String,String> entry : inlineConf) {
                if (entry.getKey().startsWith("oozie.launcher.")) {
                    String name = entry.getKey().substring("oozie.launcher.".length());
                    String value = entry.getValue();
                    // 保留原始 KEY
                    launcherConf.set(entry.getKey(), value);
                    // 同時設定去掉前綴的 KEY,讓它能作為 launcher job 的 Hadoop 參數
                    launcherConf.set(name, value);
                }
            }
            checkForDisallowedProps(launcherConf, "inline launcher configuration");
            XConfiguration.copy(launcherConf, conf);
        }
        return conf;
    }
    catch (IOException ex) {
        throw convertException(ex);
    }
}

邏輯寫得很明白:凡是 <configuration> 節點中以 oozie.launcher. 開頭的屬性,都會被塞進 launcher 的 Configuration 中,並且同時保留「帶前綴」與「去前綴」兩份鍵值。

所以,只要在 workflow.xml 裡這樣寫

<action name="java_checkApp">
    <java>
        <job-tracker>${jobTracker}</job-tracker>
        <name-node>${nameNode}</name-node>
        <configuration>
            <property>
                <name>mapred.job.queue.name</name>
                <value>${queueName}</value>
            </property>
            <property>
                <name>oozie.launcher.mapreduce.task.classpath.user.precedence</name>
                <value>true</value>
            </property>
        </configuration>
        <main-class>com.jd.ebsdi.hadoop.mapreduce.ooziemain.main.DoCheck</main-class>
        <arg>{"dbSetPointerType":"pointerTime","wfName":"${wf:name()}","coorTime":"${wf:conf("nominalTime")}","wfPath":"${wf_app_path()}","failClockThresholdValue":"${failThreshold}","checkLockFrequence":"${checkLockSequence}","waitingThresholdValue":"${waitingThreshold}"}</arg>
        <capture-output/>
    </java>
    <ok to="java_InitialData" />
    <error to="kill"/>
</action>

驗證結果

作業跑起來後,Hadoop 會把實際生效的配置寫進 staging 目錄下的 job.xml,路徑形如:

hdfs://hadoop-master.xxx.com:8020/home/data/hadoop/cache/mapred/staging/houchangren/.staging/job_201401261826_18982/job.xml

打開這個檔案,你會看到兩部分內容:一部分是 Oozie 自身的屬性,另一部分則是解析後真正下發給 Hadoop 的屬性——此時 mapreduce.task.classpath.user.precedence 已經按預期出現了。

結論:遇到「只想在 Java 節點裡微調 Hadoop 參數」的需求,不必繞道 MapReduce,加上 oozie.launcher. 前綴即可。

Copyright © Linux教程網 All Rights Reserved