0
0
Kafkadevops~10 mins

Subscribing to topics 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 subscribe the consumer to a topic named 'my_topic'.

Kafka
consumer.subscribe([1])
Drag options to blanks, or click blank then click option'
A['my_topic']
B'my_topic'
Cmy_topic
D{'my_topic'}
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a string instead of a list
Using curly braces which create a set instead of a list
2fill in blank
medium

Complete the code to subscribe the consumer to multiple topics: 'topic1' and 'topic2'.

Kafka
consumer.subscribe([1])
Drag options to blanks, or click blank then click option'
A('topic1', 'topic2')
B['topic1', 'topic2']
C{'topic1', 'topic2'}
D'topic1, topic2'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a tuple instead of a list
Passing a string with comma-separated topics
3fill in blank
hard

Fix the error in the code to correctly subscribe the consumer to 'events'.

Kafka
consumer.subscribe([1])
Drag options to blanks, or click blank then click option'
A{'events'}
Bevents
C['events']
D'events'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a string instead of a list
Using a set instead of a list
4fill in blank
hard

Fill both blanks to subscribe the consumer to topics starting with 'data_' and print the subscription list.

Kafka
topics = ['data_1', 'data_2', 'logs']
filtered = [topic for topic in topics if topic.[1]('data_')]
consumer.subscribe([2])
print(consumer.subscription())
Drag options to blanks, or click blank then click option'
Astartswith
Bfiltered
Cin
Dtopics
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'in' instead of 'startswith'
Subscribing to the original list instead of filtered
5fill in blank
hard

Fill all three blanks to subscribe the consumer to topics with length greater than 5 and print the subscription.

Kafka
topics = ['alpha', 'beta', 'gamma', 'delta123']
long_topics = [topic for topic in topics if len(topic) [1] 5]
consumer.subscribe([2])
print([3].subscription())
Drag options to blanks, or click blank then click option'
A<
B>
Clong_topics
Dconsumer
Attempts:
3 left
💡 Hint
Common Mistakes
Using less than operator
Printing the list instead of consumer subscription