Adding Python syntax highlighting to README.md
This commit is contained in:
14
README.md
14
README.md
@@ -14,26 +14,32 @@ Copyright 2012, David Arthur under Apache License, v2.0. See `LICENSE`
|
|||||||
|
|
||||||
You need to specify the topic and partition
|
You need to specify the topic and partition
|
||||||
|
|
||||||
|
```python
|
||||||
kafka = KafkaClient("localhost", 9092)
|
kafka = KafkaClient("localhost", 9092)
|
||||||
kafka.send_messages_simple("my-topic", 0, "some message")
|
kafka.send_messages_simple("my-topic", 0, "some message")
|
||||||
kafka.close()
|
kafka.close()
|
||||||
|
```
|
||||||
|
|
||||||
## Send several messages to a topic
|
## Send several messages to a topic
|
||||||
|
|
||||||
Same as before, just add more arguments to `send_simple`
|
Same as before, just add more arguments to `send_simple`
|
||||||
|
|
||||||
|
```python
|
||||||
kafka = KafkaClient("localhost", 9092)
|
kafka = KafkaClient("localhost", 9092)
|
||||||
kafka.send_messages_simple("my-topic", 0, "some message", "another message", "and another")
|
kafka.send_messages_simple("my-topic", 0, "some message", "another message", "and another")
|
||||||
kafka.close()
|
kafka.close()
|
||||||
|
```
|
||||||
|
|
||||||
## Recieve some messages from a topic
|
## Recieve some messages from a topic
|
||||||
|
|
||||||
Supply `get_message_set` with a `FetchRequest`, get back the messages and new `FetchRequest`
|
Supply `get_message_set` with a `FetchRequest`, get back the messages and new `FetchRequest`
|
||||||
|
|
||||||
|
```python
|
||||||
kafka = KafkaClient("localhost", 9092)
|
kafka = KafkaClient("localhost", 9092)
|
||||||
req = FetchRequest("my-topic", 0, 0, 1024*1024)
|
req = FetchRequest("my-topic", 0, 0, 1024*1024)
|
||||||
(messages, req1) = kafka.get_message_set(req)
|
(messages, req1) = kafka.get_message_set(req)
|
||||||
kafka.close()
|
kafka.close()
|
||||||
|
```
|
||||||
|
|
||||||
The returned `FetchRequest` includes the offset of the next message. This makes
|
The returned `FetchRequest` includes the offset of the next message. This makes
|
||||||
paging through the queue very simple.
|
paging through the queue very simple.
|
||||||
@@ -42,6 +48,7 @@ paging through the queue very simple.
|
|||||||
|
|
||||||
For this we use the `send_multi_message_set` method along with `ProduceRequest` objects.
|
For this we use the `send_multi_message_set` method along with `ProduceRequest` objects.
|
||||||
|
|
||||||
|
```python
|
||||||
kafka = KafkaClient("localhost", 9092)
|
kafka = KafkaClient("localhost", 9092)
|
||||||
req1 = ProduceRequest("my-topic-1", 0, [
|
req1 = ProduceRequest("my-topic-1", 0, [
|
||||||
create_message_from_string("message one"),
|
create_message_from_string("message one"),
|
||||||
@@ -53,23 +60,28 @@ For this we use the `send_multi_message_set` method along with `ProduceRequest`
|
|||||||
])
|
])
|
||||||
kafka.sent_multi_message_set([req1, req1])
|
kafka.sent_multi_message_set([req1, req1])
|
||||||
kafka.close()
|
kafka.close()
|
||||||
|
```
|
||||||
|
|
||||||
## Iterate through all messages from an offset
|
## Iterate through all messages from an offset
|
||||||
|
|
||||||
The `iter_messages` method will make the underlying calls to `get_message_set`
|
The `iter_messages` method will make the underlying calls to `get_message_set`
|
||||||
to provide a generator that returns every message available.
|
to provide a generator that returns every message available.
|
||||||
|
|
||||||
|
```python
|
||||||
kafka = KafkaClient("localhost", 9092)
|
kafka = KafkaClient("localhost", 9092)
|
||||||
for msg in kafka.iter_messages(FetchRequest("my-topic", 0, 0, 1024*1024)):
|
for msg in kafka.iter_messages(FetchRequest("my-topic", 0, 0, 1024*1024)):
|
||||||
print(msg.payload)
|
print(msg.payload)
|
||||||
kafka.close()
|
kafka.close()
|
||||||
|
```
|
||||||
|
|
||||||
An optional `auto` argument will control auto-paging through results
|
An optional `auto` argument will control auto-paging through results
|
||||||
|
|
||||||
|
```python
|
||||||
kafka = KafkaClient("localhost", 9092)
|
kafka = KafkaClient("localhost", 9092)
|
||||||
for msg in kafka.iter_messages(FetchRequest("my-topic", 0, 0, 1024*1024), False):
|
for msg in kafka.iter_messages(FetchRequest("my-topic", 0, 0, 1024*1024), False):
|
||||||
print(msg.payload)
|
print(msg.payload)
|
||||||
kafka.close()
|
kafka.close()
|
||||||
|
```
|
||||||
|
|
||||||
This will only iterate through messages in the first byte range of
|
This will only iterate through messages in the byte range of
|
||||||
(0, 1024\*1024)
|
(0, 1024\*1024)
|
||||||
|
|||||||
Reference in New Issue
Block a user