按照Oozie Official Example: http://oozie.apache.org/docs/3.3.2/DG_Examples.html
写了一个LocalOozie的例子:
import org.apache.oozie.client.OozieClient;
import org.apache.oozie.client.WorkflowAction;
import org.apache.oozie.client.WorkflowJob;
import org.apache.oozie.local.LocalOozie;
import java.io.File;
import java.io.FileInputStream;
import java.net.URL;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
/**
* Oozie provides a embedded Oozie implementation, LocalOozie,
* which is useful for development, debugging and testing of
* workflow applications within the convenience of an IDE.
* <p/>
* User: George Sun
* Date: 6/28/13
* Time: 9:25 PM
*/
public class LocalOozieExample {
public static void main(String[] args) {
LocalOozieExample example = new LocalOozieExample();
System.exit(example.execute(args));
}
private int execute(String... args) {
if (args.length != 2) {
System.out.println();
System.out.println("Expected parameters: <WF_APP_HDFS_URI> <WF_PROPERTIES>");
return -1;
}
String appUri = args[0];
String propertiesFilePath = args[1];
if (propertiesFilePath != null) {
URL defaultPropsFile = getClass().getResource("workflow.xml");
File propFile = new File(propertiesFilePath);
if (!propFile.exists()) {
System.out.println();
System.out.println("Specified properties file don't exists: " + propFile);
System.out.println("Use default workflow.xml instead.");
propertiesFilePath = defaultPropsFile.toString();
}
if (!propFile.isFile()) {
System.out.println();
System.out.println("Specified properties file is not a file: " + propFile);
System.out.println("Use default workflow.xml instead.");
propertiesFilePath = defaultPropsFile.toString();
}
}
try {
// Start local oozie
LocalOozie.start();
// Get a OozieClient for local Oozie
OozieClient oozieClient = LocalOozie.getClient();
// Create a workflow job configuration and set the workflow application path
Properties conf = oozieClient.createConfiguration();
conf.setProperty(OozieClient.APP_PATH, appUri + File.separator + "workflow.xml");
// Load additional workflow job parameters from properties file
if (propertiesFilePath != null) {
conf.load(new FileInputStream(propertiesFilePath));
}
// Submit and start workflow job
String jobId = oozieClient.run(conf);
TimeUnit.SECONDS.sleep(1);
System.out.println("Workflow job submitted");
// Wait until the workflow job finishes printing the status every 10 seconds
while (oozieClient.getJobInfo(jobId).getStatus() == WorkflowJob.Status.RUNNING) {
System.out.println("Workflow job running...");
printWorkflowInfo(oozieClient.getJobInfo(jobId));
TimeUnit.SECONDS.sleep(10);
}
// Print the final status of the workflow job
System.out.println("Workflow job completed.");
printWorkflowInfo(oozieClient.getJobInfo(jobId));
return (oozieClient.getJobInfo(jobId).getStatus() == WorkflowJob.Status.SUCCEEDED) ? 0 : -1;
} catch (Exception e) {
System.err.println("Job executing error: " + Arrays.toString(e.getStackTrace()));
return -1;
} finally {
LocalOozie.stop();
}
}
private static void printWorkflowInfo(WorkflowJob job) {
System.out.println("=============START of WORKFLOW INFO============");
System.out.println("Application Path: " + job.getAppPath());
System.out.println("Application Name: " + job.getAppName());
System.out.println("Application Status: " + job.getStatus());
System.out.println("Application Actions: ");
for (WorkflowAction action : job.getActions()) {
System.out.println(MessageFormat.format("\tName: {0}\tType: {1}\tStatus: {2}",
action.getName(), action.getType(), action.getStatus()));
}
System.out.println("==============END of WORKFLOW INFO=============");
}
}
正如代码javadoc所讲,LocalOozie 是用来在IDE中开发、调试、测试Oozie workflow apps的好东西。但是在运行这个例子的时候遇到一些问题,其中的一个问题到目前尚未搞定,这里记录一下。
- 问题1 - Service.start()调用的时候会去设置OozieHome,代码如下
方案:需要在IDE中指定Java System变量,-Doozie.home.dir=Your oozie homepublic static void setOozieHome() throws ServiceException { oozieHome = System.getProperty(OOZIE_HOME_DIR); if (oozieHome == null) { throw new ServiceException(ErrorCode.E0000); } if (!oozieHome.startsWith("/")) { throw new ServiceException(ErrorCode.E0003, oozieHome); } File file = new File(oozieHome); if (!file.exists()) { throw new ServiceException(ErrorCode.E0004, oozieHome); } }
- 问题2 - java.lang.NoClassDef error. 有好几个jar包需要放到工程的类库里。它们都在$OOZIE_HOME/tools/target/oozie-tools-3.3.2-tools/oozie-tools-3.3.2/libtools下。具体有如下几个:geronimo-jta_1.1_spec-1.1.1.jar, openjpa-jdbc-2.1.0.jar, openjpa-kernel-2.1.0.jar, openjpa-lib-2.1.0.jar, openjpa-persistence-2.1.0.jar, openjpa-persistence-jdbc-2.1.0.jar, serp-1.13.1.jar, geronimo-jpa_2.0_spec-1.1.jar, jdom-1.1.jar. 你也可以把libtools下所有jar全加进去,当然了,要去掉类库中重复的jar。
- 问题3 - Services#setServiceInterval(Class, boolean)方法里会去设置需要使用的Service,其中的JPAService初始化的时候会从你设置的OozieHome路径中读取oozie-core.xml配置。其中一个配置
<property> <name>oozie.service.JPAService.jdbc.url</name> <value>jdbc:derby:${oozie.data.dir}/${oozie.db.schema.name}-db;create=true</value> <!--<value>jdbc:mysql://localhost:3306/oozie</value>--> <description> JDBC URL. </description> </property>
如果你使用了External的MySQL或者其他数据库的话要记得改为内置的Derby DB。 - 问题4 - JPAService#init()方法中有一行代码出错,具体原因暂时还不清楚。
有空接着找原因,如果哪位大神有答案还请留言!EntityManager entityManager = getEntityManager(); // Line no. 158