CentOS7下oozie调度任务案例

背景

以官方例子为例,记录下如何使用oozie调度任务,首先进入oozie的解压根目录

调度普通任务

1、解压oozie根目录里的oozie-examples.tar.gz

# tar -zxvf oozie-examples.tar.gz

2、在oozie根目录新建目录,将解压得到的examples里的app/shell目录复制其中

# mkdir oozie-apps
# cp -r examples/apps/shell/ oozie-apps/

3、切换到oozie-apps/shell目录下,编写待执行脚本p1.sh

# cd oozie-apps/shell/
# vim p1.sh

p1.sh负责输出当前日期到指定文件

/sbin/date > /home/szc/p1.log

4、修改job.properties文件

# vim job.properties

将nameNode、jobTracker的ip换成自己的,修改exapmplesRoot和oozie.wf.application.path,定义变量EXEC为脚本在examplesRoot下的路径,这里就是p1.sh

nameNode=hdfs://192.168.57.141:8020
jobTracker=192.168.57.141:8032
queueName=default
examplesRoot=oozie-apps


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


EXEC=p1.sh

5、修改workflow.xml文件

# vim workflow.xml

start标签里定义shell-node结点就是任务的起点,所以我们修改shell-node结点,将exec标签修改为${EXEC},表示引用EXEC变量的值;增加file标签,内容为脚本文件在hdfs中的路径#脚本文件;删除原有的check-output和fail-output标签即可

<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/root/oozie-apps/shell/${EXEC}#${EXEC}</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>

6、上传oozie-apps到hdfs中/user/${user.name}目录中,这里用户名为root

# cd ../..
# /home/szc/cdh/hadoop-2.5.0-cdh5.3.6/bin/hadoop fs -put oozie-apps/ /user/root

上传完成后,可以在namenode的webui中找到这个目录和其中三个文件

 7、执行任务,指定oozie地址和配置文件,这里配置文件路径为hdfs中用户名目录下的路径

# bin/oozie job -oozie http://192.168.57.141:11000/oozie -config oozie-apps/shell/job.properties -run
job: 0000003-200428221417435-oozie-root-W

执行成功会得到job的id,并且可以在oozie的webui中看到此任务,并且状态为RUNNING

 多刷新几次,就会在此页面的Done Jobs里看到已经完成的此任务

 任务成功后,我们可以在执行此任务的namenode的/home/szc下看到ps1.log

$ cat /home/szc/p1.log
Thu Apr 30 08:50:57 CST 2020

调度多个任务

1、在oozie根目录\oozie-apps\shell目录下新建ps2.sh,内容如下

/sbin/date > /home/szc/p2.sh

2、修改job.properties,加入EXEC2

nameNode=hdfs://192.168.57.141:8020
jobTracker=192.168.57.141:8032
queueName=default
examplesRoot=oozie-apps


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


EXEC1=p1.sh
EXEC2=p2.sh

 

3、修改workflow.xml,加入shell-node2结点,并修改EXEC相关内容

<workflow-app xmlns="uri:oozie:workflow:0.4" name="shell-wf">
    <start to="shell-node1"/>
    <action name="shell-node1">
        <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>
            <argument>my_output=Hello Oozie</argument>
        <file>/user/root/oozie-apps/shell/${EXEC1}#${EXEC1}</file>
            <capture-output/>
        </shell>
        <ok to="shell-node2"/>
        <error to="fail"/>
    </action>


    <action name="shell-node2">
        <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>
            <argument>my_output=Hello Oozie</argument>
        <file>/user/root/oozie-apps/shell/${EXEC2}#${EXEC2}</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>

4、删除hdfs中原有的shell,上传新的

# /home/szc/cdh/hadoop-2.5.0-cdh5.3.6/bin/hadoop fs -rm -r /user/root/oozie-apps/shell

20/04/30 09:10:23 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
20/04/30 09:10:23 INFO fs.TrashPolicyDefault: Namenode trash configuration: Deletion interval = 0 minutes, Emptier interval = 0 minutes.
Deleted /user/root/oozie-apps/shell

# /home/szc/cdh/hadoop-2.5.0-cdh5.3.6/bin/hadoop fs -put oozie-apps/shell /user/root/oozie-apps

