kafka progress will be reset to the latest consumer location

 

 

 

/ ** 
* Reset kafka consumption schedule
* parameter to specify a broker kafka cluster address, to reset the topic name, consumer groups, and the number of Partition
* /
public
static void seekLatest (String broker, topic String, String Group, int partitionCnt) { the Map <String, Object> = configProps new new the HashMap <> (); configProps.put (ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, Broker); configProps.put (ConsumerConfig.GROUP_ID_CONFIG, Group); configProps.put (ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG , to true ); configProps.put (ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, 100 ); configProps.put (ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG,30000); configProps.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "latest"); configProps.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, LongDeserializer.class); configProps.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, ByteArrayDeserializer.class); DefaultKafkaConsumerFactory<Long, byte[]> factory = new DefaultKafkaConsumerFactory<>(configProps, new LongDeserializer(), new ByteArrayDeserializer()); org.apache.kafka.clients.consumer.KafkaConsumer<Long, byte[]> consumer = (org.apache.kafka.clients.consumer.KafkaConsumer<Long, byte[]>) factory.createConsumer(); consumer.subscribe(Arrays.asList(topic)); List<TopicPartition> topicPartitionLists = new ArrayList<>(); for(int i = 0 ; i < partitionCnt; i ++){ topicPartitionLists.add(new TopicPartition(topic, i)); } consumer.poll(1); Map<TopicPartition, Long> topicPartitionLongMap = consumer.endOffsets(topicPartitionLists); for(TopicPartition tp : topicPartitionLongMap.keySet()){ consumer.seek(tp, topicPartitionLongMap.get(tp)); } consumer.close(); }

 

Guess you like

Origin www.cnblogs.com/ZhengQiZHou/p/12627921.html