Oozie英文翻译为:驯象人。一个基于工作流引擎的开源框架,由Cloudera公司贡献给Apache,提供对Hadoop MapReduce、Pig Jobs的任务调度与协调。Oozie需要部署到Java Servlet容器中运行。主要用于定时调度任务,多任务可以按照执行的逻辑顺序调度。
Oozie工作流定义,同JBoss jBPM提供的jPDL一样,也提供了类似的流程定义语言hPDL,通过XML文件格式来实现流程的定义。对于工作流系统,一般都会有很多不同功能的节点,比如分支、并发、汇合等等。
Oozie有两个常见节点:
控制流节点(Control Flow Nodes):控制流节点一般都是定义在工作流开始或者结束的位置,比如start,end,kill等。以及提供工作流的执行路径机制,如decision,fork,join等。动作节点(Action Nodes):负责执行具体动作的节点,比如:拷贝文件,执行某个Shell脚本等等。Oozie本质就是一个作业协调工具(底层原理是通过将XML语言转换成MapReduce程序来做,但只是在集中Map端做处理,避免Shuffle的过程。)
执行workflow之前首先要进行相关配置:
job.properties:定义job相关属性以及参数workflow.xml:定义控制流和动作节点lib:存放job任务运行的相关资料文件jar注意:使用Oozie之前必须先启动hdfs,yarn和jobhistory
l 启动任务
oozie job -oozie oozie_url -config job.properties_address -run
l 停止任务
oozie job -oozie oozie_url -kill jobId -oozie-oozi -W
l 提交任务
oozie job -oozie oozie_url -config job.properties_address -submit
l 开始任务
oozie job -oozie oozie_url -config job.properties_address -startJobId -oozie-oozi -W
l 查看任务执行情况
oozie job -oozie oozie_url -config job.properties_address -info jobId -oozie-oozi -W
说明: 所有的命令都是以oozie job -oozie oozie_url开头的-config制定job.properties文件夹的位置,-run文件启动后会返回一个唯一的jobId,供之后使用。
访问Oozie Web页面:http://hadoop100:11000/oozie/
① 创建工作目录
[root@hadoop100 oozie-4.0.0-cdh5.3.6]$ mkdir -p oozie-apps/shell② 在该工作目录下创建job.properties和workflow.xml文件
[root@hadoop100 shell]$ touch workflow.xml [root@hadoop100 shell]$ touch job.properties③ 编辑job.properties和workflow.xml文件
job.properties
#HDFS地址 nameNode=hdfs://hadoop100:8020 #ResourceManager地址 jobTracker=hadoop101:8032 #队列名称 queueName=default examplesRoot=oozie-apps oozie.wf.application.path=${nameNode}/user/${user.name}/${examplesRoot}/shellworkflow.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>④ 上传任务配置
[root@hadoop100 ~]$ /opt/module/cdh/hadoop-2.5.0-cdh5.3.6/bin/hadoop fs \ -put oozie-apps/ /user/root⑤ 执行任务
[root@hadoop100 oozie-4.0.0-cdh5.3.6]$ bin/oozie job -oozie \ http://hadoop102:11000/oozie -config oozie-apps/shell/job.properties -run⑥ 杀死某个任务
[root@hadoop100 oozie-4.0.0-cdh5.3.6]$ bin/oozie job -oozie \ http://hadoop102:11000/oozie -kill 0000004-170425105153692-oozie-z-W进入Oozie Web页面,可以看到整个的执行流程:
①编辑job.properties和workflow.xml文件
job.properties
#HDFS地址 nameNode=hdfs://hadoop100:8020 #ResourceManager地址 jobTracker=hadoop101:8032 #队列名称 queueName=default examplesRoot=oozie-apps oozie.wf.application.path=${nameNode}/user/${user.name}/${examplesRoot}/shellworkflow.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="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>mkdir</exec> <argument>/opt/module/d2</argument> <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>②上传任务配置
[root@hadoop100 ~]$ /opt/module/cdh/hadoop-2.5.0-cdh5.3.6/bin/hadoop fs \ -rmr /user/root/oozie-apps [root@hadoop100 ~]$ /opt/module/cdh/hadoop-2.5.0-cdh5.3.6/bin/hadoop fs \ -put oozie-apps/shells /user/root/oozie-apps③ 执行任务
[root@hadoop100 oozie-4.0.0-cdh5.3.6]$ bin/oozie job -oozie \ http://hadoop102:11000/oozie -config oozie-apps/shell/job.properties -run①oozie-apps下创建文件夹map-reduce,在该文件夹下创建lib目录,并将MapReduce官方案例移到该目录下
[root@hadoop100 oozie-apps]$ mkdir -p ./map-reduce/lib [root@hadoop100 lib]$ cp -a /opt/module/oozie-4.0.0-cdh5.3.6/examples/apps/map-reduce/lib/oozie-examples-4.0.0-cdh5.3.6.jar ./② 编辑job.properties和workflow.xml文件
job.properties
nameNode=hdfs://hadoop100:8020 jobTracker=hadoop101:8032 queueName=default examplesRoot=oozie-apps oozie.wf.application.path=${nameNode}/user/${user.name}/${examplesRoot}/map-reduce/workflow.xml outputDir=map-reduceworkflow.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>③文件目录详情 ④ 上传任务配置
[root@hadoop100 map-reduce]$ /opt/module/cdh/hadoop-2.5.0-cdh5.3.6/bin/hdfs dfs -put ./ \ /user/root/oozie-apps⑤ 执行任务
[root@hadoop100 oozie-4.0.0-cdh5.3.6]$ bin/oozie job -oozie \ http://hadoop100:11000/oozie -config oozie-apps/map-reduce/job.properties -run⑥ 任务执行完成
① 检查当前时区为GMT+0800(上海/北京),若不是另行修改
[root@hadoop100 oozie-4.0.0-cdh5.3.6]$ date -R Fri, 03 Jul 2020 10:14:13 +0800② 配置oozie-site.xml文件,添加东八区属性
<property> <name>oozie.processing.timezone</name> <value>GMT+0800</value> </property>③修改js框架中的关于时间设置的代码
[root@hadoop100 oozie-4.0.0-cdh5.3.6]$ vi /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"); }④重启oozie服务,并重启浏览器 ⑤ 拷贝官方模板配置定时任务
[root@hadoop100 oozie-4.0.0-cdh5.3.6]# cp -r examples/apps/cron oozie-apps/⑥ 编辑job.properties、coordinator.xml和workflow.xml文件
job.properties
nameNode=hdfs://hadoop100:8020 jobTracker=hadoop101:8032 queueName=default examplesRoot=oozie-apps oozie.coord.application.path=${nameNode}/user/${user.name}/${examplesRoot}/cron #start:必须设置为未来时间,否则任务失败 start=2020-07-03T10:40+0800 end=2020-07-03T10:50+0800 workflowAppUri=${nameNode}/user/${user.name}/${examplesRoot}/cron EXEC=p1.shcoordinator.xml
<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>workflow.xml
<workflow-app xmlns="uri:oozie:workflow:0.5" name="one-op-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/root/oozie-apps/cron/${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>p1.sh
#!/bin/bash data >> /opt/module/p1.log⑦ 上传任务配置
[root@hadoop100 cron]$ /opt/module/cdh/hadoop-2.5.0-cdh5.3.6/bin/hdfs dfs -put ./ \ /user/root/oozie-apps⑧ 执行任务
[root@hadoop100 oozie-4.0.0-cdh5.3.6]$ bin/oozie job -oozie \ http://hadoop100:11000/oozie -config oozie-apps/cron/job.properties -run注意:oozie允许的最小执行任务的频率是5分钟
⑨在52分、57分均定时执行了脚本