|
| 1 | +import asyncio |
| 2 | +import argparse |
| 3 | +import logging |
1 | 4 | import ydb
|
2 | 5 |
|
3 | 6 |
|
4 |
| -def writer_example(driver: ydb.Driver, topic: str): |
5 |
| - session_pool = ydb.QuerySessionPool(driver) |
| 7 | +async def connect(endpoint: str, database: str) -> ydb.aio.Driver: |
| 8 | + config = ydb.DriverConfig(endpoint=endpoint, database=database) |
| 9 | + config.credentials = ydb.credentials_from_env_variables() |
| 10 | + driver = ydb.aio.Driver(config) |
| 11 | + await driver.wait(5, fail_fast=True) |
| 12 | + return driver |
6 | 13 |
|
7 |
| - def callee(tx: ydb.QueryTxContext): |
8 |
| - tx_writer: ydb.TopicTxWriter = driver.topic_client.tx_writer(tx, topic) # <======= |
9 |
| - # дефолт - без дедупликации, без ретраев и без producer_id. |
10 | 14 |
|
11 |
| - with tx.execute(query="select 1") as result_sets: |
12 |
| - messages = [result_set.rows[0] for result_set in result_sets] |
| 15 | +async def create_topic(driver: ydb.aio.Driver, topic: str, consumer: str): |
| 16 | + try: |
| 17 | + await driver.topic_client.drop_topic(topic) |
| 18 | + except ydb.SchemeError: |
| 19 | + pass |
13 | 20 |
|
14 |
| - tx_writer.write(messages) # вне зависимости от состояния вышестоящего стрима поведение должно быть одинаковое |
| 21 | + await driver.topic_client.create_topic(topic, consumers=[consumer]) |
15 | 22 |
|
16 |
| - session_pool.retry_tx_sync(callee) |
17 | 23 |
|
| 24 | +async def write_with_tx_example(driver: ydb.aio.Driver, topic: str, message_count: int = 10): |
| 25 | + async with ydb.aio.QuerySessionPool(driver) as session_pool: |
18 | 26 |
|
19 |
| -def reader_example(driver: ydb.Driver, reader: ydb.TopicReader): |
20 |
| - session_pool = ydb.QuerySessionPool(driver) |
| 27 | + async def callee(tx: ydb.aio.QueryTxContext): |
| 28 | + print(f"TX ID: {tx.tx_id}") |
| 29 | + print(f"TX STATE: {tx._tx_state._state.value}") |
| 30 | + tx_writer: ydb.TopicTxWriterAsyncIO = driver.topic_client.tx_writer(tx, topic) |
| 31 | + print(f"TX ID: {tx.tx_id}") |
| 32 | + print(f"TX STATE: {tx._tx_state._state.value}") |
| 33 | + for i in range(message_count): |
| 34 | + result_stream = await tx.execute(query=f"select {i} as res") |
| 35 | + messages = [result_set.rows[0]["res"] async for result_set in result_stream] |
21 | 36 |
|
22 |
| - def callee(tx: ydb.QueryTxContext): |
23 |
| - batch = reader.receive_batch_with_tx(tx, max_messages=5) # <======= |
| 37 | + await tx_writer.write([ydb.TopicWriterMessage(data=str(message)) for message in messages]) |
24 | 38 |
|
25 |
| - with tx.execute(query="INSERT INTO max_values(val) VALUES ($val)", parameters={"$val": max(batch)}) as _: |
26 |
| - pass |
| 39 | + print(f"Messages {messages} were written with tx.") |
27 | 40 |
|
28 |
| - session_pool.retry_tx_sync(callee) |
| 41 | + await session_pool.retry_tx_async(callee) |
| 42 | + |
| 43 | + |
| 44 | +async def read_with_tx_example(driver: ydb.aio.Driver, topic: str, consumer: str, message_count: int = 10): |
| 45 | + async with driver.topic_client.reader(topic, consumer) as reader: |
| 46 | + async with ydb.aio.QuerySessionPool(driver) as session_pool: |
| 47 | + for _ in range(message_count): |
| 48 | + |
| 49 | + async def callee(tx: ydb.aio.QueryTxContext): |
| 50 | + batch = await reader.receive_batch_with_tx(tx, max_messages=1) |
| 51 | + print(f"Messages {batch.messages[0].data} were read with tx.") |
| 52 | + |
| 53 | + await session_pool.retry_tx_async(callee) |
| 54 | + |
| 55 | + |
| 56 | +async def main(): |
| 57 | + parser = argparse.ArgumentParser( |
| 58 | + formatter_class=argparse.RawDescriptionHelpFormatter, |
| 59 | + description="""YDB topic basic example.\n""", |
| 60 | + ) |
| 61 | + parser.add_argument("-d", "--database", default="/local", help="Name of the database to use") |
| 62 | + parser.add_argument("-e", "--endpoint", default="grpc://localhost:2136", help="Endpoint url to use") |
| 63 | + parser.add_argument("-p", "--path", default="test-topic", help="Topic name") |
| 64 | + parser.add_argument("-c", "--consumer", default="consumer", help="Consumer name") |
| 65 | + parser.add_argument("-v", "--verbose", default=False, action="store_true") |
| 66 | + parser.add_argument( |
| 67 | + "-s", |
| 68 | + "--skip-drop-and-create-topic", |
| 69 | + default=False, |
| 70 | + action="store_true", |
| 71 | + help="Use existed topic, skip remove it and re-create", |
| 72 | + ) |
| 73 | + |
| 74 | + args = parser.parse_args() |
| 75 | + |
| 76 | + if args.verbose: |
| 77 | + logger = logging.getLogger("topicexample") |
| 78 | + logger.setLevel(logging.DEBUG) |
| 79 | + logger.addHandler(logging.StreamHandler()) |
| 80 | + |
| 81 | + driver = await connect(args.endpoint, args.database) |
| 82 | + if not args.skip_drop_and_create_topic: |
| 83 | + await create_topic(driver, args.path, args.consumer) |
| 84 | + |
| 85 | + await write_with_tx_example(driver, args.path) |
| 86 | + await read_with_tx_example(driver, args.path, args.consumer) |
| 87 | + |
| 88 | + |
| 89 | +if __name__ == "__main__": |
| 90 | + asyncio.run(main()) |
0 commit comments