尚硅谷大数据技术之Oozie

2018-12-18  本文已影响5人  尚硅谷教育

第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> <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
4.2 案例二:Oozie逻辑调度执行多个Job
目标:使用Oozie执行多个Job调度
分步执行:
1) 解压官方案例模板
[atguigu@hadoop102 oozie-4.0.0-cdh5.3.6]tar -zxf oozie-examples.tar.gz 2) 编写脚本 [atguigu@hadoop102 oozie-4.0.0-cdh5.3.6] vi oozie-apps/shell/p2.sh
内容如下:

!/bin/bash

/bin/date > /tmp/p2.log
3)修改job.properties和workflow.xml文件
job.properties
nameNode=hdfs://hadoop102:8020
jobTracker=hadoop103:8032
queueName=default
examplesRoot=oozie-apps

oozie.wf.application.path={nameNode}/user/{user.name}/{examplesRoot}/shell EXEC1=p1.sh EXEC2=p2.sh 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>{EXEC1}</exec> <file>/user/atguigu/oozie-apps/shell/{EXEC1}#${EXEC1}</file>

<capture-output/>
</shell>
<ok to="p2-shell-node"/>
<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>${EXEC2}</exec>
        <file>/user/admin/oozie-apps/shell/${EXEC2}#${EXEC2}</file>
        <!-- <argument>my_output=Hello Oozie</argument>-->
        <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>
3) 上传任务配置
bin/hadoop fs -rmr /user/atguigu/oozie-apps/ bin/hadoop fs -put oozie-apps/map-reduce /user/atguigu/oozie-apps
4) 执行任务
[atguigu@hadoop102 oozie-4.0.0-cdh5.3.6]$ bin/oozie job -oozie http://hadoop102:11000/oozie -config oozie-apps/shell/job.properties -run

本教程由尚硅谷教育大数据研究院出品,如需转载请注明来源。

上一篇 下一篇

猜你喜欢

热点阅读