Search code examples
javaoozie

Generating Oozie Workflows using Java Code


Looking through Oozie examples and documentation, it looks like you need a workflow file in order to run an oozie job from Java code. Is ther any way to submit a job directly fro Java code, without needing a workflow file? Is there any pre-existing way to dynamically generate these files through java code? Are there any pre-existing tools that will make generating them easier? Or will I have to write the entirety of the code to generate the file?

Current Situation

    OozieClient wc = new OozieClient("http://bar:8080/oozie");

    Properties conf = wc.createConfiguration();
    conf.setProperty(OozieClient.APP_PATH, "workflow file path");
    // set other properties
    ...

    // submit and start the workflow job
    wc.run(conf);

Ideal situation is something vaguely like this.

OozieAction action = new OozieAction("actionName");
action.setOkDestination("nextAction");
action.setErrorDestination("errorDestination");
//Rest of config for action

OozieWorkflow workflow = new Oozieworkflow();
workfow.setStartAction(action);
workflow.addAction(otherAction);
//rest of conf

OozieClient wc = new OozieClient("http://bar:8080/oozie");
wc.runWorkflow(workflow);

Another situation that would be desirable if the former is impossibble is:

OozieAction action = new OozieAction("actionName");
action.setOkDestination("nextAction");
action.setErrorDestination("errorDestination");

//Rest of config for action

OozieWorkflow workflow = new Oozieworkflow();
workfow.setStartAction(action);
workflow.addAction(otherAction);

//rest of conf

workflow.writeToFile("some localFile")


//load file to HDFS

//This would also work
// workflow.writeToHDFS("someHdfsLocation");

OozieClient wc = new OozieClient("http://bar:8080/oozie");

//run with created workflow

Solution

  • Oozie 5.1.0 added support for Fluent Job API which makes it possible to write java code instead of workflow XML files (under the hood, Oozie will generate the XML file for you).

    Simple example for the java code which creates a workflow similar to the shell action demo of Oozie:

    public class MyFirstWorkflowFactory implements WorkflowFactory {
    
        @Override
        public Workflow create() {
            final ShellAction shellAction = ShellActionBuilder.create()
                    .withName("shell-action")
                    .withResourceManager("${resourceManager}")
                    .withNameNode("${nameNode}")
                    .withConfigProperty("mapred.job.queue.name", "${queueName}")
                    .withExecutable("echo")
                    .withArgument("my_output=Hello Oozie")
                    .withCaptureOutput(true)
                    .build();
    
            final Workflow shellWorkflow = new WorkflowBuilder()
                    .withName("shell-workflow")
                    .withDagContainingNode(shellAction).build();
    
            return shellWorkflow;
        }
    }
    

    More detailed documentation can be found here: https://oozie.apache.org/docs/5.1.0/DG_FluentJobAPI.html