0
0
Kafkadevops~10 mins

Transactional producer in Kafka - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to initialize the Kafka producer with transactions enabled.

Kafka
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("enable.idempotence", [1]);
KafkaProducer<String, String> producer = new KafkaProducer<>(props);
Drag options to blanks, or click blank then click option'
Afalse
Btrue
C"true"
D"false"
Attempts:
3 left
💡 Hint
Common Mistakes
Using string "true" instead of boolean true
Setting 'enable.idempotence' to false
Omitting the property entirely
2fill in blank
medium

Complete the code to initialize the transaction with a unique transactional ID.

Kafka
props.put("transactional.id", [1]);
producer.initTransactions();
Drag options to blanks, or click blank then click option'
AtransactionalId
Bmy-transactional-id
C"transactionalId"
D"my-transactional-id"
Attempts:
3 left
💡 Hint
Common Mistakes
Not using quotes around the transactional ID string
Using an invalid or empty transactional ID
3fill in blank
hard

Fix the error in starting a transaction before sending messages.

Kafka
producer.[1]();
producer.send(new ProducerRecord<>("my-topic", "key", "value"));
producer.commitTransaction();
Drag options to blanks, or click blank then click option'
AbeginTransaction
BstartTransaction
CbeginTransactions
DinitTransaction
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'startTransaction' which does not exist
Using plural 'beginTransactions'
Using 'initTransaction' which is incorrect
4fill in blank
hard

Fill both blanks to handle exceptions properly during a transaction.

Kafka
try {
    producer.[1]();
    producer.send(new ProducerRecord<>("my-topic", "key", "value"));
    producer.commitTransaction();
} catch (ProducerFencedException e) {
    producer.close();
} catch (Exception e) {
    producer.[2]();
}
Drag options to blanks, or click blank then click option'
AbeginTransaction
BcommitTransaction
CabortTransaction
Dclose
Attempts:
3 left
💡 Hint
Common Mistakes
Calling commitTransaction() in the catch block instead of abortTransaction()
Not handling ProducerFencedException properly
5fill in blank
hard

Fill all three blanks to create a transactional producer, send a message, and commit the transaction.

Kafka
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("enable.idempotence", [1]);
props.put("transactional.id", [2]);
KafkaProducer<String, String> producer = new KafkaProducer<>(props);
producer.[3]();
producer.beginTransaction();
producer.send(new ProducerRecord<>("my-topic", "key", "value"));
producer.commitTransaction();
Drag options to blanks, or click blank then click option'
Atrue
B"txn-123"
CinitTransactions
DbeginTransaction
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'beginTransaction()' instead of 'initTransactions()' to initialize
Not quoting the transactional ID string
Setting enable.idempotence as a string instead of boolean