Skip to content

Commit

Permalink
add read in transaction in russian (#8825)
Browse files Browse the repository at this point in the history
Co-authored-by: Ivan Blinkov <[email protected]>
  • Loading branch information
rekby and blinkov authored Sep 24, 2024
1 parent a6cd079 commit f3bb311
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
40 changes: 40 additions & 0 deletions ydb/docs/en/core/reference/ydb-sdk/topic.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
39 changes: 39 additions & 0 deletions ydb/docs/ru/core/reference/ydb-sdk/topic.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit f3bb311

Please sign in to comment.