DESCRIPTION.rst 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. Kafka Python client
  2. ------------------------
  3. .. image:: https://img.shields.io/badge/kafka-0.11%2C%200.10%2C%200.9%2C%200.8-brightgreen.svg
  4. :target: https://kafka-python.readthedocs.io/compatibility.html
  5. .. image:: https://img.shields.io/pypi/pyversions/kafka.svg
  6. :target: https://pypi.python.org/pypi/kafka
  7. .. image:: https://coveralls.io/repos/dpkp/kafka-python/badge.svg?branch=master&service=github
  8. :target: https://coveralls.io/github/dpkp/kafka-python?branch=master
  9. .. image:: https://travis-ci.org/dpkp/kafka-python.svg?branch=master
  10. :target: https://travis-ci.org/dpkp/kafka-python
  11. .. image:: https://img.shields.io/badge/license-Apache%202-blue.svg
  12. :target: https://github.com/dpkp/kafka-python/blob/master/LICENSE
  13. Python client for the Apache Kafka distributed stream processing system.
  14. kafka-python is designed to function much like the official java client, with a
  15. sprinkling of pythonic interfaces (e.g., consumer iterators).
  16. kafka-python is best used with newer brokers (0.9+), but is backwards-compatible with
  17. older versions (to 0.8.0). Some features will only be enabled on newer brokers.
  18. For example, fully coordinated consumer groups -- i.e., dynamic partition
  19. assignment to multiple consumers in the same group -- requires use of 0.9+ kafka
  20. brokers. Supporting this feature for earlier broker releases would require
  21. writing and maintaining custom leadership election and membership / health
  22. check code (perhaps using zookeeper or consul). For older brokers, you can
  23. achieve something similar by manually assigning different partitions to each
  24. consumer instance with config management tools like chef, ansible, etc. This
  25. approach will work fine, though it does not support rebalancing on failures.
  26. See <https://kafka-python.readthedocs.io/en/master/compatibility.html>
  27. for more details.
  28. Please note that the master branch may contain unreleased features. For release
  29. documentation, please see readthedocs and/or python's inline help.
  30. >>> pip install kafka
  31. KafkaConsumer
  32. *************
  33. KafkaConsumer is a high-level message consumer, intended to operate as similarly
  34. as possible to the official java client. Full support for coordinated
  35. consumer groups requires use of kafka brokers that support the Group APIs: kafka v0.9+.
  36. See <https://kafka-python.readthedocs.io/en/master/apidoc/KafkaConsumer.html>
  37. for API and configuration details.
  38. The consumer iterator returns ConsumerRecords, which are simple namedtuples
  39. that expose basic message attributes: topic, partition, offset, key, and value:
  40. >>> from kafka import KafkaConsumer
  41. >>> consumer = KafkaConsumer('my_favorite_topic')
  42. >>> for msg in consumer:
  43. ... print (msg)
  44. >>> # join a consumer group for dynamic partition assignment and offset commits
  45. >>> from kafka import KafkaConsumer
  46. >>> consumer = KafkaConsumer('my_favorite_topic', group_id='my_favorite_group')
  47. >>> for msg in consumer:
  48. ... print (msg)
  49. >>> # manually assign the partition list for the consumer
  50. >>> from kafka import TopicPartition
  51. >>> consumer = KafkaConsumer(bootstrap_servers='localhost:1234')
  52. >>> consumer.assign([TopicPartition('foobar', 2)])
  53. >>> msg = next(consumer)
  54. >>> # Deserialize msgpack-encoded values
  55. >>> consumer = KafkaConsumer(value_deserializer=msgpack.loads)
  56. >>> consumer.subscribe(['msgpackfoo'])
  57. >>> for msg in consumer:
  58. ... assert isinstance(msg.value, dict)
  59. KafkaProducer
  60. *************
  61. KafkaProducer is a high-level, asynchronous message producer. The class is
  62. intended to operate as similarly as possible to the official java client.
  63. See <https://kafka-python.readthedocs.io/en/master/apidoc/KafkaProducer.html>
  64. for more details.
  65. >>> from kafka import KafkaProducer
  66. >>> producer = KafkaProducer(bootstrap_servers='localhost:1234')
  67. >>> for _ in range(100):
  68. ... producer.send('foobar', b'some_message_bytes')
  69. >>> # Block until a single message is sent (or timeout)
  70. >>> future = producer.send('foobar', b'another_message')
  71. >>> result = future.get(timeout=60)
  72. >>> # Block until all pending messages are at least put on the network
  73. >>> # NOTE: This does not guarantee delivery or success! It is really
  74. >>> # only useful if you configure internal batching using linger_ms
  75. >>> producer.flush()
  76. >>> # Use a key for hashed-partitioning
  77. >>> producer.send('foobar', key=b'foo', value=b'bar')
  78. >>> # Serialize json messages
  79. >>> import json
  80. >>> producer = KafkaProducer(value_serializer=lambda v: json.dumps(v).encode('utf-8'))
  81. >>> producer.send('fizzbuzz', {'foo': 'bar'})
  82. >>> # Serialize string keys
  83. >>> producer = KafkaProducer(key_serializer=str.encode)
  84. >>> producer.send('flipflap', key='ping', value=b'1234')
  85. >>> # Compress messages
  86. >>> producer = KafkaProducer(compression_type='gzip')
  87. >>> for i in range(1000):
  88. ... producer.send('foobar', b'msg %d' % i)
  89. Thread safety
  90. *************
  91. The KafkaProducer can be used across threads without issue, unlike the
  92. KafkaConsumer which cannot.
  93. While it is possible to use the KafkaConsumer in a thread-local manner,
  94. multiprocessing is recommended.
  95. Compression
  96. ***********
  97. kafka-python supports gzip compression/decompression natively. To produce or consume lz4
  98. compressed messages, you should install python-lz4 (pip install lz4).
  99. To enable snappy compression/decompression install python-snappy (also requires snappy library).
  100. See <https://kafka-python.readthedocs.io/en/master/install.html#optional-snappy-install>
  101. for more information.
  102. Protocol
  103. ********
  104. A secondary goal of kafka-python is to provide an easy-to-use protocol layer
  105. for interacting with kafka brokers via the python repl. This is useful for
  106. testing, probing, and general experimentation. The protocol support is
  107. leveraged to enable a KafkaClient.check_version() method that
  108. probes a kafka broker and attempts to identify which version it is running
  109. (0.8.0 to 0.11).
  110. Low-level
  111. *********
  112. Legacy support is maintained for low-level consumer and producer classes,
  113. SimpleConsumer and SimpleProducer. See
  114. <https://kafka-python.readthedocs.io/en/master/simple.html?highlight=SimpleProducer> for API details.