From f3bb311b382b307918adba2f34c600b6e21ca361 Mon Sep 17 00:00:00 2001 From: Timofey Koolin Date: Tue, 24 Sep 2024 08:48:26 +0300 Subject: [PATCH] add read in transaction in russian (#8825) Co-authored-by: Ivan Blinkov --- ydb/docs/en/core/reference/ydb-sdk/topic.md | 40 +++++++++++++++++++++ ydb/docs/ru/core/reference/ydb-sdk/topic.md | 39 ++++++++++++++++++++ 2 files changed, 79 insertions(+) diff --git a/ydb/docs/en/core/reference/ydb-sdk/topic.md b/ydb/docs/en/core/reference/ydb-sdk/topic.md index df11d3363117..d44078c7fb10 100644 --- a/ydb/docs/en/core/reference/ydb-sdk/topic.md +++ b/ydb/docs/en/core/reference/ydb-sdk/topic.md @@ -849,6 +849,23 @@ All the metadata provided when writing a message is sent to a consumer with the {% list tabs %} +- Go + + To write to a topic within a transaction, create a transactional writer by calling [TopicClient.StartTransactionalWriter](https://pkg.go.dev/github.com/ydb-platform/ydb-go-sdk/v3/topic#Client.StartTransactionalWriter) 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. + + [Example on GitHub](https://github.com/ydb-platform/ydb-go-sdk/blob/master/examples/topic/topicwriter/topic_writer_transaction.go) + + ```go + err := db.Query().DoTx(ctx, func(ctx context.Context, tx query.TxActor) error { + writer, err := db.Topic().StartTransactionalWriter(tx, topicName) + if err != nil { + return err + } + + return writer.Write(ctx, topicwriter.Message{Data: strings.NewReader("asd")}) + }) + ``` + - Java (sync) [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) @@ -1656,6 +1673,29 @@ Reading progress is usually saved on a server for each Consumer. However, such p } ``` +- Go + + To read messages from a topic within a transaction, use the [Reader.PopMessagesBatchTx](https://pkg.go.dev/github.com/ydb-platform/ydb-go-sdk/v3/topic/topicreader#Reader.PopMessagesBatchTx) 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 important 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. The simplest way to ensure this is by using the reader within a loop. + + + [Example on GitHub](https://github.com/ydb-platform/ydb-go-sdk/blob/master/examples/topic/topicreader/topic_reader_transaction.go) + + ```go + for { + err := db.Query().DoTx(ctx, func(ctx context.Context, tx query.TxActor) error { + batch, err := reader.PopMessagesBatchTx(ctx, tx) // the batch will be committed along with the transaction + if err != nil { + return err + } + + return processBatch(ctx, batch) + }) + if err != nil { + handleError(err) + } + } + ``` + - Java (sync) [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) diff --git a/ydb/docs/ru/core/reference/ydb-sdk/topic.md b/ydb/docs/ru/core/reference/ydb-sdk/topic.md index cf18e553bdf1..8b07d9866d56 100644 --- a/ydb/docs/ru/core/reference/ydb-sdk/topic.md +++ b/ydb/docs/ru/core/reference/ydb-sdk/topic.md @@ -859,6 +859,23 @@ transaction.Commit().GetValueSync(); ``` +- Go + + Для записи в топик в транзакции необходимо создать транзакционного писателя через вызов [TopicClient.StartTransactionalWriter](https://pkg.go.dev/github.com/ydb-platform/ydb-go-sdk/v3/topic#Client.StartTransactionalWriter). После этого можно отправлять сообщения, как обычно. Закрывать транзакционного писателя не требуется — это происходит автоматически при завершении транзакции. + + [Пример на GitHub](https://github.com/ydb-platform/ydb-go-sdk/blob/master/examples/topic/topicwriter/topic_writer_transaction.go) + + ```go + err := db.Query().DoTx(ctx, func(ctx context.Context, tx query.TxActor) error { + writer, err := db.Topic().StartTransactionalWriter(tx, topicName) + if err != nil { + return err + } + + return writer.Write(ctx, topicwriter.Message{Data: strings.NewReader("asd")}) + }) + ``` + - Java (sync) [Пример на GitHub](https://github.com/ydb-platform/ydb-java-examples/blob/develop/ydb-cookbook/src/main/java/tech/ydb/examples/topic/transactions/TransactionWriteSync.java) @@ -1670,6 +1687,28 @@ } ``` +- Go + + Для чтения сообщений в рамках транзакции следует использовать метод [`Reader.PopMessagesBatchTx`](https://pkg.go.dev/github.com/ydb-platform/ydb-go-sdk/v3/topic/topicreader#Reader.PopMessagesBatchTx). Он прочитает пакет сообщений и добавит их коммит в транзакцию, при этом отдельно коммитить эти сообщения не требуется. Читателя сообщений можно использовать повторно в разных транзакциях. При этом важно, чтобы порядок коммита транзакций соответствовал порядку получения сообщений от читателя, так как коммиты сообщений в топике должны выполняться строго по порядку. Проще всего это сделать если использовать читателя в цикле. + + [Пример на GitHub](https://github.com/ydb-platform/ydb-go-sdk/blob/master/examples/topic/topicreader/topic_reader_transaction.go) + + ```go + for { + err := db.Query().DoTx(ctx, func(ctx context.Context, tx query.TxActor) error { + batch, err := reader.PopMessagesBatchTx(ctx, tx) // батч закоммитится при общем коммите транзакции + if err != nil { + return err + } + + return processBatch(ctx, batch) + }) + if err != nil { + handleError(err) + } + } + ``` + - Java (sync) [Пример на GitHub](https://github.com/ydb-platform/ydb-java-examples/blob/develop/ydb-cookbook/src/main/java/tech/ydb/examples/topic/transactions/TransactionReadSync.java)