Skip to content

Commit d23db84

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

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

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

+26
Original file line numberDiff line numberDiff line change
@@ -1928,6 +1928,32 @@ Reading progress is usually saved on a server for each Consumer. However, such p
19281928
}
19291929
```
19301930

1931+
- Python
1932+
1933+
To read without a `Consumer`, create a reader using the `reader` method with specifying these arguments:
1934+
* `topic` - `ydb.TopicReaderSelector` object with defined `path` and `partitions` list;
1935+
* `consumer` - should be `None`;
1936+
* `event_handler` - `ydb.TopicReaderEvents.EventHandler` inheritor with defined `on_partition_get_start_offset` method. This method will calculate the starting offset when the reader starts, as well as when it reconnects. It can be asynchronous.
1937+
1938+
Full example:
1939+
1940+
```python
1941+
class CustomEventHandler(ydb.TopicReaderEvents.EventHandler):
1942+
def on_partition_get_start_offset(self, event: ydb.TopicReaderEvents.OnPartitionGetStartOffsetRequest):
1943+
return ydb.TopicReaderEvents.OnPartitionGetStartOffsetResponse(
1944+
start_offset=0,
1945+
)
1946+
1947+
reader = driver.topic_client.reader(
1948+
topic=ydb.TopicReaderSelector(
1949+
path="topic-path",
1950+
partitions=[0, 1, 2],
1951+
),
1952+
consumer=None,
1953+
event_handler=CustomEventHandler(),
1954+
)
1955+
```
1956+
19311957
{% endlist %}
19321958

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

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

+26
Original file line numberDiff line numberDiff line change
@@ -1931,6 +1931,32 @@
19311931
}
19321932
```
19331933
1934+
- Python
1935+
1936+
Для чтения без `Consumer`а следует создать читателя с помощью метода `reader` с указанием следующих аргументов:
1937+
* `topic` - объект `ydb.TopicReaderSelector` с указанными `path` и списком `partitions`;
1938+
* `consumer` - должен быть `None`;
1939+
* `event_handler` - наследник `ydb.TopicReaderEvents.EventHandler` с определенной функцией `on_partition_get_start_offset`. Благодаря этой функции будет высчитываться стартовый offset при старте читателя, а так же при его переподключениях. Может быть асинхронной.
1940+
1941+
Полный пример:
1942+
1943+
```python
1944+
class CustomEventHandler(ydb.TopicReaderEvents.EventHandler):
1945+
def on_partition_get_start_offset(self, event: ydb.TopicReaderEvents.OnPartitionGetStartOffsetRequest):
1946+
return ydb.TopicReaderEvents.OnPartitionGetStartOffsetResponse(
1947+
start_offset=0,
1948+
)
1949+
1950+
reader = driver.topic_client.reader(
1951+
topic=ydb.TopicReaderSelector(
1952+
path="topic-path",
1953+
partitions=[0, 1, 2],
1954+
),
1955+
consumer=None,
1956+
event_handler=CustomEventHandler(),
1957+
)
1958+
```
1959+
19341960
{% endlist %}
19351961
19361962
### Чтение в транзакции {#read-tx}

0 commit comments

Comments
 (0)