尚硅谷大数据技术之Oozie(新)第4章 Oozie的使用

4.1 案例一:Oozie调度shell脚本

目标:使用Oozie调度Shell脚本

分步实现:

1)解压官方案例模板

[atguigu@hadoop102 oozie-4.0.0-cdh5.3.6]$ tar -zxvf oozie-examples.tar.gz

2)创建工作目录

[atguigu@hadoop102 oozie-4.0.0-cdh5.3.6]$ mkdir oozie-apps/

3)拷贝任务模板到oozie-apps/目录

[atguigu@hadoop102 oozie-4.0.0-cdh5.3.6]$ cp -r examples/apps/shell/ oozie-apps

4)编写脚本p1.sh

[atguigu@hadoop102 oozie-4.0.0-cdh5.3.6]$ vi oozie-apps/shell/p1.sh

内容如下:

#!/bin/bash

/sbin/ifconfig > /opt/module/p1.log

5)修改job.properties和workflow.xml文件

job.properties

#HDFS地址

nameNode=hdfs://hadoop102:8020

#ResourceManager地址

jobTracker=hadoop103:8032

#队列名称

queueName=default

examplesRoot=oozie-apps

oozie.wf.application.path=${nameNode}/user/${user.name}/${examplesRoot}/shell

EXEC=p1.sh

workflow.xml

<workflow-app xmlns="uri:oozie:workflow:0.4" name="shell-wf">

<start to="shell-node"/>

<action name="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>${EXEC}</exec>

        <!-- <argument>my_output=Hello Oozie</argument> -->

        <file>/user/atguigu/oozie-apps/shell/${EXEC}#${EXEC}</file>

 

        <capture-output/>

    </shell>

    <ok to="end"/>

    <error to="fail"/>

</action>

<decision name="check-output">

    <switch>

        <case to="end">

            ${wf:actionData('shell-node')['my_output'] eq 'Hello Oozie'}

        </case>

        <default to="fail-output"/>

    </switch>

</decision>

<kill name="fail">

    <message>Shell action failed, error message[${wf:errorMessage(wf:lastErrorNode())}]</message>

</kill>

<kill name="fail-output">

    <message>Incorrect output, expected [Hello Oozie] but was [${wf:actionData('shell-node')['my_output']}]</message>

</kill>

<end name="end"/>

</workflow-app>

6)上传任务配置

[atguigu@hadoop102 oozie-4.0.0-cdh5.3.6]$ /opt/module/cdh/hadoop-2.5.0-cdh5.3.6/bin/hadoop fs -put oozie-apps/ /user/atguigu

7)执行任务

[atguigu@hadoop102 oozie-4.0.0-cdh5.3.6]$ bin/oozie job -oozie http://hadoop102:11000/oozie -config oozie-apps/shell/job.properties -run

8)杀死某个任务

[atguigu@hadoop102 oozie-4.0.0-cdh5.3.6]$ bin/oozie job -oozie http://hadoop102:11000/oozie -kill 0000004-170425105153692-oozie-z-W