JAVA-6390: Move kafka articles from libraries-6 to new module
apache-kafka
This commit is contained in:
parent
d13f26e437
commit
b4b48d4268
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.kafka;
|
package com.baeldung.kafka.exactlyonce;
|
||||||
|
|
||||||
import org.apache.kafka.clients.producer.KafkaProducer;
|
import org.apache.kafka.clients.producer.KafkaProducer;
|
||||||
import org.apache.kafka.clients.producer.ProducerRecord;
|
import org.apache.kafka.clients.producer.ProducerRecord;
|
||||||
@ -28,8 +28,8 @@ public class TransactionalMessageProducer {
|
|||||||
|
|
||||||
producer.beginTransaction();
|
producer.beginTransaction();
|
||||||
|
|
||||||
Stream.of(DATA_MESSAGE_1, DATA_MESSAGE_2).forEach(s -> producer.send(
|
Stream.of(DATA_MESSAGE_1, DATA_MESSAGE_2)
|
||||||
new ProducerRecord<String, String>("input", null, s)));
|
.forEach(s -> producer.send(new ProducerRecord<String, String>("input", null, s)));
|
||||||
|
|
||||||
producer.commitTransaction();
|
producer.commitTransaction();
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.kafka;
|
package com.baeldung.kafka.exactlyonce;
|
||||||
|
|
||||||
import org.apache.kafka.clients.consumer.ConsumerRecord;
|
import org.apache.kafka.clients.consumer.ConsumerRecord;
|
||||||
import org.apache.kafka.clients.consumer.ConsumerRecords;
|
import org.apache.kafka.clients.consumer.ConsumerRecords;
|
||||||
@ -44,7 +44,8 @@ public class TransactionalWordCount {
|
|||||||
|
|
||||||
Map<String, Integer> wordCountMap = records.records(new TopicPartition(INPUT_TOPIC, 0))
|
Map<String, Integer> wordCountMap = records.records(new TopicPartition(INPUT_TOPIC, 0))
|
||||||
.stream()
|
.stream()
|
||||||
.flatMap(record -> Stream.of(record.value().split(" ")))
|
.flatMap(record -> Stream.of(record.value()
|
||||||
|
.split(" ")))
|
||||||
.map(word -> Tuple.of(word, 1))
|
.map(word -> Tuple.of(word, 1))
|
||||||
.collect(Collectors.toMap(tuple -> tuple.getKey(), t1 -> t1.getValue(), (v1, v2) -> v1 + v2));
|
.collect(Collectors.toMap(tuple -> tuple.getKey(), t1 -> t1.getValue(), (v1, v2) -> v1 + v2));
|
||||||
|
|
||||||
@ -56,7 +57,8 @@ public class TransactionalWordCount {
|
|||||||
|
|
||||||
for (TopicPartition partition : records.partitions()) {
|
for (TopicPartition partition : records.partitions()) {
|
||||||
List<ConsumerRecord<String, String>> partitionedRecords = records.records(partition);
|
List<ConsumerRecord<String, String>> partitionedRecords = records.records(partition);
|
||||||
long offset = partitionedRecords.get(partitionedRecords.size() - 1).offset();
|
long offset = partitionedRecords.get(partitionedRecords.size() - 1)
|
||||||
|
.offset();
|
||||||
|
|
||||||
offsetsToCommit.put(partition, new OffsetAndMetadata(offset + 1));
|
offsetsToCommit.put(partition, new OffsetAndMetadata(offset + 1));
|
||||||
}
|
}
|
||||||
@ -72,7 +74,6 @@ public class TransactionalWordCount {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static KafkaConsumer<String, String> createKafkaConsumer() {
|
private static KafkaConsumer<String, String> createKafkaConsumer() {
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.kafka;
|
package com.baeldung.kafka.exactlyonce;
|
||||||
|
|
||||||
public class Tuple {
|
public class Tuple {
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user