/
githubmirror
/
ydb-go-sdk
Обзор
Документация
Войти
/
githubmirror
/
ydb-go-sdk
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
examples/topic/topicreader/topicreader_simple.go
50 строк
1 KB
Timofey Koolin
fix linter warnings
01 авг 2024, 21:43
01 авг 2024, 21:43
0c1a4c8
Код
Авторство
О чём код?
package topicreaderexamples import ( "context" "fmt" "io" "github.com/ydb-platform/ydb-go-sdk/v3/topic/topicreader" "github.com/ydb-platform/ydb-go-sdk/v3/topic/topicsugar" firestore "google.golang.org/genproto/firestore/bundle" ) // PrintMessageContent is simple example for easy start read messages // it is not recommend way for heavy-load processing, batch processing usually will faster func PrintMessageContent(ctx context.Context, reader *topicreader.Reader) { for { msg, _ := reader.ReadMessage(ctx) content, _ := io.ReadAll(msg) fmt.Println(string(content)) _ = reader.Commit(msg.Context(), msg) } } // ReadMessagesByBatch it is recommended way for process messages func ReadMessagesByBatch(ctx context.Context, reader *topicreader.Reader) { for { batch, _ := reader.ReadMessagesBatch(ctx) _, _ = processBatch(batch.Context(), batch) _ = reader.Commit(batch.Context(), batch) } } // UnmarshalMessageContentToJSONStruct is example for effective way for unmarshal json message content to value func UnmarshalMessageContentToJSONStruct(msg *topicreader.Message) { //nolint:tagliatelle type S struct { MyField int `json:"my_field"` } var v S _ = topicsugar.JSONUnmarshal(msg, &v) } // UnmarshalMessageContentToProtobufStruct is example for effective way for unmarshal protobuf message content to value func UnmarshalMessageContentToProtobufStruct(msg *topicreader.Message) { v := &firestore.BundledDocumentMetadata{} // protobuf type _ = topicsugar.ProtoUnmarshal(msg, v) }