简体   繁体   English

如何阅读flink中的前N个kafka消息?

[英]How to read first N kafka messages in flink?

I'm using flink to build a pipeline whose source is kafka. 我正在使用flink构建源为kafka的管道。 For testing, I just want to read the first N messages from kafka, and need to stop the stream after that. 为了进行测试,我只想从kafka中读取前N条消息,然后需要停止流。

How can I do that? 我怎样才能做到这一点? I'm using FlinkKafkaConsumer08 . 我正在使用FlinkKafkaConsumer08

To do anything stateful with Flink you should be using Flink's managed state, so that your application will be fault tolerant. 要对Flink进行有状态的操作,您应该使用Flink的托管状态,以便您的应用程序可以容错。 But if you're willing to overlook that requirement, this could be as simple as this: 但是,如果您愿意忽略该要求,则可以这样简单:

public class Example {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        env.addSource(...)
           .filter(new Limiter())
           .print();

        env.execute();
    }

    public static class Limiter implements FilterFunction<Event> {
        private transient int count = 0;

        @Override
        public boolean filter(Event e) throws Exception {
            if (++count <= 10) {
                return true;
            } else {
                return false;
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM