如何知道从kafka队列消耗的数据有多少以及kafka队列主题中存在的数据是什么?

时间:2016-10-26 11:08:56

标签: java stream streaming apache-kafka kafka-consumer-api

我正在" topicDemo"中生成数据并使用数据。 Kafka启用分布式数据并并行处理这些数据。

但实时情况需要监控当前队列中的数据量( topicDemo )以及消耗的数据量( topicDemo )。

是否有任何kafka API可用于提供这些详细信息?

这是我正在生成数据的代码

  // create instance for properties to access producer configs
        Properties props = new Properties();

        // props.put("serializer.class",
        // "kafka.serializer.StringEncoder");
        props.put("bootstrap.servers", "localhost:9092");
        props.put("metadata.broker.list", "localhost:9092");

        props.put("producer.type", "async");
        props.put("batch.size", "500");
        props.put("compression.codec", "1");
        props.put("compression.topic", "topicdemo");
        // props.put("key.serializer",
        // "org.apache.kafka.common.serialization.StringSerializer");
        props.put("key.serializer", "org.apache.kafka.common.serialization.IntegerSerializer");
        props.put("value.serializer", "org.apache.kafka.common.serialization.ByteArraySerializer");

        org.apache.kafka.clients.producer.Producer<Integer, byte[]> producer = new KafkaProducer<Integer, byte[]>(
                props);

            producer.send(new ProducerRecord<Integer, byte[]>("topicdemo", resume.getBytes()));
        producer.close();

这是我正在使用数据的代码

  String topicName = "topicDemo";

    Properties props = new Properties();

    props.put("bootstrap.servers", "localhost:9092");
    props.put("group.id", "test");
    props.put("enable.auto.commit", "true");
    props.put("auto.commit.interval.ms", "1000");
    props.put("session.timeout.ms", "30000");
    props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
    props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");

    KafkaConsumer<String, String> consumer = new KafkaConsumer<String, String>(props);

    // Kafka Consumer subscribes list of topics here.
    consumer.subscribe(Arrays.asList(topicName));

    try {
        while (true) {
            ConsumerRecords<String, String> records = consumer.poll(5);
            for (ConsumerRecord<String, String> record : records) {
                Consumer cons = new Consumer();
                if (cons.SaveDetails(record.value()) == 1) {
                    consumer.commitSync();
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        consumer.close();
    }

1 个答案:

答案 0 :(得分:0)

执行以下命令检查您为每个分区生成的消息数量:

  

bin / kafka-run-class.sh kafka.tools.GetOffsetShell --broker-list host:port --topic topic_name --time -1

执行以下命令检查消费者已消耗和滞后的消息数量:

  

bin / kafka-consumer-groups.sh --bootstrap-server broker1:9092 --describe --group test-consumer-group(for old consumer)

     

bin / kafka-consumer-groups.sh --bootstrap-server broker1:9092 --describe --group test-consumer-group --new-consumer(for new consumer)