Oozie快速入門系列(2) | 一文帶你快速瞭解Oozie的使用(超詳細!!!)

  大家好,我是不溫卜火,是一名計算機學院大數據專業大二的學生,暱稱來源於成語—不溫不火,本意是希望自己性情溫和。作爲一名互聯網行業的小白,博主寫博客一方面是爲了記錄自己的學習過程,另一方面是總結自己所犯的錯誤希望能夠幫助到很多和自己一樣處於起步階段的萌新。但由於水平有限,博客中難免會有一些錯誤出現,有紕漏之處懇請各位大佬不吝賜教!暫時只有csdn這一個平臺,博客主頁:https://buwenbuhuo.blog.csdn.net/

  此篇爲大家帶來的是Oozie的使用。


20

一. Oozie調度shell腳本

  目標:使用Oozie調度Shell腳本
  大體過程如下:
1

  • 1. 創建工作目錄
[bigdata@hadoop002 oozie-4.0.0-cdh5.3.6]$ mkdir oozie-apps/
[bigdata@hadoop002 oozie-apps]$ mkdir shell
[bigdata@hadoop002 oozie-apps]$ cd shell/
  • 2. 新建所需要的兩個文件——job.properties和workflow.xml文件
// 定義工作流程
[bigdata@hadoop002 shell]$ touch workflow.xml
// 
[bigdata@hadoop002 shell]$ touch job.properties

2

  • 3. 修改job.properties和workflow.xml文件
// 1. job.properties
#HDFS地址
nameNode=hdfs://hadoop002:8020
#ResourceManager地址
jobTracker=hadoop003:8032
#隊列名稱
queueName=default
examplesRoot=oozie-apps
oozie.wf.application.path=${nameNode}/user/${user.name}/${examplesRoot}/shell


// 2. workflow.xml
<workflow-app xmlns="uri:oozie:workflow:0.4" name="shell-wf">
<!--開始節點-->
<start to="shell-node"/>
<!--動作節點-->
<action name="shell-node">
    <!--shell動作-->
    <shell xmlns="uri:oozie:shell-action:0.2">
        <job-tracker>${jobTracker}</job-tracker>
        <name-node>${nameNode}</name-node>
        <configuration>
            <property>
                <name>mapred.job.queue.name</name>
                <value>${queueName}</value>
            </property>
        </configuration>
        <!--要執行的腳本-->
        <exec>mkdir</exec>
        <argument>/opt/module/d</argument>
        <capture-output/>
    </shell>
    <ok to="end"/>
    <error to="fail"/>
</action>
<!--kill節點-->
<kill name="fail">
    <message>Shell action failed, error message[${wf:errorMessage(wf:lastErrorNode())}]</message>
</kill>
<!--結束節點-->
<end name="end"/>
</workflow-app>

  • 4. 上傳任務配置
[bigdata@hadoop002 hadoop-2.5.0-cdh5.3.6]$ bin/hadoop fs -put /opt/module/oozie-4.0.0-cdh5.3.6/oozie-apps/ /user/bigdata
  • 5. 執行任務
[bigdata@hadoop002 oozie-4.0.0-cdh5.3.6]$ bin/oozie job -oozie http://hadoop002:11000/oozie -config oozie-apps/shell/job.properties -run

3
  web端查看
4
  程序流程圖對比
5

二. Oozie邏輯調度執行多個Job

  使用Oozie執行多個Job調度,過程如下圖
6

  • 1. 新建文件夾及文件
[bigdata@hadoop002 oozie-apps]$ mkdir xshell
[bigdata@hadoop002 oozie-apps]$ cd xshell/
[bigdata@hadoop002 xshell]$ touch workflow.xml
[bigdata@hadoop002 xshell]$ touch job.properties

7

  • 2. 編輯job.properties和workflow.xml文件
// 1. job.properties

nameNode=hdfs://hadoop002:8020
jobTracker=hadoop003:8032
queueName=default
examplesRoot=oozie-apps
oozie.wf.application.path=${nameNode}/user/${user.name}/${examplesRoot}/xshell

// 2. workflow.xml

