Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to set the number of partitions when creating a Kafka topic.
Kafka
NewTopic topic = new NewTopic("my-topic", [1], (short) 1);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string instead of an integer for partitions.
Using a variable name without declaration.
✗ Incorrect
The number of partitions must be an integer value, like 3, not a string or variable name.
2fill in blank
mediumComplete the code to retrieve the partition count of a Kafka topic using AdminClient.
Kafka
DescribeTopicsResult result = adminClient.describeTopics(Collections.singleton("my-topic")); Map<String, TopicDescription> descriptions = result.all().get(); int partitionCount = descriptions.get("my-topic").[1]().size();
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method that does not exist like getPartitionCount.
Using a field name instead of a method.
✗ Incorrect
The method getPartitions() returns the list of partitions for the topic.
3fill in blank
hardFix the error in the code that sets the partition count dynamically based on a variable.
Kafka
int partitions = 5; NewTopic topic = new NewTopic("my-topic", [1], (short) 1);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Putting the variable name in quotes making it a string.
Trying to parse an int variable as if it were a string.
✗ Incorrect
The constructor expects an int for partitions, so use the variable directly without quotes or conversion.
4fill in blank
hardFill both blanks to create a topic with a partition count and replication factor.
Kafka
NewTopic topic = new NewTopic("my-topic", [1], (short) [2]);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using replication factor greater than available brokers.
Using zero or negative numbers.
✗ Incorrect
The topic is created with 3 partitions and replication factor 1.
5fill in blank
hardFill all three blanks to create a topic and check if the partition count is greater than 3.
Kafka
NewTopic topic = new NewTopic("my-topic", [1], (short) [2]); int count = topic.[3](); if (count > 3) { System.out.println("Many partitions"); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong method name to get partition count.
Using replication factor as partition count.
✗ Incorrect
Create topic with 5 partitions, replication factor 1, and use numPartitions() method to get count.