分享

Oozie案例分析及配置文件说明

阿飞 2019-5-22 12:35:05 发表于 介绍解说 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 0 5465
问题导读

1.Oozie是什么?
2.Oozie的功能模块有哪些?
3.本文有哪些案例?

1、Oozie英文翻译
驯象人

2、Oozie简介
一个基于工作流引擎的开源框架,由Cloudera公司贡献给Apache,提供对Hadoop Mapreduce、Pig Jobs的任务调度与协调。
Oozie需要部署到Java Servlet容器中运行。

3、Oozie在集群中扮演的角色
定时调度任务,多任务可以按照执行的逻辑顺序调度。

4、Oozie的功能模块
4.1、Workflow
顺序执行流程节点,支持fork(分支多个节点),join(合并多个节点为一个)

4.2、Coordinator
定时触发workflow

4.3、Bundle Job
绑定多个Coordinator

5、Oozie的节点
5.1、控制流节点(Control Flow Nodes)
控制流节点一般都是定义在工作流开始或者结束的位置,比如start,end,kill等。以及提供工作流的执行路径机制,
如decision,fork,join等。

5.2、动作节点(Action Nodes)
就是执行具体任务动作的节点。

6、Oozie的安装与部署
6.1、解压Oozie
$ tar -zxf /opt/softwares/oozie-4.0.0-cdh5.3.6.tar.gz -C /opt/modules/cdh/
6.2、Hadoop配置文件修改,完成后scp到其他机器节点
6.2.1、core-site.xml


        <!-- 允许被Oozie代理的用户组 -->
                                  <property>
                                    <name>hadoop.proxyuser.admin.groups</name>
                                            <value>*</value>
                                  </property>
6.2.2、配置JobHistoryServer服务(必须)
                                  mapred-site.xml
                                      <!-- 配置 MapReduce JobHistory Server 地址 ,默认端口10020 -->
                                    <property>
                                        <name>mapreduce.jobhistory.address</name>
                                        <value>hadoop-senior01.itguigu.com:10020</value>
                                    </property>

                                    <!-- 配置 MapReduce JobHistory Server web ui 地址, 默认端口19888 -->
                                    <property>
                                        <name>mapreduce.jobhistory.webapp.address</name>
                                        <value>hadoop-senior01.itguigu.com:19888</value>
                                    </property>
                                 yarn-site.xml
                                         <!-- 任务历史服务 -->
                                         <property>
                                                <name>yarn.log.server.url</name>
                                                <value>http://hadoop-senior01.itguigu.c ... logs/</value>
                                        </property>

                                完成后:记得scp同步到其他机器节点
6.3、开启Hadoop集群
                        $ sh ~/start-cluster.sh
                        尖叫提示:需要配合开启JobHistoryServer

                        最好执行一个MR任务进行测试。
6.4、解压hadooplibs
                        $ tar -zxf /opt/modules/cdh/oozie-4.0.0-cdh5.3.6/oozie-hadooplibs-4.0.0-cdh5.3.6.tar.gz -C /opt/modules/cdh/
                        完成后Oozie目录下会出现hadooplibs目录
6.5、在Oozie目录下创建libext目录
                        $ mkdir libext/
