即使状态存在密钥,ProcessElement发出记录

发布于 2025-01-30 12:35:56 字数 2342 浏览 0 评论 0原文

我有一个保存数据2分钟的状态,即使该密钥有一个州,有时仍然会发出记录。

  @Override
    public void processElement(EngagerEvents value, KeyedProcessFunction<String, EngagerEvents, String>.Context ctx, Collector<String> out) throws Exception {
        if (anonymousIdHasBeenSeen.value() == null) {
            System.out.println("time stamp emitting: " +jsonNode.get("server_timestamp"));
            // key is not available in the state
            anonymousIdHasBeenSeen.update(true);
            System.out.println("TIMER START TIME: " +ctx.timestamp());
            out.collect(value.getEventString());
            ctx.timerService().registerProcessingTimeTimer(ctx.timestamp() + (stateTtl * 1000));
        }
    }

TIMER TRIGGER
-------------

  public void onTimer(long timestamp, OnTimerContext ctx, Collector<String> out)
            throws Exception {
        // triggers after ttl has passed
        System.out.println("Call back triggered : time : " +timestamp + " value : " +anonymousIdHasBeenSeen.value());
        if (anonymousIdHasBeenSeen.value()) {
            anonymousIdHasBeenSeen.clear();
        }
    }

我的模拟器代码可生产到Kafka的数据 - IM泵送Anonymid 111,5倍

 public static void main(String[] args) {
        int n = 1; // Number of threads
        for (int i = 0; i < n; i++) {
            Thread thread = new Thread(new ExecutorThread());
            thread.start();
        }


    for (int i = 0; i < 5; i++) {
                ProducerRecord<String, String> record = new ProducerRecord<>("topic", key ,""{"anonymousId": "111", "device": "ios"}"");
                try {
                     producer.send(record);
                     Thread.sleep(500);
                } catch (SerializationException | InterruptedException e) {
                    // may need to do something with it
                }
            }

我用Anonymousid键入。就我而言,只有一个Anonymid 111。回电触发器是60秒,

  DataStream<String> keyedStream = mappedEngagerEventsDataStream.keyBy(EngagerEvents::getAnonymousId)
                .process(new KeyedProcessingWithCallBack(Long.parseLong(60))
                .uid("engager-events-keyed-processing");

我在这里做错了什么?我尝试调试设置一个断点,即使控件不在IF条件内(Sout Insid如果未打印条件),我也会看到发出了特定事件。

我在这里做错了吗?为什么即使我的外出也会发出事件。有人可以指出我在这里做错了什么。

I have a state which holds a data for 2 mins, sometime processElement still emits the record out even though there is a state present for that key.

  @Override
    public void processElement(EngagerEvents value, KeyedProcessFunction<String, EngagerEvents, String>.Context ctx, Collector<String> out) throws Exception {
        if (anonymousIdHasBeenSeen.value() == null) {
            System.out.println("time stamp emitting: " +jsonNode.get("server_timestamp"));
            // key is not available in the state
            anonymousIdHasBeenSeen.update(true);
            System.out.println("TIMER START TIME: " +ctx.timestamp());
            out.collect(value.getEventString());
            ctx.timerService().registerProcessingTimeTimer(ctx.timestamp() + (stateTtl * 1000));
        }
    }

TIMER TRIGGER
-------------

  public void onTimer(long timestamp, OnTimerContext ctx, Collector<String> out)
            throws Exception {
        // triggers after ttl has passed
        System.out.println("Call back triggered : time : " +timestamp + " value : " +anonymousIdHasBeenSeen.value());
        if (anonymousIdHasBeenSeen.value()) {
            anonymousIdHasBeenSeen.clear();
        }
    }

My simulator code which produces data to kafka - im pumping anonymousId 111, 5 times

 public static void main(String[] args) {
        int n = 1; // Number of threads
        for (int i = 0; i < n; i++) {
            Thread thread = new Thread(new ExecutorThread());
            thread.start();
        }


    for (int i = 0; i < 5; i++) {
                ProducerRecord<String, String> record = new ProducerRecord<>("topic", key ,""{"anonymousId": "111", "device": "ios"}"");
                try {
                     producer.send(record);
                     Thread.sleep(500);
                } catch (SerializationException | InterruptedException e) {
                    // may need to do something with it
                }
            }

I'm keying by anonymousId. In my case there is only one anonymousId 111. call back trigger is 60 secs

  DataStream<String> keyedStream = mappedEngagerEventsDataStream.keyBy(EngagerEvents::getAnonymousId)
                .process(new KeyedProcessingWithCallBack(Long.parseLong(60))
                .uid("engager-events-keyed-processing");

Am I doing something wrong here ? I tried debugging setting a break points, Even if the control doesnt go inside the If condition(sout insid If condition aren't printed) I see that particular event emitted out.

Am I doing something wrong here ? Why is the event emitted even though my out.collect inside If statement ? Can someone please point out what am I doing wrong here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文