Skip to content

Commit 70f95e1

Browse files
committed
Python SDK topic reader without consumer docs
1 parent 79d13dd commit 70f95e1

File tree

2 files changed

+59
-5
lines changed

2 files changed

+59
-5
lines changed

ydb/docs/en/core/reference/ydb-sdk/topic.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,7 +1184,8 @@ All the metadata provided when writing a message is sent to a consumer with the
11841184

11851185
### Connecting to a topic for message reads {#start-reader}
11861186

1187-
To be able to read messages from topic, a Consumer on this topic should exist.
1187+
Reading messages from a topic can be done by specifying a Consumer associated with that topic, as well as without a Consumer. If a Consumer is not specified, the client application must calculate the offset for reading messages on its own. A more detailed example of reading without a Consumer is discussed in the [relevant section](#no-consumer).
1188+
11881189
A Consumer can be created on [creating](#create-topic) or [altering](#alter-topic) a topic.
11891190
Topic can have several Consumers and for each of them server stores its own reading progress.
11901191

@@ -1222,7 +1223,7 @@ Topic can have several Consumers and for each of them server stores its own read
12221223
To establish a connection to the existing `my-topic` topic using the added `my-consumer` consumer, use the following code:
12231224

12241225
```python
1225-
reader = driver.topic_client.reader(topic="topic-path", consumer="consumer_name")
1226+
reader = driver.topic_client.reader(topic="my-topic", consumer="my-consumer")
12261227
```
12271228

12281229
- Java (sync)
@@ -1928,6 +1929,32 @@ Reading progress is usually saved on a server for each Consumer. However, such p
19281929
}
19291930
```
19301931

1932+
- Python
1933+
1934+
To read without a `Consumer`, create a reader using the `reader` method with specifying these arguments:
1935+
* `topic` - `ydb.TopicReaderSelector` object with defined `path` and `partitions` list;
1936+
* `consumer` - should be `None`;
1937+
* `event_handler` - inheritor of `ydb.TopicReaderEvents.EventHandler` that implements the `on_partition_get_start_offset` function. This function is responsible for returning the initial offset for reading messages when the reader starts and during reconnections. The client application must specify this offset in the parameter `ydb.TopicReaderEvents.OnPartitionGetStartOffsetResponse.start_offset`. The function can also be implemented as asynchronous.
1938+
1939+
Example:
1940+
1941+
```python
1942+
class CustomEventHandler(ydb.TopicReaderEvents.EventHandler):
1943+
def on_partition_get_start_offset(self, event: ydb.TopicReaderEvents.OnPartitionGetStartOffsetRequest):
1944+
return ydb.TopicReaderEvents.OnPartitionGetStartOffsetResponse(
1945+
start_offset=0,
1946+
)
1947+
1948+
reader = driver.topic_client.reader(
1949+
topic=ydb.TopicReaderSelector(
1950+
path="topic-path",
1951+
partitions=[0, 1, 2],
1952+
),
1953+
consumer=None,
1954+
event_handler=CustomEventHandler(),
1955+
)
1956+
```
1957+
19311958
{% endlist %}
19321959

19331960
### Reading in a transaction {#read-tx}

ydb/docs/ru/core/reference/ydb-sdk/topic.md

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,7 +1182,8 @@
11821182

11831183
### Подключение к топику для чтения сообщений {#start-reader}
11841184

1185-
Для чтения сообщений из топика необходимо наличие заранее созданного Consumer, связанного с этим топиком.
1185+
Чтение сообщений из топика может выполнятся с указанием Consumer'а, связанного с этим топиком, а также без Consumer'а. Если Consumer не указан, то клиентское приложение должно самостоятельно рассчитывать offset для чтения сообщений. Более подробно пример чтения без Consumer'а рассмотрен в [соответствующей секции](#no-consumer).
1186+
11861187
Создать Consumer можно при [создании](#create-topic) или [изменении](#alter-topic) топика.
11871188
У топика может быть несколько Consumer'ов и для каждого из них сервер хранит свой прогресс чтения.
11881189

@@ -1220,7 +1221,7 @@
12201221
Чтобы создать подключение к существующему топику `my-topic` через добавленного ранее читателя `my-consumer`, используйте следующий код:
12211222

12221223
```python
1223-
reader = driver.topic_client.reader(topic="topic-path", consumer="consumer_name")
1224+
reader = driver.topic_client.reader(topic="my-topic", consumer="my-consumer")
12241225
```
12251226

12261227
- Java (sync)
@@ -1909,7 +1910,7 @@
19091910

19101911
- Java
19111912

1912-
Для чтения без `Consumer`а следует в настройках читателя `ReaderSettings` это явно указать, вызвав `withoutConsumer()`:
1913+
Для чтения без Consumer'а следует в настройках читателя `ReaderSettings` это явно указать, вызвав `withoutConsumer()`:
19131914
19141915
```java
19151916
ReaderSettings settings = ReaderSettings.newBuilder()
@@ -1931,6 +1932,32 @@
19311932
}
19321933
```
19331934
1935+
- Python
1936+
1937+
Для чтения без Consumer'а следует создать читателя с помощью метода `reader` с указанием следующих аргументов:
1938+
* `topic` - объект `ydb.TopicReaderSelector` с указанными `path` и списком `partitions`;
1939+
* `consumer` - должен быть `None`;
1940+
* `event_handler` - наследник `ydb.TopicReaderEvents.EventHandler`, который реализует функцию `on_partition_get_start_offset`. Эта функция отвечает за возвращение начального смещения (offset) для чтения сообщений при старте читателя, а также во время переподключений. Клиентское приложение должно указать это смещение в параметре `ydb.TopicReaderEvents.OnPartitionGetStartOffsetResponse.start_offset`. Также функция может быть реализована как асинхронная.
1941+
1942+
Пример:
1943+
1944+
```python
1945+
class CustomEventHandler(ydb.TopicReaderEvents.EventHandler):
1946+
def on_partition_get_start_offset(self, event: ydb.TopicReaderEvents.OnPartitionGetStartOffsetRequest):
1947+
return ydb.TopicReaderEvents.OnPartitionGetStartOffsetResponse(
1948+
start_offset=0,
1949+
)
1950+
1951+
reader = driver.topic_client.reader(
1952+
topic=ydb.TopicReaderSelector(
1953+
path="topic-path",
1954+
partitions=[0, 1, 2],
1955+
),
1956+
consumer=None,
1957+
event_handler=CustomEventHandler(),
1958+
)
1959+
```
1960+
19341961
{% endlist %}
19351962

19361963
### Чтение в транзакции {#read-tx}

0 commit comments

Comments
 (0)