每秒Flink流式消息

时间:2017-07-03 13:14:32

标签: java apache-flink flink-streaming

我编写了以下代码,该代码使用来自KAFKA生产者的消息,该生产者不断以JSON格式发送消息。任何人都可以帮助我如何计算每秒的消息数量(这将是我设置的吞吐量)

public class FlinkStreamingJob {

public static void main(String[] args) throws Exception {
        // parse input arguments
    final ParameterTool parameterTool = ParameterTool.fromArgs(args);

    if(parameterTool.getNumberOfParameters() < 4) {
        System.out.println("Missing parameters!\nUsage: Kafka --topic <topic> " +
                "--bootstrap.servers <kafka brokers> --zookeeper.connect <zk quorum> --group.id <some id>");
        return;
    }

    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    env.getConfig().disableSysoutLogging();
    env.getConfig().setRestartStrategy(RestartStrategies.fixedDelayRestart(4, 10000));
    env.enableCheckpointing(1000); // create a checkpoint every 1 seconds
    env.getConfig().setGlobalJobParameters(parameterTool); // make parameters available in the web interface
    env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);


    DataStream<String> messageStream = env
            .addSource(new FlinkKafkaConsumer010<>(
                    parameterTool.getRequired("topic"),
                    new SimpleStringSchema(),
                    parameterTool.getProperties()));

    messageStream.map(new MapFunction<String, String>() {

        @Override
        public String map(String value) throws Exception {

            JSONObject json = new JSONObject(value);
            System.out.println("Time for streaming (ms): " +(System.currentTimeMillis() - json.getLong("Time")));

            return value;
        }
    });

    // write kafka stream to standard out
    messageStream.print();
    env.execute();

}
}

任何帮助将不胜感激。提前致谢

1 个答案:

答案 0 :(得分:2)

开箱即用,Flink报告了一些有用的指标,包括每个运算符和任务的numRecordsInPerSecondnumRecordsOutPerSecond。有关内置指标的更多信息,请参阅metrics documentation,并在Web UI和/或REST API中查找数据。或者,您可以使用内置的metrics reporters

之一导出指标