1414import java .util .List ;
1515import java .util .Map ;
1616import java .util .Properties ;
17+ import java .util .stream .Collectors ;
18+ import java .util .stream .Stream ;
1719
1820import static java .time .Duration .ofSeconds ;
1921import static java .util .Collections .singleton ;
2022import static org .apache .kafka .clients .consumer .ConsumerConfig .*;
2123import static org .apache .kafka .clients .consumer .ConsumerConfig .BOOTSTRAP_SERVERS_CONFIG ;
2224import static org .apache .kafka .clients .producer .ProducerConfig .*;
2325
24- public class TransactionalApp {
26+ public class TransactionalWordCount {
2527
26- private static final String CONSUMER_GROUP_ID = "test " ;
28+ private static final String CONSUMER_GROUP_ID = "my-group-id " ;
2729 private static final String OUTPUT_TOPIC = "output" ;
2830 private static final String INPUT_TOPIC = "input" ;
2931
3032 public static void main (String [] args ) {
3133
32- KafkaConsumer <String , String > consumer = initConsumer ();
33- KafkaProducer <String , String > producer = initProducer ();
34+ KafkaConsumer <String , String > consumer = createKafkaConsumer ();
35+ KafkaProducer <String , String > producer = createKafkaProducer ();
3436
3537 producer .initTransactions ();
3638
3739 try {
3840
3941 while (true ) {
4042
41- ConsumerRecords <String , String > records = consumer .poll (ofSeconds (20 ));
43+ ConsumerRecords <String , String > records = consumer .poll (ofSeconds (60 ));
44+
45+ Map <String , Integer > wordCountMap = records .records (new TopicPartition (INPUT_TOPIC , 0 ))
46+ .stream ()
47+ .flatMap (record -> Stream .of (record .value ().split (" " )))
48+ .map (word -> Tuple .of (word , 1 ))
49+ .collect (Collectors .toMap (tuple -> tuple .getKey (), t1 -> t1 .getValue (), (v1 , v2 ) -> v1 + v2 ));
4250
4351 producer .beginTransaction ();
4452
45- for (ConsumerRecord record : records )
46- producer .send (new ProducerRecord (OUTPUT_TOPIC , record ));
53+ wordCountMap .forEach ((key , value ) -> producer .send (new ProducerRecord <String , String >(OUTPUT_TOPIC , key , value .toString ())));
4754
4855 Map <TopicPartition , OffsetAndMetadata > offsetsToCommit = new HashMap <>();
4956
5057 for (TopicPartition partition : records .partitions ()) {
5158 List <ConsumerRecord <String , String >> partitionedRecords = records .records (partition );
5259 long offset = partitionedRecords .get (partitionedRecords .size () - 1 ).offset ();
5360
54- offsetsToCommit .put (partition , new OffsetAndMetadata (offset ));
61+ offsetsToCommit .put (partition , new OffsetAndMetadata (offset + 1 ));
5562 }
5663
5764 producer .sendOffsetsToTransaction (offsetsToCommit , CONSUMER_GROUP_ID );
@@ -68,11 +75,12 @@ public static void main(String[] args) {
6875
6976 }
7077
71- private static KafkaConsumer <String , String > initConsumer () {
78+ private static KafkaConsumer <String , String > createKafkaConsumer () {
7279 Properties props = new Properties ();
7380 props .put (BOOTSTRAP_SERVERS_CONFIG , "localhost:9092" );
7481 props .put (GROUP_ID_CONFIG , CONSUMER_GROUP_ID );
7582 props .put (ENABLE_AUTO_COMMIT_CONFIG , "false" );
83+ props .put (ISOLATION_LEVEL_CONFIG , "read_committed" );
7684 props .put (KEY_DESERIALIZER_CLASS_CONFIG , "org.apache.kafka.common.serialization.StringDeserializer" );
7785 props .put (VALUE_DESERIALIZER_CLASS_CONFIG , "org.apache.kafka.common.serialization.StringDeserializer" );
7886
@@ -81,19 +89,14 @@ private static KafkaConsumer<String, String> initConsumer() {
8189 return consumer ;
8290 }
8391
84- private static KafkaProducer <String , String > initProducer () {
92+ private static KafkaProducer <String , String > createKafkaProducer () {
8593
8694 Properties props = new Properties ();
87- props .put (ProducerConfig .BOOTSTRAP_SERVERS_CONFIG , "localhost:9092" );
88- props .put (ACKS_CONFIG , "all" );
89- props .put (RETRIES_CONFIG , 3 );
90- props .put (BATCH_SIZE_CONFIG , 16384 );
91- props .put (LINGER_MS_CONFIG , 1 );
92- props .put (BUFFER_MEMORY_CONFIG , 33554432 );
95+ props .put (BOOTSTRAP_SERVERS_CONFIG , "localhost:9092" );
9396 props .put (ENABLE_IDEMPOTENCE_CONFIG , "true" );
9497 props .put (TRANSACTIONAL_ID_CONFIG , "prod-1" );
95- props .put (ProducerConfig . KEY_SERIALIZER_CLASS_CONFIG , "org.apache.kafka.common.serialization.StringSerializer" );
96- props .put (ProducerConfig . VALUE_SERIALIZER_CLASS_CONFIG , "org.apache.kafka.common.serialization.StringSerializer" );
98+ props .put (KEY_SERIALIZER_CLASS_CONFIG , "org.apache.kafka.common.serialization.StringSerializer" );
99+ props .put (VALUE_SERIALIZER_CLASS_CONFIG , "org.apache.kafka.common.serialization.StringSerializer" );
97100
98101 return new KafkaProducer (props );
99102
0 commit comments