@@ -50,7 +50,34 @@ for examples.
50
50
KafkaProducer
51
51
*************
52
52
53
- <`in progress - see SimpleProducer for legacy producer implementation `>
53
+ KafkaProducer is a high-level, asynchronous message producer. The class is
54
+ intended to operate as similarly as possible to the official java client.
55
+ See `ReadTheDocs <http://kafka-python.readthedocs.org/en/master/apidoc/KafkaProducer.html >`_
56
+ for more details.
57
+
58
+ >>> from kafka import KafkaProducer
59
+ >>> producer = KafkaProducer(bootstrap_servers = ' localhost:1234' )
60
+ >>> producer.send(' foobar' , b ' some_message_bytes' )
61
+
62
+ >>> # Blocking send
63
+ >>> producer.send(' foobar' , b ' another_message' ).get(timeout = 60 )
64
+
65
+ >>> # Use a key for hashed-partitioning
66
+ >>> producer.send(' foobar' , key = b ' foo' , value = b ' bar' )
67
+
68
+ >>> # Serialize json messages
69
+ >>> import json
70
+ >>> producer = KafkaProducer(value_serializer = json.loads)
71
+ >>> producer.send(' fizzbuzz' , {' foo' : ' bar' })
72
+
73
+ >>> # Serialize string keys
74
+ >>> producer = KafkaProducer(key_serializer = str .encode)
75
+ >>> producer.send(' flipflap' , key = ' ping' , value = b ' 1234' )
76
+
77
+ >>> # Compress messages
78
+ >>> producer = KafkaProducer(compression_type = ' gzip' )
79
+ >>> for i in range (1000 ):
80
+ ... producer.send(' foobar' , b ' msg %d ' % i)
54
81
55
82
56
83
Protocol
0 commit comments