[英]Kafka & Spring Batch - How to read ONLY uncommitted messages from the same topic?
我正在使用 Spring 批處理和 Kafka 處理小批量,它從 Kafka 主題讀取 json 數據,將其轉換為學生 object 主題,更改值並將其發送回 Kafka。 一切正常,但我唯一的問題是我的消費者總是從主題的乞求中閱讀。 我需要它從最后一條未消費的消息中讀取。 我已經添加了這些屬性:
ConsumerConfig.AUTO_OFFSET_RESET_CONFIG to earliest
ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG to false
ConsumerConfig.GROUP_ID_CONFIG to a random value
但這似乎不起作用,在消費者啟動時,它會處理所有消息。 有人知道如何使用 Spring Batch 和 Kafka 來做到這一點嗎? 這是我的代碼:
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();
}
}
應用程序.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
學生.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;
}
}
謝謝您的幫助 !
一切正常,但我唯一的問題是我的消費者總是從主題的乞求中閱讀。 我需要它從最后一條未消費的消息中讀取。
Spring Batch 4.3 引入了一種從存儲在 Kafka 中的偏移量中消費記錄的方法。 我在 Spring 的演講中談到了這個功能 去年: Spring Batch 4.3 中有什么新功能? . 您可以使用setPartitionOffsets在每個分區中使用自定義起始偏移量配置 kafka 閱讀器:
Setter for partition offsets. This mapping tells the reader the offset to start reading
from in each partition. This is optional, defaults to starting from offset 0 in each
partition. Passing an empty map makes the reader start from the offset stored in Kafka
for the consumer group ID.
你可以在這個測試用例中找到一個完整的例子。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.