|
| 1 | +import ydb |
| 2 | + |
| 3 | + |
| 4 | +def writer_example(driver: ydb.Driver, topic: str): |
| 5 | + session_pool = ydb.QuerySessionPool(driver) |
| 6 | + |
| 7 | + def callee(session: ydb.QuerySession): |
| 8 | + tx = session.transaction() |
| 9 | + tx.begin() |
| 10 | + |
| 11 | + tx_writer: ydb.TopicTxWriter = driver.topic_client.tx_writer(topic, tx=tx) |
| 12 | + # tx_writer: ydb.TopicWriter = driver.topic_client.writer(topic) |
| 13 | + |
| 14 | + def prepare_messages(result_sets): |
| 15 | + # some messages based on query result |
| 16 | + return ["1"] |
| 17 | + |
| 18 | + with tx.execute(query="select 1") as result_sets: |
| 19 | + messages = prepare_messages(result_sets) |
| 20 | + |
| 21 | + tx_writer.write(messages) # wait only on commit |
| 22 | + # tx_writer.flush() -> maybe |
| 23 | + |
| 24 | + tx.commit() # <---------------------- |
| 25 | + |
| 26 | + session_pool.retry_operation_sync(callee) |
| 27 | + |
| 28 | + |
| 29 | +# ============================================================================= |
| 30 | + |
| 31 | + |
| 32 | +def reader_example(driver: ydb.Driver, reader: ydb.TopicReader): |
| 33 | + session_pool = ydb.QuerySessionPool(driver) |
| 34 | + |
| 35 | + def callee(session: ydb.QuerySession): |
| 36 | + tx = session.transaction().begin() |
| 37 | + |
| 38 | + batch = reader.receive_batch_with_tx(tx=tx) |
| 39 | + # this batch will be commited on tx commit |
| 40 | + # important to commit in correct order |
| 41 | + |
| 42 | + def prepare_query(batch): |
| 43 | + # some query based on batch |
| 44 | + return "select 1" |
| 45 | + |
| 46 | + with tx.execute( |
| 47 | + query=prepare_query(batch), |
| 48 | + commit_tx=True, |
| 49 | + ) as result_sets: |
| 50 | + # handle results |
| 51 | + pass |
| 52 | + |
| 53 | + session_pool.retry_operation_sync(callee) |
| 54 | + |
| 55 | +# ============================================================================= |
| 56 | + |
| 57 | +def writer_example_update(driver: ydb.Driver, topic: str): |
| 58 | + session_pool = ydb.QuerySessionPool(driver) |
| 59 | + |
| 60 | + def callee(tx: ydb.QueryTxContext): |
| 61 | + tx_writer: ydb.TopicWriter = driver.topic_client.tx_writer(topic, tx=tx) |
| 62 | + |
| 63 | + def prepare_messages(result_sets): |
| 64 | + # some messages based on query result |
| 65 | + return ["1"] |
| 66 | + |
| 67 | + with tx.execute(query="select 1") as result_sets: |
| 68 | + messages = prepare_messages(result_sets) |
| 69 | + |
| 70 | + tx_writer.write(messages) |
| 71 | + |
| 72 | + session_pool.retry_operation_tx(callee) |
| 73 | + |
| 74 | + |
| 75 | +# ============================================================================= |
| 76 | + |
| 77 | + |
| 78 | +def reader_example_update(driver: ydb.Driver, reader: ydb.TopicReader): |
| 79 | + session_pool = ydb.QuerySessionPool(driver) |
| 80 | + |
| 81 | + def callee(tx: ydb.QueryTxContext): |
| 82 | + batch = reader.receive_batch_with_tx(tx=tx) |
| 83 | + # this batch will be commited on tx commit |
| 84 | + |
| 85 | + def prepare_query(batch): |
| 86 | + # some query based on patch |
| 87 | + return "select 1" |
| 88 | + |
| 89 | + with tx.execute( |
| 90 | + query=prepare_query(batch) |
| 91 | + ) as result_sets: |
| 92 | + # handle results |
| 93 | + pass |
| 94 | + |
| 95 | + session_pool.retry_operation_tx(callee) |
0 commit comments