I am working on a small batch with Spring batch and Kafka that reads json data from a Kafka topic, converts it to a Student object, changes a value and sends it back into a Kafka topic.
Everything is working fine, but my only problem is that my consumer is ALWAYS reading from the begging of the topic.
I need it to read from the last non consumed message.
I have already added those properties :
ConsumerConfig.AUTO_OFFSET_RESET_CONFIG to earliest
ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG to false
ConsumerConfig.GROUP_ID_CONFIG to a random value
But this doesn't seem to work, on start-up of the consumer, it processes all the messages. Anybody has an idea on how to do it with Spring Batch and Kafka please ? This is my code :
BatchStudent.java :
@SpringBootApplication
@EnableBatchProcessing
@RequiredArgsConstructor
public class BatchStudent {
public static void main(String[] args) {
SpringApplication.run(BatchStudent.class, args);
}
private final JobBuilderFactory jobBuilderFactory;
private final StepBuilderFactory stepBuilderFactory;
private final KafkaTemplate<Integer, Student> template;
private final KafkaProperties properties;
@Value("${kafka.topic.consumer}")
private String topic;
@Bean
public ItemProcessor<Student, Student> customItemProcessor() {
return new CustomProcessor();
}
@Bean
Job job() {
return this.jobBuilderFactory.get("job")
.start(start())
.incrementer(new RunIdIncrementer())
.build();
}
@Bean
KafkaItemWriter<Integer, Student> writer() {
return new KafkaItemWriterBuilder<Integer, Student>()
.kafkaTemplate(template)
.itemKeyMapper(Student::getId)
.build();
}
@Bean
public KafkaItemReader<Integer, Student> reader() {
Properties props = new Properties();
props.putAll(this.properties.buildConsumerProperties());
return new KafkaItemReaderBuilder<Integer, Student>()
.partitions(0)
.consumerProperties(props)
.name("students-consumer-reader")
.saveState(true)
.topic(topic)
.build();
}
@Bean
Step start() {
return this.stepBuilderFactory
.get("step")
.<Student, Student>chunk(10)
.writer(writer())
.processor(customItemProcessor())
.reader(reader())
.build();
}
}
app.yml
spring.batch.initialize-schema: always
#Conf Kafka Consumer
spring.kafka.consumer.key-deserializer: org.apache.kafka.common.serialization.IntegerDeserializer
spring.kafka.consumer.value-deserializer: org.springframework.kafka.support.serializer.JsonDeserializer
#spring.kafka.consumer.group-id: student-group
spring.kafka.consumer.properties.spring.json.trusted.packages: '*'
spring.kafka.consumer.properties.spring.json.value.default.type: com.org.model.Student
#Conf Kafka Producer
spring.kafka.producer.key-serializer: org.apache.kafka.common.serialization.IntegerSerializer
spring.kafka.producer.value-serializer: org.springframework.kafka.support.serializer.JsonSerializer
spring.kafka.producer.bootstrap-servers: localhost:9092
#Conf topics
spring.kafka.template.default-topic: producer.student
kafka.topic.consumer: consumer.student
Student.java
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student {
Integer id;
Integer count;
}
CustomProcessor.java
@NoArgsConstructor
public class CustomProcessor implements ItemProcessor<Student, Student> {
@Override
public Student process(Student studentRecieved) {
final Student studentSent = new Student();
studentSent.setId(studentRecieved.getId());
studentSent.setCount(200);
return studentSent;
}
}
Thank you for your help !
question from:
https://stackoverflow.com/questions/65835903/kafka-spring-batch-how-to-read-only-uncommitted-messages-from-the-same-topic 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…