kafka java api 删除,Kafka:删除闲置的消费者组ID

In some cases, I use Kafka-stream to model a small in memory (hashmap) projection of a topic. The K,V cache does require some manipulations, so it is not a good case for a GlobalKTable. In such a “caching” scenario, I want all my sibling instances to have the same cache, so I need to bypass the consumer-group mechanism.

To enable this, I normally simply start my apps with a randomly generated application Id, so each app will reload the topic each time it restarts. The only caveat to that is that I end up with some consumer group orphaned on the kafka brokers up to the offsets.retention.minutes which is not ideal for our operational monitoring tools.

Any idea how to workaround this?

Can we configure the applicationId to be ephemeral in order to have it disappeared once the app dies?

Or could we force the consumer to manage its offset only locally?

Or is there some java adminApi I could use to clean up my consumer-group-id when gracefully shutting down the app?

Thanks

解决方案

There is a Java API in the AdminClient called deleteConsumerGroups which can be used to delete individual ConsumerGroups.

You can use it as shown below with Kafka 2.5.0.

import java.util.Arrays;

import java.util.Properties;

import java.util.concurrent.ExecutionException;

import org.apache.kafka.clients.admin.*;

import org.apache.kafka.common.KafkaFuture;

public class DeleteConsumerGroups {

public static void main(String[] args) {

System.out.println("*** Starting AdminClient to delete a Consumer Group ***");

final Properties properties = new Properties();

properties.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");

properties.put(AdminClientConfig.REQUEST_TIMEOUT_MS_CONFIG, "1000");

properties.put(AdminClientConfig.DEFAULT_API_TIMEOUT_MS_CONFIG, "5000");

AdminClient adminClient = AdminClient.create(properties);

String consumerGroupToBeDeleted = "console-consumer-65092";

DeleteConsumerGroupsResult deleteConsumerGroupsResult = adminClient.deleteConsumerGroups(Arrays.asList(consumerGroupToBeDeleted));

KafkaFuture resultFuture = deleteConsumerGroupsResult.all();

try {

resultFuture.get();

} catch (InterruptedException e) {

e.printStackTrace();

} catch (ExecutionException e) {

e.printStackTrace();

}

adminClient.close();

}

}

ConsumerGroups List before running the code above

$ kafka-consumer-groups --bootstrap-server localhost:9092 --list

console-consumer-65092

console-consumer-53268

ConsumerGroups List after running the code above

$ kafka-consumer-groups --bootstrap-server localhost:9092 --list

console-consumer-53268

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值