Apache Oozie 工作流程中的 Java 操作

问题描述 投票:0回答:1

我正在尝试配置 Apache Oozie 工作流程以根据一周中的某一天执行不同的操作。阅读完https://stackoverflow.com/questions/71422257/oozie-coordinator-get-day-of-the-week后,我正在尝试自己开发一个解决方案。

我首先编译这个简单的java源代码:

import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Scanner;

public class DatetimeProcessor {
    public static void main(String[] args) {
        String inputDate = args.length > 0 ? args[0] : "";
        String day = "";
        try {
            day = getDayOfWeek(inputDate);
        } catch (Exception e) {
            System.out.println("Invalid date format. Please enter a valid date in the format 'yyyy-MM-dd'T'HH:mm'Z'.");
        }
        System.out.println("dayOfWeek=" + day);
    }

    public static String getDayOfWeek(String date) {
        LocalDateTime dateTime = LocalDateTime.parse(date, DateTimeFormatter.ISO_DATE_TIME);
        DayOfWeek dayOfWeek = dateTime.atOffset(ZoneOffset.UTC).getDayOfWeek();
        return dayOfWeek.toString().substring(0, 3);
    }
}

构建 jar 文件后,我编写了这个 Oozie 协调器:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<coordinator-app xmlns="uri:oozie:coordinator:0.5" name="mail-test" frequency="* * * * *" start="2024-11-11T00:00Z" end="2024-11-11T00:02Z" timezone="UTC">
    <action>
        <workflow>
            <app-path>/user/my.user.name</app-path>
            <configuration>
                <property>
                    <name>nominalTime</name>
                    <value>${coord:nominalTime()}</value>
                </property>
            </configuration>
        </workflow>
    </action>
</coordinator-app>

但是,当我启动以下工作流程时:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<workflow-app xmlns="uri:oozie:workflow:0.5" name="workflow-test">
    <parameters>
        <property>
            <name>nominalTime</name>
        </property>
    </parameters>
    <start to="process-date-action"/>

<action name="process-date-action">
    <java>
        <job-tracker>${jobTracker}</job-tracker>
        <name-node>${nameNode}</name-node>
        <main-class>DatetimeProcessor</main-class>
        <arg>${nominalTime}</arg>
        <file>${wf:appPath()}/DatetimeProcessor.jar</file>
        <capture-output/>
    </java>
    <ok to="mail-action"/>
    <error to="fail"/>
</action>

    <action name="mail-action">
        <email xmlns="uri:oozie:email-action:0.2">
            <to>[email protected]</to>
            <subject>Mail Test</subject>
            <body>
        Workflow id: ${wf:id()}
        Workflow usr: ${wf:user()}
        Timestamp: ${timestamp()}
        Coordinator nominalTime: ${nominalTime}
        Coordinator Day of the Week: ${wf:actionData("process-date-action")["dayOfWeek"]}
            </body>
        </email>
        <ok to="end"/>
        <error to="fail"/>
    </action>
    <kill name="fail">
        <message>Workflow failed, error message[${wf:errorMessage(wf:lastErrorNode())}]</message>
    </kill>
    <end name="end"/>
</workflow-app>

在“协调员本周日:”之后,我在最终电子邮件中没有看到任何输出。我是否错误地使用了捕获输出标签? 预先感谢您。

java hadoop oozie oozie-coordinator oozie-workflow
1个回答
0
投票

我通过阅读这个主题找到了解决方案:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Properties;

public class DatetimeProcessor {
    public static void main(String[] args) {
        String inputDate = args.length > 0 ? args[0] : "";
        String day = "";
        try {
            day = getDayOfWeek(inputDate);
        } catch (Exception e) {
            System.out.println("Invalid date format. Please enter a valid date in the format 'yyyy-MM-dd'T'HH:mm'Z'.");
        }
        System.out.println("dayOfWeek=" + day);
        
        // Write output to Oozie properties file
        try {
            File outputFile = new File(System.getProperty("oozie.action.output.properties"));
            FileOutputStream fos = new FileOutputStream(outputFile);
            Properties props = new Properties();
            props.setProperty("dayOfWeek", day);
            props.store(fos, "");
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String getDayOfWeek(String date) {
        LocalDateTime dateTime = LocalDateTime.parse(date, DateTimeFormatter.ISO_DATE_TIME);
        DayOfWeek dayOfWeek = dateTime.atOffset(ZoneOffset.UTC).getDayOfWeek();
        return dayOfWeek.toString().substring(0, 3);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.