<workflow-app xmlns="uri:oozie:workflow:0.4" name="shell-wf">
    <start to="p1-shell-node"/>
    <action name="p1-shell-node">
        <shell xmlns="uri:oozie:shell-action:0.2">
            <job-tracker>${jobTracker}</job-tracker>
            <name-node>${nameNode}</name-node>
            <configuration>
                <property>
                    <name>mapred.job.queue.name</name>
                    <value>${queueName}</value>
                </property>
            </configuration>
            <exec>mkdir</exec>
            <argument>/opt/module/d1</argument>
            <capture-output/>
        </shell>
        <ok to="forking"/>
        <error to="fail"/>
    </action>

    <action name="p2-shell-node">
        <shell xmlns="uri:oozie:shell-action:0.2">
            <job-tracker>${jobTracker}</job-tracker>
            <name-node>${nameNode}</name-node>
            <configuration>
                <property>
                    <name>mapred.job.queue.name</name>
                    <value>${queueName}</value>
                </property>
            </configuration>
            <exec>mkdir</exec>
            <argument>/opt/module/d2</argument>
            <capture-output/>
        </shell>
        <ok to="joining"/>
        <error to="fail"/>
    </action>
    
    <action name="p3-shell-node">
        <shell xmlns="uri:oozie:shell-action:0.2">
            <job-tracker>${jobTracker}</job-tracker>
            <name-node>${nameNode}</name-node>
            <configuration>
                <property>
                    <name>mapred.job.queue.name</name>
                    <value>${queueName}</value>
                </property>
            </configuration>
            <exec>mkdir</exec>
            <argument>/opt/module/d3</argument>
            <capture-output/>
        </shell>
        <ok to="joining"/>
        <error to="fail"/>
    </action>

    <action name="p4-shell-node">
        <shell xmlns="uri:oozie:shell-action:0.2">
            <job-tracker>${jobTracker}</job-tracker>
            <name-node>${nameNode}</name-node>
            <configuration>
                <property>
                    <name>mapred.job.queue.name</name>
                    <value>${queueName}</value>
                </property>
            </configuration>
            <exec>mkdir</exec>
            <argument>/opt/module/d4</argument>
            <capture-output/>
        </shell>
        <ok to="end"/>
        <error to="fail"/>
    </action>

	<fork name="forking">
		<path start = "p2-shell-node"/>
		<path start = "p3-shell-node"/>
	</fork>

	<join name="joining" to="p4-shell-node"/>
		
    
    <kill name="fail">
        <message>Shell action failed, error message[${wf:errorMessage(wf:lastErrorNode())}]</message>
    </kill>
    <end name="end"/>
</workflow-app>

  下圖爲流程圖
10

  • 3. 上傳任務配置
[bigdata@hadoop002 hadoop-2.5.0-cdh5.3.6]$ bin/hadoop fs -rm -r -f  /user/bigdata/oozie-apps/
[bigdata@hadoop002 hadoop-2.5.0-cdh5.3.6]$ bin/hadoop fs -put /opt/module/oozie-4.0.0-cdh5.3.6/oozie-apps/ /user/bigdata/

8

  • 4. 執行任務
[bigdata@hadoop002 oozie-4.0.0-cdh5.3.6]$ bin/oozie job -oozie http://hadoop002:11000/oozie -config oozie-apps/xshell/job.properties -run

9

三. Oozie調度MapReduce任務

目標:使用Oozie調度MapReduce任務

  • 1. 解壓oozie官方案例到ozzie根目錄下
    11
[bigdata@hadoop002 oozie-4.0.0-cdh5.3.6]$ tar -zxvf oozie-examples.tar.gz 

12

  • 2. 進入到所解壓的目錄下
[bigdata@hadoop002 oozie-4.0.0-cdh5.3.6]$ cd examples/
[bigdata@hadoop002 examples]$ cd apps/

13

  • 3.拷貝官方模板到oozie-apps
[bigdata@hadoop002 apps]$ cp -r map-reduce/ ../../oozie-apps/
[bigdata@hadoop002 oozie-4.0.0-cdh5.3.6]$ cd oozie-apps/

// 刪除多餘的這兩個文件,暫時用不到
[bigdata@hadoop002 map-reduce]$ rm job-with-config-class.properties workflow-with-config-class.xml

