如何迭代Flink DataStream中的每条消息?

时间:2016-11-03 01:56:21

标签: apache-flink flink-streaming

我有来自Kafka的消息流,如下所示

DataStream<String> messageStream = env
  .addSource(new FlinkKafkaConsumer09<>(topic, new MsgPackDeserializer(), props));

如何迭代流中的每条消息并对其执行某些操作?我在iterate()上看到了DataStream方法,但它没有返回Iterator<String>

1 个答案:

答案 0 :(得分:4)

我认为您正在寻找MapFunction

DataStream<String> messageStream = env.addSource(
    new FlinkKafkaConsumer09<>(topic, new MsgPackDeserializer(), props));

DataStream<Y> mappedMessages = messageStream
  .map(new MapFunction<String, Y>() {
    public Y map(String message) {
      // do something with each message and return Y
    }
  });

如果您不想为每封传入的邮件只发出一条记录,请查看FlatMapFunction