20/04/30 09:10:47 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable

5、执行oozie任务

# bin/oozie job -oozie http://192.168.57.141:11000/oozie -config oozie-apps/shell/job.properties -run

完成后,可以在webui对应任务中的Job DAG中看到任务的有向无环图

最后,在执行map任务的namenode的/home/szc目录下可以看到ps1.log和ps2.sh

$ cat /home/szc/p1.log
Thu Apr 30 09:11:14 CST 2020

$ cat /home/szc/p2.sh
Thu Apr 30 09:11:22 CST 2020

调度定时任务

切换到oozie根目录

1、修改conf/oozie-site.xml,在最后添加时区设置

# vim conf/oozie-site.xml

对应代码如下

    <property>
         <name>oozie.processing.timezone</name>
         <value>GMT+0800</value>
    </property>

2、修改oozie-server/webapps/oozie/oozie-console.js文件,修改时区

# vim oozie-server/webapps/oozie/oozie-console.js

对应代码如下

function getTimeZone() {
     Ext.state.Manager.setProvider(new Ext.state.CookieProvider());
     return Ext.state.Manager.get("TimezoneId","GMT+0800");
}

3、重启oozie服务,清空浏览器缓存

# bin/oozied.sh stop
# bin/oozied.sh start

清空浏览器缓存,重启浏览器,再次进入oozie的webui,查看Done jobs,可以发现时间都变成了东八区时间

 4、把examples目录下apps/cron目录复制到oozie-apps目录下,并切换到里面

# cp -r examples/apps/cron oozie-apps/
# cd oozie-apps/cron/

5、修改job.properties、workflow.xml和coordinator.xml文件

job.properties主要修改namenode的ip、hdfs目录、起止时间。注意,起止时间必须设置成未来时间

nameNode=hdfs://192.168.57.141:8020
jobTracker=192.168.57.141:8032
queueName=default
examplesRoot=oozie-apps


oozie.coord.application.path=${nameNode}/user/${user.name}/${examplesRoot}/cron
start=2020-04-30T11:02+0800 # 开始时间
end=2020-04-30T11:30+0800 # 结束时间
workflowAppUri=${nameNode}/user/${user.name}/${examplesRoot}/cron


EXEC3=p3.sh

p3.sh就是把时间追加到某个文件中

date >> /home/szc/p3.log

workflow.xml主要修改执行的脚本

<workflow-app xmlns="uri:oozie:workflow:0.5" name="one-op-wf">
    <start to="action1"/>
    <action name="action1">
        <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>${EXEC3}</exec>
            <argument>my_output=Hello Oozie</argument>
        <file>/user/root/oozie-apps/cron/${EXEC3}#${EXEC3}</file>
            <capture-output/>
        </shell>
    <ok to="end"/>
    <error to="end"/>
    </action>
    <end name="end"/>
</workflow-app>

coordinator.xml主要修改任务的时间间隔(frequency)为5分钟

<coordinator-app name="cron-coord" frequency="${coord:minutes(5)}" start="${start}" end="${end}" timezone="GMT+0800"
                 xmlns="uri:oozie:coordinator:0.2">
        <action>
        <workflow>
            <app-path>${workflowAppUri}</app-path>
            <configuration>
                <property>
                    <name>jobTracker</name>
                    <value>${jobTracker}</value>
                </property>
                <property>
                    <name>nameNode</name>
                    <value>${nameNode}</value>
                </property>
                <property>
                    <name>queueName</name>
                    <value>${queueName}</value>
                </property>
            </configuration>
        </workflow>
    </action>
</coordinator-app>

6、回到oozie根目录下,把cron目录上传到hdfs中

# /home/szc/cdh/hadoop-2.5.0-cdh5.3.6/bin/hadoop fs -put oozie-apps/cron/ /user/root/oozie-apps/

7、执行任务

# bin/oozie job -oozie http://192.168.57.141:11000/oozie -config oozie-apps/cron/job.properties -run

8、到时间时,在oozie的webUI界面中的Coordinator Jobs标签下,可以看到我们的定时任务

 点击它,发现11点2分和7分的任务已经执行完毕

 并且在文件中也有对应输出

# cat /home/szc/p3.log
Thu Apr 30 11:02:07 CST 2020
Thu Apr 30 11:07:08 CST 2020

调度MapReduce任务

调度MR任务和调度普通任务类似,只是需要在workflow.xml中指定mapper等属性

1、切换到hadoop根目录下,构建wordcount.txt文件,作为测试用例

2、上传wordcount.txt到hdfs,用yarn运行待测MR任务的jar包

# bin/hadoop fs -put wordcount.txt /user/root

# bin/yarn jar share/hadoop/mapreduce/hadoop-mapreduce-examples-2.5.0-cdh5.3.6.jar wordcount /user/root/wordcount.txt /user/root/out

3、完成后,在集群的webui界面找到词频统计应用,点击History

 进入如下界面,点击右侧的Configuration,查看此任务的配置

 

在右侧搜索框中搜索属性,包括map.class、reduce.class、combine.class、output.key、output.value、mapper.new-api、reducer.new-api

4、切换到oozie根目录,把examples/apps/map-reduce目录复制到oozie-apps目录下

# cp -r examples/apps/map-reduce/ oozie-apps/

5、进入oozie-apps/map-reduce目录,修改job.properties文件

nameNode=hdfs://192.168.57.141:8020
jobTracker=192.168.57.141:8032
queueName=default
examplesRoot=oozie-apps


oozie.wf.application.path=${nameNode}/user/${user.name}/${examplesRoot}/map-reduce/workflow.xml
outputDir=map-reduce

6、修改workflow.xml文件,指定map.class、reduce.class、combine.class、output.key、output.value、mapper.new-api、reducer.new-api等属性

<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}/user/${wf:user()}/${examplesRoot}/output-data/${outputDir}"/>
            </prepare>
            <configuration>
                <property>
                    <name>mapred.job.queue.name</name>
                    <value>${queueName}</value>
                </property>


                 <property>
                    <name>mapred.mapper.new-api</name>
                    <value>true</value>
                </property>
                <property>
                    <name>mapreduce.job.map.class</name>
                    <value>org.apache.hadoop.examples.WordCount$TokenizerMapper</value>
                </property>


                 <property>
                    <name>mapred.reducer.new-api</name>
                    <value>true</value>
                </property>
                <property>
                    <name>mapreduce.job.combine.class</name>
                    <value>org.apache.hadoop.examples.WordCount$IntSumReducer</value>
                </property>
                <property>
                    <name>mapreduce.job.reduce.class</name>
                    <value>org.apache.hadoop.examples.WordCount$IntSumReducer</value>
                </property>


                <property>
                    <name>mapreduce.job.output.key.class</name>
                    <value>org.apache.hadoop.io.Text</value>
                </property>
                <property>
                    <name>mapreduce.job.output.value.class</name>
                    <value>org.apache.hadoop.io.IntWritable</value>
                </property>


                <property>
                    <name>mapred.map.tasks</name>
                    <value>1</value>
                </property>
                <property>
                    <name>mapred.input.dir</name>
                    <value>/user/root/wordcount.txt</value>
                </property>
                <property>
                    <name>mapred.output.dir</name>
                    <value>/user/root/output</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>

7、删除lib目录中原有jar包,替换成待测jar包

# rm -rf lib/*
# cp /home/szc/cdh/hadoop-2.5.0-cdh5.3.6/share/hadoop/mapreduce/hadoop-mapreduce-examples-2.5.0-cdh5.3.6.jar lib/

8、上传mapreduce目录到hdfs中/user/root/oozie-apps目录下

# cd ..
# /home/szc/cdh/hadoop-2.5.0-cdh5.3.6/bin/hadoop fs -put map-reduce/ /user/root/oozie-apps/

9、执行任务

# bin/oozie job -oozie http://192.168.57.141:11000/oozie -config oozie-apps/map-reduce/job.properties -run

执行完成后,可以在hdfs中我们指定的输出目录下看到结果

结语 

总结一下oozie这个用于实时任务的调度的hadoop组件

功能模块:

1、workflow:顺序执行流程结点,支持fork、join

2、coordinator:定时触发workflow,控制任务的开始和结束

workflow常用结点:

1、控制流结点:控制工作流的开始、结束以及执行路径等

2、动作结点:执行具体的动作,比如拷贝文件、执行脚本等

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值