6.6、拷贝一些依赖的Jar包
                        6.6.1、将hadooplibs里面的jar包,拷贝到libext目录下
                                $ cp -ra /opt/modules/cdh/oozie-4.0.0-cdh5.3.6/hadooplibs/hadooplib-2.5.0-cdh5.3.6.oozie-4.0.0-cdh5.3.6/* libext/
                        6.6.2、拷贝Mysql驱动包到libext目录下
                                $ cp -a /opt/softwares/mysql-connector-java-5.1.27/mysql-connector-java-5.1.27-bin.jar /opt/modules/cdh/oozie-4.0.0-cdh5.3.6/libext/
6.7、将ext-2.2.zip拷贝到libext/目录下
                        $ cp /opt/softwares/ext-2.2.zip libext/
6.8、修改Oozie配置文件
                        6.8.1、oozie-site.xml
                                ** JDBC驱动
                                        oozie.service.JPAService.jdbc.driver
                                        com.mysql.jdbc.Driver

                                ** Mysql的oozie数据库的配置
                                        oozie.service.JPAService.jdbc.url
                                        jdbc:mysql://192.168.122.20:3306/oozie

                                ** 数据库用户名
                                        oozie.service.JPAService.jdbc.username
                                        root

                                ** 数据库密码
                                        oozie.service.JPAService.jdbc.password
                                        123456

                                ** 让Oozie引用Hadoop的配置文件
                                        oozie.service.HadoopAccessorService.hadoop.configurations
                                        真的就是这样:--> *=/opt/modules/cdh/hadoop-2.5.0-cdh5.3.6/etc/hadoop

6.9、在Mysql中创建Oozie的数据库
                        6.9.1、进入数据库
                                $ mysql -uroot -p123456
                        6.9.2、创建oozie数据库
                                mysql> create database oozie;
6.10、初始化Oozie的配置
                        6.10.1、上传Oozie目录下的yarn.tar.gz文件到HDFS(尖叫提示:yarn.tar.gz文件会自行解压)
                                $ bin/oozie-setup.sh sharelib create -fs hdfs://hadoop-senior01.itguigu.com:8020 -locallib oozie-sharelib-4.0.0-cdh5.3.6-yarn.tar.gz
                               
                                执行成功之后,去50070检查对应目录有没有文件生成。
                        6.10.2、创建oozie.sql文件
                                $ bin/oozie-setup.sh db create -run -sqlfile oozie.sql
                        6.10.3、打包项目,生成war包
                                $ bin/oozie-setup.sh prepare-war
6.11、启动Oozie服务
                        $ bin/oozied.sh start
                        (关闭Oozie服务:$ bin/oozied.sh stop)
6.12、访问Oozie的Web页面
                        http://hadoop-senior01.itguigu.com:11000/oozie
       
7、案例


7.1、案例一:使用Oozie调度Shell脚本
                        7.1.1、解压官方案例模板
                                $ tar -zxf oozie-examples.tar.gz
                        7.1.2、创建工作目录
                                $ mkdir oozie-apps/
                        7.1.3、拷贝任务模板到oozie-apps/目录
                                $ cp -r examples/apps/shell/ oozie-apps/
                        7.1.4、随意编写一个脚本p1.sh
                                $ vi /opt/modules/cdh/oozie-4.0.0-cdh5.3.6/oozie-apps/shell/p1.log
                                内容如下:
                                #!/bin/bash
                                /usr/sbin/ifconfig > /tmp/p1.log
                        7.1.5、修改job.properties和workflow.xml文件
                                ** job.properties
                                                #HDFS地址
                                                nameNode=hdfs://hadoop-senior01.itguigu.com:8020
                                                #ResourceManager地址
                                                jobTracker=hadoop-senior02.itguigu.com: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/admin/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>

                        7.1.6、上传任务配置
                                $ /opt/modules/cdh/hadoop-2.5.0-cdh5.3.6/bin/hdfs dfs -put /opt/modules/cdh/oozie-4.0.0-cdh5.3.6/oozie-apps /user/admin
                        7.1.7、执行任务
                                $ bin/oozie job -oozie http://hadoop-senior01.itguigu.com:11000/oozie -config oozie-apps/shell/job.properties -run
                        7.1.8、杀死某个任务
                                $ bin/oozie job -oozie http://hadoop-senior01.itguigu.com:11000/oozie -kill 0000004-170425105153692-oozie-z-W

7.2、案例二:执行多个Job调度
                        7.2.1、解压官方案例模板
                                job.properties
                                nameNode=hdfs://hadoop-senior01.itguigu.com:8020
                                jobTracker=hadoop-senior02.itguigu.com: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/admin/oozie-apps/shell/${EXEC1}#${EXEC1}</file>
                                            <!-- <argument>my_output=Hello Oozie</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>${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>
                                测试时需要留意的点:
                                        1、每个脚本是在哪台机器上执行的。
                                        2、脚本的执行时间。
                                        3、发现一个问题:执行任务的时间和本地时间不一致。

7.3、案例三:调度MapReduce任务
                                7.3.1、先编写一个可以运行的MR任务的.jar包
                                7.3.2、拷贝官方模板到oozie-apps
                                        $ cp -r /opt/modules/cdh/oozie-4.0.0-cdh5.3.6/examples/apps/map-reduce/ /opt/modules/cdh/oozie-4.0.0-cdh5.3.6/oozie-apps/
                                7.3.3、测试一下wordcount在yarn中的运行
                                        $ /opt/modules/cdh/hadoop-2.5.0-cdh5.3.6/bin/yarn jar hadoop-mapreduce-examples-2.5.0-cdh5.3.6.jar wordcount /input/ /output111/
                                7.3.4、配置map-reduce任务
                                        job.properties
                                        nameNode=hdfs://hadoop-senior01.itguigu.com:8020
                                        jobTracker=hadoop-senior02.itguigu.com:8021
                                        queueName=default
                                        examplesRoot=oozie-apps
                                        #hdfs://hadoop-senior01.itguigu.com:8020/user/admin/oozie-apps/map-reduce/workflow.xml
                                        oozie.wf.application.path=${nameNode}/user/${user.name}/${examplesRoot}/map-reduce/workflow.xml
                                        outputDir=map-reduce

                                        workflow.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>

                                7.3.5、拷贝待执行的jar包到map-reduce的lib目录下
                                        $ cp -a /opt/modules/cdh/oozie-4.0.0-cdh5.3.6/hadoop-mapreduce-examples-2.5.0-cdh5.3.6.jar ./
                                7.3.6、上传配置好的app文件夹到HDFS
                                        $ /opt/modules/cdh/hadoop-2.5.0-cdh5.3.6/bin/hdfs dfs -put map-reduce/ /user/admin/oozie-apps
                                7.3.7、执行任务
                                        $ bin/oozie job -oozie http://hadoop-senior01.itguigu.com:11000/oozie -config oozie-apps/map-reduce/job.properties -run
7.4、案例四:Coordinator周期性调度任务
                        7.4.1、配置Linux时区
                                详见Hive第一天Word文档
                        7.4.2、修改oozie-default.xml文件,涉及属性:
                                ** 修改oozie的时区
                                        oozie.processing.timezone
                                        GMT+0800
                        7.4.3、修改js框架中的关于时间设置的代码
                                $ vi /opt/modules/cdh/oozie-4.0.0-cdh5.3.6/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");
                                        }
                        7.4.4、重启oozie服务,并重启浏览器(一定要注意清除缓存)
                                        $ bin/oozied.sh stop
                                        $ bin/oozied.sh start
                        7.4.5、拷贝官方模板配置定时任务
                                $ cp -r /opt/modules/cdh/oozie-4.0.0-cdh5.3.6/examples/apps/cron/ /opt/modules/cdh/oozie-4.0.0-cdh5.3.6/oozie-apps/
                        7.4.6、修改模板
                                job.properties
                                       
                                        nameNode=hdfs://hadoop-senior01.itguigu.com:8020
                                        jobTracker=hadoop-senior02.itguigu.com:8032
                                        queueName=default
                                        examplesRoot=oozie-apps

                                        oozie.coord.application.path=${nameNode}/user/${user.name}/${examplesRoot}/apps/cron
                                        #start:必须设置为未来时间,否则任务失败
                                        start=2017-07-29T17:00+0800
                                        end=2017-07-30T17:00+0800
                                        workflowAppUri=${nameNode}/user/${user.name}/${examplesRoot}/cron

                                        EXEC1=p1.sh
                                        EXEC2=p2.sh

                                coordinator.xml
                                <coordinator-app name="cron-coord" frequency="${coord:minutes(10)}" 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="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/admin/oozie-apps/shell/${EXEC1}#${EXEC1}</file>
                                            <!-- <argument>my_output=Hello Oozie</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>${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>
                        7.4.7、上传配置,启动任务
                                $ /opt/modules/cdh/hadoop-2.5.0-cdh5.3.6/bin/hdfs dfs -put oozie-apps/cron/ /user/admin/oozie-apps
                                $ bin/oozie job -oozie http://hadoop-senior01.itguigu.com:11000/oozie -config oozie-apps/cron/job.properties -run
                                (尖叫提示:oozie允许的最小执行任务的频率是5分钟)


最新经典文章,欢迎关注公众号


本帖被以下淘专辑推荐:

没找到任何评论,期待你打破沉寂

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条