如何获得星火结构流应用程序的kafka消费者滞后

问题描述 投票:0回答:1

我正在为我的Spark结构化流应用程序构建监视,并且需要让Spark应用程序消耗某个主题的消费者滞后。我相信火花驱动程序必须知道这种滞后,因为它具有执行程序的所有元数据。我看不到有任何方法可以从任何现有的spark文档或资源中获取此指标。我签出了streaminQueryListener接口,但它的功能也很有限,因为我们只能从中获取每个查询指标。

apache-spark apache-kafka spark-structured-streaming
1个回答
0
投票

这里是一种在执行器节点上获取请求信息的方法。为每条消息都提取信息,您可以采用最适合您的需求(数量,时间等)的方式来减少请求数量。

下面我将监视信息发送到另一个Kafka主题。

我经常在每批流消息上打开Kafka使用者连接(以获取有关最大偏移量的信息)。也许这是您无法接受的。

final JavaInputDStream<ConsumerRecord<String, byte[]>> stream = KafkaUtils.createDirectStream(jssc, LocationStrategies.PreferConsistent(),
        ConsumerStrategies.<String, byte[]>Subscribe(topics, kafkaParams));


JavaPairDStream<String, Income> streamPair = stream
.mapPartitionsToPair(new PairFlatMapFunction<Iterator<ConsumerRecord<String, byte[]>>, String, Income>() {

    private Map<String, Object> getProps() {
        Map<String, Object> kafkaParams2 = new HashMap<>();
        kafkaParams2.put("bootstrap.servers", ApiConsts.BOOTSTRAP_SERVERS);
        kafkaParams2.put("key.deserializer", StringDeserializer.class);
        kafkaParams2.put("value.deserializer", ByteArrayDeserializer.class);
        kafkaParams2.put("group.id", "ta_calc_spark" + UUID.randomUUID().toString());
        kafkaParams2.put("auto.offset.reset", "latest");
        kafkaParams2.put("enable.auto.commit", false);
        kafkaParams2.put(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, 30);
        kafkaParams2.put(ConsumerConfig.MAX_POLL_INTERVAL_MS_CONFIG, 1000);
        return kafkaParams2;
    }

    @Override
    public Iterator<Tuple2<String, Income>> call(Iterator<ConsumerRecord<String, byte[]>> t) throws Exception {
        KafkaConsumer consumer = new KafkaConsumer<>(getProps());

        ArrayList<TopicPartition> partitions0 = new ArrayList<TopicPartition>();
        IntStream.range(0, consumer.partitionsFor(ApiConsts.TOPIC_TA_CALC_SPARK_TASK).size())
                .forEach(i -> partitions0.add(new TopicPartition(ApiConsts.TOPIC_TA_CALC_SPARK_TASK, i)));
        consumer.assign(partitions0);
        KafkaProducer producerMonitoring = getKafkaProducer();

        List<Tuple2<String, Income>> result = new ArrayList<Tuple2<String, Income>>();
        try {
            t.forEachRemaining(t2 -> {
                // business logic - message handling

                try {
                    Set<TopicPartition> partitions = new HashSet<TopicPartition>();
                    TopicPartition actualTopicPartition = new TopicPartition(ApiConsts.TOPIC_TA_CALC_SPARK_TASK, t2.partition());
                    partitions.add(actualTopicPartition);
                    Long actualEndOffset = (Long) consumer.endOffsets(partitions).get(actualTopicPartition);
                    long actualPosition = consumer.position(actualTopicPartition);
                    String monitorValue = String.format(
                            "diff: %s   (partition:%s; actualEndOffsetStreaming:%s; actualEndOffset:%s; actualPosition=%s)",
                            actualEndOffset - actualPosition, t2.partition(), t2.offset(), actualEndOffset, actualPosition);
                    ProducerRecord<String, String> pRecord = new ProducerRecord<String, String>(ApiConsts.TOPIC_TA_CALC_SPARK_TEMP_RESULT,
                            UUID.randomUUID().toString(), monitorValue);
                    producerMonitoring.send(pRecord);
                } catch (Exception ex) {
                    log.error("################# mapPartitionsToPair.call() ERROR", ex);
                    ex.printStackTrace();
                }
            });
        } finally {
            producerMonitoring.close();
            consumer.close();
        }
        return result.iterator();
    }
});

输出:

Consumer Record:(f45cd24b-6232-45b2-b8f2-814753ae89bf, diff: 0   (partition:4; actualEndOffsetStreaming:1177; actualEndOffset:1178; actualPosition=1178), 2, 109)
Consumer Record:(3ec4f576-1fff-4c91-885f-fc709f7f4531, diff: 0   (partition:4; actualEndOffsetStreaming:1176; actualEndOffset:1178; actualPosition=1178), 3, 105)
© www.soinside.com 2019 - 2024. All rights reserved.