Oozie支持Java action ,Java action 会自动执行workflow任务中制定的java类中的 public static void main(String[] args)方法,会在hadoop集群上以单mapper task的形式执行一个map-reduce job.
workflow任务会等待当前java程序执行完继续执行下一个action,这意味着我们可以写多个action以此来调用多个类. 当java类正确执行退出后,将会进入ok控制流;当发生异常时,将会进入error控制流。
Java action 由以下几个元素组成:
• job-tracker (required)
• name-node (required)
• prepare ---执行删除文件或者创建目录的操作
• configuration ---将里面配置的参数传递给任务
• main-class (required) ---指定执行的java类的全类名(包名.类名)
• java-opts ---提交给驱动程序的参数。
• arg ---提交给java应用的参数
• file ---添加额外所需jar包
• archive
• capture-output ----可以捕获输出
action语法规则如下:
<workflow-app name="[WF-DEF-NAME]" xmlns="uri:oozie:workflow:0.1">
...
<action name="[NODE-NAME]">
<java>
<job-tracker>[JOB-TRACKER]</job-tracker>
<name-node>[NAME-NODE]</name-node>
<prepare>
<delete path="[PATH]"/>
...
<mkdir path="[PATH]"/>
...
</prepare>
<job-xml>[JOB-XML]</job-xml>
<configuration>
<property>
<name>[PROPERTY-NAME]</name>
<value>[PROPERTY-VALUE]</value>
</property>
...
</configuration>
<main-class>[MAIN-CLASS]</main-class>
<java-opts>[JAVA-STARTUP-OPTS]</java-opts>
<arg>ARGUMENT</arg>
...
<file>[FILE-PATH]</file>
...
<archive>[FILE-PATH]</archive>
...
<capture-output />
</java>
<ok to="[NODE-NAME]"/>
<error to="[NODE-NAME]"/>
</action>
...
</workflow-app>
若想调用java类有三个是必需的:1.workflow.xml(名字不可改) 2.job.properties(名字可改) 3.jar包
官网给出的例子:
<workflow-app name="sample-wf" xmlns="uri:oozie:workflow:0.1"> ... <action name="myfirstjavajob"> <java> <job-tracker>foo:8021</job-tracker> <name-node>bar:8020</name-node> <prepare> <delete path="${jobOutput}"/> </prepare> <configuration> <property> <name>mapred.queue.name</name> <value>default</value> </property> </configuration> <main-class>org.apache.oozie.MyFirstMainClass</main-class> <java-opts>-Dblah</java-opts> <arg>argument1</arg> <arg>argument2</arg> </java> <ok to="myotherjob"/> <error to="errorcleanup"/> </action> ... </workflow-app>
我们工作时的例子:
1.workflow.xml---放到hdfs目录中
<workflow-app name="java-example1" xmlns="uri:oozie:workflow:0.5"> <start to="java-Action"/> <action name="java-Action"> <java> <job-tracker>${jobTracker}</job-tracker> <name-node>${nameNode}</name-node> <configuration> <property> <name>mapred.job.queue.name</name> <value>${queueName}</value> </property> </configuration> <main-class>test1.OzzieTest1</main-class> <capture-output/> </java> <ok to="java-Action2"/> <error to="fail"/> </action> <action name="java-Action2"> <java> <job-tracker>${jobTracker}</job-tracker> <name-node>${nameNode}</name-node> <configuration> <property> <name>mapred.job.queue.name</name> <value>${queueName}</value> </property> </configuration> <main-class>test1.OzzieTest1</main-class> <capture-output/> </java> <ok to="end"/> <error to="fail"/> </action> <kill name="fail"> <message>Action failed, error message[${wf:errorMessage(wf:lastErrorNode())}]</message> </kill> <end name="end"/> </workflow-app>
以下几点需要注意:
<workflow-app name="java-example1" xmlns="uri:oozie:workflow:0.5">中的workflow如果设置成0.2那么就不会显示wofkflow的Graph视图,如下图所示:
2.job.properties---放在本地即可
nameNode=hdfs://hgdp-001:8020 -----hdfs地址 jobTracker=hgdp-001:8032 -----jobTracker地址 queueName=default ------oozie队列 hdfspath=user/root examplesRoot=ocn-itv-oozie -----全局目录 oozie.use.system.libpath=True -----是否加载用户lib库(oozie的system share lib) oozie.libpath=${nameNode}/${hdfspath}/${examplesRoot}/lib/ -----用户lib库地址(存放所需的jar包) oozie.wf.application.path=${nameNode}/${hdfspath}/${examplesRoot}/wf/wf4/ ----oozie工作流程workflow.xml所在hdfs中的地址
3.oozie运行:
启动任务:oozie job -config job.properties -run -oozie http://xxxx(地址):11000/oozie