上一篇文章我們簡單介紹了一下Oozie以及怎樣安裝部署Oozie,本文我們通過幾個案例來看一下怎樣使用Oozie,關注專欄《破繭成蝶——大資料篇》,查看更多相關的內容~
目錄
一、Oozie調度shell腳本
二、Oozie調度多個job
三、Oozie調度MapReduce任務
四、Oozie定時任務
一、Oozie調度shell腳本
1、創建目錄
[root@master oozie-4.0.0-cdh5.3.6]# mkdir -p oozie-apps/shell
2、在創建的shell目錄下創建兩個檔案
[root@master shell]# touch workflow.xml job.properties
3、編輯job.properties
#HDFS地址
nameNode=hdfs://master:8020
#ResourceManager地址
jobTracker=slave01:8032
#佇列名稱
queueName=default
examplesRoot=oozie-apps
oozie.wf.application.path=${nameNode}/user/${user.name}/${examplesRoot}/shell
4、編輯workflow.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>
<!--要執行的腳本:創建/root/files目錄下創建xzw目錄-->
<exec>mkdir</exec>
<argument>/root/files/xzw</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>
5、上傳組態檔到HDFS上
[root@master hadoop-2.5.0-cdh5.3.6]# bin/hdfs dfs -put /opt/modules/oozie-4.0.0-cdh5.3.6/oozie-apps/ /user/root
6、執行任務
[root@master oozie-4.0.0-cdh5.3.6]# bin/oozie job -oozie http://master:11000/oozie -config oozie-apps/shell/job.properties -run


被執行的腳本是創建目錄xzw,我們可以去對應目錄看一下是否創建成功:

當然也可以通過監控界面進行查看:

7、殺死某個任務
bin/oozie job -oozie http://master:11000/oozie -kill 0000001-210412154035993-oozie-root-W
二、Oozie調度多個job
1、編輯job.properties
nameNode=hdfs://master:8020
jobTracker=slave01:8032
queueName=default
examplesRoot=oozie-apps
oozie.wf.application.path=${nameNode}/user/${user.name}/${examplesRoot}/shells
2、編輯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>mkdir</exec>
<argument>/root/files/aaa1</argument>
<capture-output/>
</shell>
<ok to="forking"/>
<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>/root/files/aaa2</argument>
<capture-output/>
</shell>
<ok to="joining"/>
<error to="fail"/>
</action>
<action name="p3-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>/root/files/aaa3</argument>
<capture-output/>
</shell>
<ok to="joining"/>
<error to="fail"/>
</action>
<action name="p4-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>/root/files/aaa4</argument>
<capture-output/>
</shell>
<ok to="end"/>
<error to="fail"/>
</action>
<fork name="forking">
<path start="p2-shell-node"/>
<path start="p3-shell-node"/>
</fork>
<join name="joining" to="p4-shell-node"/>
<kill name="fail">
<message>Shell action failed, error message[${wf:errorMessage(wf:lastErrorNode())}]</message>
</kill>
<end name="end"/>
</workflow-app>
3、上傳組態檔到HDFS上
[root@master hadoop-2.5.0-cdh5.3.6]# bin/hdfs dfs -put /opt/modules/oozie-4.0.0-cdh5.3.6/oozie-apps/shells/ /user/root/oozie-apps
4、執行任務
bin/oozie job -oozie http://master:11000/oozie -config oozie-apps/shells/job.properties -run
5、查看執行結果

三、Oozie調度MapReduce任務
1、我們使用Oozie里面自帶的examples,首先我們解壓,
tar -zxvf oozie-examples.tar.gz
2、將examples目錄下的map-reduce案例拷貝到我們oozie-apps目錄下
[root@master apps]# pwd
/opt/modules/oozie-4.0.0-cdh5.3.6/examples/apps
[root@master apps]# cp -r ./map-reduce/ ../../oozie-apps/
3、洗掉不相關的目錄

4、拷貝MapReduce的執行jar包到map-reduce的lib目錄下
[root@master map-reduce]# cp /opt/modules/cdh/hadoop-2.5.0-cdh5.3.6/share/hadoop/mapreduce/hadoop-mapreduce-examples-2.5.0-cdh5.3.6.jar lib/
5、修改workflow.xml檔案
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<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}/xzw/output"/>
</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>mapred.reducer.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>mapreduce.job.reducer.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>mapreduce.input.fileinputformat.inputdir</name>
<value>/xzw/input</value>
</property>
<property>
<name>mapreduce.output.fileoutputformat.outputdir</name>
<value>/xzw/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>
6、修改job.properties檔案
nameNode=hdfs://master:8020
jobTracker=slave01:8032
queueName=default
oozie.wf.application.path=${nameNode}/user/${user.name}/oozie-apps/map-reduce/workflow.xml
7、上傳到HDFS中
bin/hdfs dfs -put /opt/modules/oozie-4.0.0-cdh5.3.6/oozie-apps/map-reduce/ /user/root/oozie-apps
8、執行任務
bin/oozie job -oozie http://master:11000/oozie -config oozie-apps/map-reduce/job.properties -run
9、查看執行結果

四、Oozie定時任務
1、配置oozie-site.xml檔案
<property>
<name>oozie.processing.timezone</name>
<value>GMT+0800</value>
<description>
Oozie server timezone. Valid values are UTC and GMT(+/-)####, for example 'GMT+0530' would be India
timezone. All dates parsed and genered dates by Oozie Coordinator/Bundle will be done in the specified
timezone. The default value of 'UTC' should not be changed under normal circumtances. If for any reason
is changed, note that GMT(+/-)#### timezones do not observe DST changes.
</description>
</property>
2、重啟Oozie,并將時區改為東八區

3、將examples目錄下的cron案例拷貝到我們oozie-apps目錄下

4、修改coordinator.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>
5、修改workflow.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>append_time.sh</exec>
<file>/user/root/oozie-apps/cron/append_time.sh</file>
<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>
6、修改job.properties組態檔
nameNode=hdfs://master:8020
jobTracker=slave01:8032
queueName=default
oozie.coord.application.path=${nameNode}/user/${user.name}/oozie-apps/cron
start=2021-04-16T15:50+0800
end=2021-04-16T16:25+0800
workflowAppUri=${nameNode}/user/${user.name}/oozie-apps/cron
7、上傳到HDFS上
[root@master hadoop-2.5.0-cdh5.3.6]# bin/hdfs dfs -put /opt/modules/oozie-4.0.0-cdh5.3.6/oozie-apps/cron/ /user/root/oozie-apps
8、執行任務
bin/oozie job -oozie http://master:11000/oozie -config oozie-apps/cron/job.properties -run
9、查看結果

以上就是本文的所有內容,比較簡單,你們在此程序中遇到了什么問題,歡迎留言,讓我看看你們都遇到了哪些問題~
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/278047.html
標籤:其他
