Apache Flink-全局窗口的显式调用触发器

时间:2018-09-04 08:57:07

标签: apache-flink

我正在为我的信息流创建全局窗口,并尝试在 n 分钟内收集数据。我所做的是在自定义触发器的 onElement 函数中通过自己的逻辑实现的,因此它将在n分钟后触发窗口。但是,如果我在 t 分钟( t )后没有得到某个键的事件,则永远不会触发该键的触发器,这是一个问题。请告诉我是否可以在n分钟后显式调用该键的触发器。

代码

主要:

DataStream<EventData> dataStream = 
                    keyedEvents.window(GlobalWindows.create())
                    .trigger(new ServiceWindowTrigger())
                    .process(new ProcessEventWindow(configData))
                    .name("Collection Window")
                    .setParallelism(25);

触发:

public class ServiceWindowTrigger extends Trigger< MonitoringConsoleData, GlobalWindow> {


@Override
public TriggerResult onElement(MonitoringConsoleData element, long timestamp, GlobalWindow window,
        org.apache.flink.streaming.api.windowing.triggers.Trigger.TriggerContext ctx) throws Exception {



    String key = element.getKey();
    Long firstEventTime = Main.MainMap.get(key);

    // Find difference between first event timestamp and current timestamp
    Long diff = GlobalUtilities.getCurrentEpoch() - firstEventTime;

    long diffMinutes = diff / (60 * 1000) % 60;

    if(diffMinutes == 3)      // Trigger after 3 minutes
    {
        System.out.println(diff);
        System.out.println(diffMinutes);

        return TriggerResult.FIRE;
    }
    else
    {
        return TriggerResult.CONTINUE;
    }


}

@Override
public TriggerResult onProcessingTime(long time, GlobalWindow window,
        org.apache.flink.streaming.api.windowing.triggers.Trigger.TriggerContext ctx) throws Exception {
    // TODO Auto-generated method stub
    return null;
}

@Override
public TriggerResult onEventTime(long time, GlobalWindow window,
        org.apache.flink.streaming.api.windowing.triggers.Trigger.TriggerContext ctx) throws Exception {
    // TODO Auto-generated method stub

    return null;
}

@Override
public void clear(GlobalWindow window, org.apache.flink.streaming.api.windowing.triggers.Trigger.TriggerContext ctx)
        throws Exception {
    // TODO Auto-generated method stub

}

}

0 个答案:

没有答案