// 官方案例jar包
[bigdata@hadoop002 map-reduce]$ cp /opt/module/cdh/hadoop-2.5.0-cdh5.3.6/share/hadoop/mapreduce/hadoop-mapreduce-examples-2.5.0-cdh5.3.6.jar lib/

14
15

  • 4. 配置map-reduce任務的job.properties以及workflow.xml
// 1. job.properties
nameNode=hdfs://hadoop002:8020
jobTracker=hadoop003:8032
queueName=default
examplesRoot=oozie-apps
#hdfs://hadoop002:8020/user/admin/oozie-apps/map-reduce/workflow.xml
oozie.wf.application.path=${nameNode}/user/${user.name}/${examplesRoot}/map-reduce/workflow.xml
outputDir=map-reduce

// 2. workflow.xml
<workflow-app xmlns="uri:oozie:workflow:0.2" name="map-reduce-wf">
    <start to="mr-node"/>
    <action name="mr-node">
        <map-reduce>
            <job-tracker>${jobTracker}</job-tracker>
            <name-node>${nameNode}</name-node>
            <prepare>
                <delete path="${nameNode}/output/"/>
            </prepare>
            <configuration>
                <property>
                    <name>mapred.job.queue.name</name>
                    <value>${queueName}</value>
                </property>
                <!-- 配置調度MR任務時,使用新的API -->
                <property>
                    <name>mapred.mapper.new-api</name>
                    <value>true</value>
                </property>

                <property>
                    <name>mapred.reducer.new-api</name>
                    <value>true</value>
                </property>

                <!-- 指定Job Key輸出類型 -->
                <property>
                    <name>mapreduce.job.output.key.class</name>
                    <value>org.apache.hadoop.io.Text</value>
                </property>

                <!-- 指定Job Value輸出類型 -->
                <property>
                    <name>mapreduce.job.output.value.class</name>
                    <value>org.apache.hadoop.io.IntWritable</value>
                </property>

                <!-- 指定輸入路徑 -->
                <property>
                    <name>mapred.input.dir</name>
                    <value>/input/</value>
                </property>

                <!-- 指定輸出路徑 -->
                <property>
                    <name>mapred.output.dir</name>
                    <value>/output/</value>
                </property>

                <!-- 指定Map類 -->
                <property>
                    <name>mapreduce.job.map.class</name>
                    <value>org.apache.hadoop.examples.WordCount$TokenizerMapper</value>
                </property>

                <!-- 指定Reduce類 -->
                <property>
                    <name>mapreduce.job.reduce.class</name>
                    <value>org.apache.hadoop.examples.WordCount$IntSumReducer</value>
                </property>

                <property>
                    <name>mapred.map.tasks</name>
                    <value>1</value>
                </property>
            </configuration>
        </map-reduce>
        <ok to="end"/>
        <error to="fail"/>
    </action>
    <kill name="fail">
        <message>Map/Reduce failed, error message[${wf:errorMessage(wf:lastErrorNode())}]</message>
    </kill>
    <end name="end"/>
</workflow-app>

  • 5. 上傳配置好的app文件夾到HDFS
[bigdata@hadoop002 hadoop-2.5.0-cdh5.3.6]$ bin/hdfs dfs -put /opt/module/oozie-4.0.0-cdh5.3.6/oozie-apps/map-reduce/ /user/bigdata/oozie-apps

16

  • 6. 執行任務
[bigdata@hadoop002 oozie-4.0.0-cdh5.3.6]$ bin/oozie job -oozie http://hadoop002:11000/oozie -config oozie-apps/map-reduce/job.properties -run
// 下圖爲爲正在跑的任務

17
  本次的分享就到這裏了,


11

  好書不厭讀百回,熟讀課思子自知。而我想要成爲全場最靚的仔,就必須堅持通過學習來獲取更多知識,用知識改變命運,用博客見證成長,用行動證明我在努力。
  如果我的博客對你有幫助、如果你喜歡我的博客內容,請“點贊” “評論”“收藏”一鍵三連哦!聽說點讚的人運氣不會太差,每一天都會元氣滿滿呦!如果實在要白嫖的話,那祝你開心每一天,歡迎常來我博客看看。
  碼字不易,大家的支持就是我堅持下去的動力。點贊後不要忘了關注我哦!

13
12

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章