Slide 21
Slide 21 text
Enabling Transaction Support is Easy
// Java (consume / produce) //
1. KafkaProducer producer = createKafkaProducer(
2. “bootstrap.servers”, “localhost:9092”,
3. “transactional.id”, “my-transactional-id”);
4.
5. producer.initTransactions();
6.
7. KafkaConsumer consumer = createKafkaConsumer(
8. “bootstrap.servers”, “localhost:9092”,
9. “group.id”, “my-group-id”,
10. "isolation.level", "read_committed");
11.
12. consumer.subscribe(singleton(“inputTopic”));
13.
14. while (true) {
15. ConsumerRecords records = consumer.poll(Long.MAX_VALUE);
16. producer.beginTransaction();
17. for (ConsumerRecord record : records)
18. producer.send(producerRecord(“outputTopic”, record));
19. producer.sendOffsetsToTransaction(currentOffsets(consumer), group);
20. producer.commitTransaction();
21. }