Bird
Raised Fist0

Identify the error in this Python confluent-kafka producer code:

medium📝 Debug Q14 of Q15
Kafka - with Java/Python
Identify the error in this Python confluent-kafka producer code:
from confluent_kafka import Producer

p = Producer({'bootstrap.servers': 'localhost:9092'})
p.produce('my_topic', key='key1', 'hello')
p.flush()
AMissing key argument in produce()
BIncorrect order of arguments in produce()
Cflush() called before produce()
DMissing import statement
Step-by-Step Solution
Solution:
  1. Step 1: Check produce() argument order

    The produce() method expects positional arguments first (topic, optionally value), followed only by keyword arguments. Here, key='key1' (keyword) is followed by 'hello' (positional), causing SyntaxError: positional argument follows keyword argument.
  2. Step 2: Identify correct usage

    Correct usage: produce('my_topic', value='hello') or produce('my_topic', key='key1', value='hello').
  3. Final Answer:

    Incorrect order of arguments in produce() -> Option B
  4. Quick Check:

    Positional args before keywords [OK]
Quick Trick: Positional args must precede keywords [OK]
Common Mistakes:
MISTAKES
  • Placing positional argument after keyword argument
  • Mixing positional and keyword argument order
  • Forgetting Python positional-before-keyword rule

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kafka Quizzes