Complete the code to specify the partition key for a Kafka message.
producer.send(topic, key=[1], value="message")
The partition key is usually a string or bytes that Kafka uses to decide the partition. Here, a string key "user123" is correct.
Complete the code to create a custom partitioner class in Kafka.
class CustomPartitioner: def partition(self, key, partitions, [1]): return hash(key) % len(partitions)
The partition method usually receives key, partitions, and metadata. Metadata provides extra info about the message.
Fix the error in the partitioner code to correctly compute the partition index.
def partition(key, partitions): index = hash(key) [1] len(partitions) return index
The modulo operator % is used to ensure the partition index is within the range of available partitions.
Fill both blanks to create a dictionary comprehension that maps partitions to message counts greater than 10.
partition_counts = {p: counts[p] [1] p [2] counts if counts[p] > 10}The syntax for dictionary comprehension is: {key: value for key in iterable if condition}. Here, 'for' and 'in' are needed.
Fill all three blanks to create a dictionary comprehension that maps uppercase keys to values greater than 5.
filtered = [1]: [2] for [3] in data if data[[3]] > 5
The comprehension maps uppercase keys (k.upper()) to their values (data[k]) for keys k in data where value > 5.