github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/topic/README.md (about)

     1  # `topic` - pure Go native client for [YDB Topic](https://ydb.tech/en/docs/concepts/topic)
     2  [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://github.com/ydb-platform/ydb/blob/main/LICENSE)
     3  [![Release](https://img.shields.io/github/v/release/ydb-platform/ydb-go-sdk.svg?style=flat-square)](https://github.com/ydb-platform/ydb-go-sdk/releases)
     4  [![PkgGoDev](https://pkg.go.dev/badge/github.com/ydb-platform/ydb-go-sdk/v3)](https://pkg.go.dev/github.com/ydb-platform/ydb-go-sdk/v3/topic)
     5  ![tests](https://github.com/ydb-platform/ydb-go-sdk/workflows/tests/badge.svg?branch=master)
     6  ![lint](https://github.com/ydb-platform/ydb-go-sdk/workflows/lint/badge.svg?branch=master)
     7  [![Go Report Card](https://goreportcard.com/badge/github.com/ydb-platform/ydb-go-sdk/v3)](https://goreportcard.com/report/github.com/ydb-platform/ydb-go-sdk/v3)
     8  [![codecov](https://codecov.io/gh/ydb-platform/ydb-go-sdk/branch/master/graph/badge.svg?precision=2)](https://app.codecov.io/gh/ydb-platform/ydb-go-sdk)
     9  ![Code lines](https://sloc.xyz/github/ydb-platform/ydb-go-sdk/?category=code)
    10  [![View examples](https://img.shields.io/badge/learn-examples-brightgreen.svg)](https://github.com/ydb-platform/ydb-go-examples/tree/master/topic)
    11  [![Telegram](https://img.shields.io/badge/chat-on%20Telegram-2ba2d9.svg)](https://t.me/YDBPlatform)
    12  [![WebSite](https://img.shields.io/badge/website-ydb.tech-blue.svg)](https://ydb.tech)
    13  
    14  See [ydb-go-sdk](https://github.com/ydb-platform/ydb-go-sdk) for describe all driver features.
    15  
    16  `YDB` is an open-source Distributed SQL Database that combines high availability and scalability with strict consistency and [ACID](https://en.wikipedia.org/wiki/ACID) transactions with [CDC](https://en.wikipedia.org/wiki/Change_data_capture) and Data stream features.
    17  
    18  ## Installation
    19  
    20  ```sh
    21  go get -u github.com/ydb-platform/ydb-go-sdk/v3
    22  ```
    23  
    24  ## Example Usage <a name="example"></a>
    25  * connect to YDB
    26  ```golang
    27  db, err := ydb.Open(ctx, "grpc://localhost:2136/local")
    28  if err != nil {
    29      log.Fatal(err)
    30  }
    31  ```
    32  
    33  * send messages
    34  ```golang
    35  producerAndGroupID := "group-id"
    36  writer, err := db.Topic().StartWriter(producerAndGroupID, "topicName",
    37      topicoptions.WithMessageGroupID(producerAndGroupID),
    38      topicoptions.WithCodec(topictypes.CodecGzip),
    39  )
    40  if err != nil {
    41      log.Fatal(err)
    42  }
    43  
    44  data1 := []byte{1, 2, 3}
    45  data2 := []byte{4, 5, 6}
    46  mess1 := topicwriter.Message{Data: bytes.NewReader(data1)}
    47  mess2 := topicwriter.Message{Data: bytes.NewReader(data2)}
    48  
    49  err = writer.Write(ctx, mess1, mess2)
    50  if err != nil {
    51  	log.Fatal(err)
    52  }
    53  ```
    54  
    55  * read messages
    56  ```golang
    57  for {
    58      msg, err := reader.ReadMessage(ctx)
    59      if err != nil {
    60          log.Fatal(err)
    61      }
    62      content, err := io.ReadAll(msg)
    63      if err != nil {
    64          log.Fatal(err)
    65      }
    66      fmt.Println(string(content))
    67      err = reader.Commit(msg.Context(), msg)
    68      if err != nil {
    69          log.Fatal(err)
    70      }
    71  }
    72   
    73  ```