METADATA 6.8 KB

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