You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -205,7 +205,7 @@ The topic path is mandatory. Other parameters are optional.
205
205
.build())
206
206
.build());
207
207
```
208
-
208
+
209
209
- С#
210
210
211
211
Example of creating a topic with a list of supported codecs and a minimum number of partitions:
@@ -788,7 +788,7 @@ Only connections with matching [producer and message group](../../concepts/topic
788
788
```c#
789
789
var writeCts = new CancellationTokenSource();
790
790
writeCts.CancelAfter(TimeSpan.FromSeconds(3));
791
-
791
+
792
792
await writer.WriteAsync("Hello, Example YDB Topics!", writeCts.Token);
793
793
```
794
794
@@ -1005,6 +1005,54 @@ All the metadata provided when writing a message is sent to a consumer with the
1005
1005
})
1006
1006
```
1007
1007
1008
+
- Python
1009
+
1010
+
To write to a topic within a transaction, create a transactional writer by calling `topic_client.tx_writer`with the `tx` argument. Once created, you can send messages as usual. There's no need to close the transactional writer manually, as it will be closed automatically when the transaction ends.
1011
+
1012
+
In the example below, there is no explicit call to `tx.commit()`; it occurs implicitly upon the successful execution of the `callee`lambda.
1013
+
1014
+
[Example on GitHub](https://github.com/ydb-platform/ydb-python-sdk/blob/main/examples/topic/topic_transactions_example.py)
1015
+
1016
+
```python
1017
+
with ydb.QuerySessionPool(driver) as session_pool:
result_stream= tx.execute(query=f"select {i} as res;")
1024
+
for result_set in result_stream:
1025
+
message=str(result_set.rows[0]["res"])
1026
+
tx_writer.write(ydb.TopicWriterMessage(message))
1027
+
print(f"Message {message} was written with tx.")
1028
+
1029
+
session_pool.retry_tx_sync(callee)
1030
+
```
1031
+
1032
+
- Python (asyncio)
1033
+
1034
+
To write to a topic within a transaction, create a transactional writer by calling `topic_client.tx_writer`with the `tx` argument. Once created, you can send messages as usual. There's no need to close the transactional writer manually, as it will be closed automatically when the transaction ends.
1035
+
1036
+
In the example below, there is no explicit call to `tx.commit()`; it occurs implicitly upon the successful execution of the `callee`lambda.
1037
+
1038
+
[Example on GitHub](https://github.com/ydb-platform/ydb-python-sdk/blob/main/examples/topic/topic_transactions_async_example.py)
1039
+
1040
+
```python
1041
+
asyncwith ydb.aio.QuerySessionPool(driver) as session_pool:
print(f"Message {result_set.rows[0]['res']} was written with tx.")
1052
+
1053
+
await session_pool.retry_tx_async(callee)
1054
+
```
1055
+
1008
1056
- Java (sync)
1009
1057
1010
1058
[Example on GitHub](https://github.com/ydb-platform/ydb-java-examples/blob/develop/ydb-cookbook/src/main/java/tech/ydb/examples/topic/transactions/TransactionWriteSync.java)
@@ -1285,7 +1333,7 @@ Topic can have several Consumers and for each of them server stores its own read
1285
1333
{
1286
1334
ConsumerName = "Consumer_Example",
1287
1335
SubscribeSettings = { new SubscribeSettings(topicName) }
1288
-
}.Build();
1336
+
}.Build();
1289
1337
```
1290
1338
1291
1339
{% endlist %}
@@ -1360,7 +1408,7 @@ To establish a connection to the `my-topic` and `my-specific-topic` topics using
1360
1408
}
1361
1409
}.Build();
1362
1410
```
1363
-
1411
+
1364
1412
{% endlist %}
1365
1413
1366
1414
### Reading messages {#reading-messages}
@@ -1465,7 +1513,7 @@ Data from topics can be read in the context of [transactions](#read-tx). In this
1465
1513
{
1466
1514
}
1467
1515
```
1468
-
1516
+
1469
1517
{% endlist %}
1470
1518
1471
1519
#### Reading message batches
@@ -1544,7 +1592,7 @@ Data from topics can be read in the context of [transactions](#read-tx). In this
@@ -1963,6 +2011,42 @@ Reading progress is usually saved on a server for each Consumer. However, such p
1963
2011
}
1964
2012
```
1965
2013
2014
+
- Python
2015
+
2016
+
To read messages from a topic within a transaction, use the `reader.receive_batch_with_tx` method. It reads a batch of messages and adds their commit to the transaction, so there's no need to commit them separately. The reader can be reused across different transactions. However, it's essential to commit transactions in the same order as the messages are read from the reader, as message commits in the topic must be performed strictly in order - otherwise transaction will get an error during commit. The simplest way to ensure this is by using the reader within a loop.
2017
+
2018
+
[Example on GitHub](https://github.com/ydb-platform/ydb-python-sdk/blob/main/examples/topic/topic_transactions_example.py)
2019
+
2020
+
```python
2021
+
with driver.topic_client.reader(topic, consumer) as reader:
2022
+
with ydb.QuerySessionPool(driver) as session_pool:
print(f"Message {batch.messages[0].data.decode()} was read with tx.")
2028
+
2029
+
session_pool.retry_tx_sync(callee)
2030
+
```
2031
+
2032
+
- Python (asyncio)
2033
+
2034
+
To read messages from a topic within a transaction, use the `reader.receive_batch_with_tx` method. It reads a batch of messages and adds their commit to the transaction, so there's no need to commit them separately. The reader can be reused across different transactions. However, it's essential to commit transactions in the same order as the messages are read from the reader, as message commits in the topic must be performed strictly in order - otherwise transaction will get an error during commit. The simplest way to ensure this is by using the reader within a loop.
2035
+
2036
+
[Example on GitHub](https://github.com/ydb-platform/ydb-python-sdk/blob/main/examples/topic/topic_transactions_async_example.py)
2037
+
2038
+
```python
2039
+
asyncwith driver.topic_client.reader(topic, consumer) as reader:
2040
+
asyncwith ydb.aio.QuerySessionPool(driver) as session_pool:
print(f"Message {batch.messages[0].data.decode()} was read with tx.")
2046
+
2047
+
await session_pool.retry_tx_async(callee)
2048
+
```
2049
+
1966
2050
- Java (sync)
1967
2051
1968
2052
[Example on GitHub](https://github.com/ydb-platform/ydb-java-examples/blob/develop/ydb-cookbook/src/main/java/tech/ydb/examples/topic/transactions/TransactionReadSync.java)
0 commit comments