oozie4.1.x 执行shell脚本

介绍

通过 oozie 启动 shell 脚本,理解 oozie 的使用

创建 工作目录

在 /program/oozie-4.1.0-cdh5.14.0 目录下创建 oozie_apps 目录

cd /program/oozie-4.1.0-cdh5.14.0
mkdir oozie_apps

编写shell

创建 目录

在 oozie_apps 目录下创建 std1_shell 目录

cd oozie_apps
mkdir std1_shell
cd std1_shell

编写脚本

vim test_oozie.sh

内容如下:

echo hello world >> /program/oozie_hello.txt

注意:要写生成文件的路径 /program/oozie_hello.txt,否则不好找该文件

测试脚本是否可执行

修改权限:

chmod 777 test_oozie.sh

执行脚本:

./test_oozie.sh

执行结果:在 /program/ 目录下生成 oozie_hello.txt 文件,内容是:

hello world

最后一定要删除 /program/oozie_hello.txt 文件,否则无法验证 是刚才手动执行的脚本,还是由 oozie执行的脚本

编写 job.properties

在 std1_shell 目录下创建 job.properties 文件

vim job.properties

内容如下:

nameNode=hdfs://hadoop1:8020
jobTracker=hadoop2:8032
queueName=default
examplesRoot=oozie_apps
oozie.wf.application.path=${nameNode}/${examplesRoot}/std1_shell
EXEC=test_oozie.sh
shellpath=/${examplesRoot}/std1_shell/${EXEC}

解释:

  • nameNode:namenode ip、port
  • jobTracker:resources Manager ip、port
  • queueName:默认队列
  • examplesRoot:工作目录
  • oozie.wf.application.path:hdfs上的路径
  • EXEC:要执行的脚本
  • shellpath:shell脚本在 hdfs 上的路径

编写 workflow.xml

在 std1_shell 目录下创建 workflow.xml 文件

vim 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>${shellpath}</file>
            <capture-output/>
        </shell>
        <ok to="end"/>
        <error to="fail"/>
    </action>
    <kill name="fail">
        <message>Shell action failed, error message[${wf:errorMessage(wf:lastErrorNode())}]</message>
    </kill>
    <end name="end"/>
</workflow-app>

解释:

  • ${jobTracker}、${nameNode}、${queueName}、${EXEC}、${shellpath} 是 job.properties 文件的配置项

  • <start to="shell-node"/> 定义 shell-node 是任务的起点

job.properties 和 workflow.xml 关系

上传到 hdfs

cd /program/oozie-4.1.0-cdh5.14.0/
hadoop fs -put oozie_apps /

job.properties 可以不上传到 HDFS

重新上传

重新上传需要先删除 hdfs 的相关文件夹

hadoop fs -rm -r /oozie_apps/std1_shell

job.properties 和 hdfs 上文件的关系

执行

cd /program/oozie-4.1.0-cdh5.14.0
bin/oozie job -oozie http://hadoop3:11000/oozie -config oozie_apps/std1_shell/job.properties -run

解释:

  • oozie_apps/std1_shell/job.properties:本机 job.properties 所在的路径
    写绝对路径也可以:/program/oozie-4.1.0-cdh5.14.0/oozie_apps/std1_shell/job.properties

在 oozie web查看

访问 http://hadoop3:11000

注意:建议用 Chrome 或 Firefox 浏览器

在 yarn 查看结果

访问:http://hadoop2:8088/

在服务器查看生成的文件

根据上图的提示,到 hadoop3 的 /program 目录下查找 oozie_hello.txt 文件,如果有该文件,说明任务成功


原文出处:/show_1IX2FgcaJoO9.html