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

     1  package topic_test
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"io"
     7  	"log"
     8  	"os"
     9  
    10  	ydb "github.com/ydb-platform/ydb-go-sdk/v3"
    11  	"github.com/ydb-platform/ydb-go-sdk/v3/topic/topicoptions"
    12  	"github.com/ydb-platform/ydb-go-sdk/v3/topic/topictypes"
    13  )
    14  
    15  func Example_createTopic() {
    16  	ctx := context.TODO()
    17  	connectionString := os.Getenv("YDB_CONNECTION_STRING")
    18  	if connectionString == "" {
    19  		connectionString = "grpc://localhost:2136/local"
    20  	}
    21  	db, err := ydb.Open(ctx, connectionString)
    22  	if err != nil {
    23  		log.Printf("failed connect: %v", err)
    24  
    25  		return
    26  	}
    27  	defer db.Close(ctx) // cleanup resources
    28  
    29  	err = db.Topic().Create(ctx, "topic-path",
    30  
    31  		// optional
    32  		topicoptions.CreateWithSupportedCodecs(topictypes.CodecRaw, topictypes.CodecGzip),
    33  
    34  		// optional
    35  		topicoptions.CreateWithMinActivePartitions(3),
    36  	)
    37  	if err != nil {
    38  		log.Printf("failed create topic: %v", err)
    39  
    40  		return
    41  	}
    42  }
    43  
    44  func Example_alterTopic() {
    45  	ctx := context.TODO()
    46  	connectionString := os.Getenv("YDB_CONNECTION_STRING")
    47  	if connectionString == "" {
    48  		connectionString = "grpc://localhost:2136/local"
    49  	}
    50  	db, err := ydb.Open(ctx, connectionString)
    51  	if err != nil {
    52  		log.Printf("failed connect: %v", err)
    53  
    54  		return
    55  	}
    56  	defer db.Close(ctx) // cleanup resources
    57  
    58  	err = db.Topic().Alter(ctx, "topic-path",
    59  		topicoptions.AlterWithAddConsumers(topictypes.Consumer{
    60  			Name:            "new-consumer",
    61  			SupportedCodecs: []topictypes.Codec{topictypes.CodecRaw, topictypes.CodecGzip}, // optional
    62  		}),
    63  	)
    64  	if err != nil {
    65  		log.Printf("failed alter topic: %v", err)
    66  
    67  		return
    68  	}
    69  }
    70  
    71  func Example_describeTopic() {
    72  	ctx := context.TODO()
    73  	connectionString := os.Getenv("YDB_CONNECTION_STRING")
    74  	if connectionString == "" {
    75  		connectionString = "grpc://localhost:2136/local"
    76  	}
    77  	db, err := ydb.Open(ctx, connectionString)
    78  	if err != nil {
    79  		log.Printf("failed connect: %v", err)
    80  
    81  		return
    82  	}
    83  	defer db.Close(ctx) // cleanup resources
    84  
    85  	descResult, err := db.Topic().Describe(ctx, "topic-path")
    86  	if err != nil {
    87  		log.Printf("failed drop topic: %v", err)
    88  
    89  		return
    90  	}
    91  	fmt.Printf("describe: %#v\n", descResult)
    92  }
    93  
    94  func Example_dropTopic() {
    95  	ctx := context.TODO()
    96  	connectionString := os.Getenv("YDB_CONNECTION_STRING")
    97  	if connectionString == "" {
    98  		connectionString = "grpc://localhost:2136/local"
    99  	}
   100  	db, err := ydb.Open(ctx, connectionString)
   101  	if err != nil {
   102  		log.Printf("failed connect: %v", err)
   103  
   104  		return
   105  	}
   106  	defer db.Close(ctx) // cleanup resources
   107  
   108  	err = db.Topic().Drop(ctx, "topic-path")
   109  	if err != nil {
   110  		log.Printf("failed drop topic: %v", err)
   111  
   112  		return
   113  	}
   114  }
   115  
   116  func Example_readMessage() {
   117  	ctx := context.TODO()
   118  	connectionString := os.Getenv("YDB_CONNECTION_STRING")
   119  	if connectionString == "" {
   120  		connectionString = "grpc://localhost:2136/local"
   121  	}
   122  	db, err := ydb.Open(ctx, connectionString)
   123  	if err != nil {
   124  		log.Printf("failed connect: %v", err)
   125  
   126  		return
   127  	}
   128  	defer db.Close(ctx) // cleanup resources
   129  
   130  	reader, err := db.Topic().StartReader("consumer", topicoptions.ReadTopic("/topic/path"))
   131  	if err != nil {
   132  		fmt.Printf("failed start reader: %v", err)
   133  
   134  		return
   135  	}
   136  
   137  	for {
   138  		mess, err := reader.ReadMessage(ctx)
   139  		if err != nil {
   140  			fmt.Printf("failed start reader: %v", err)
   141  
   142  			return
   143  		}
   144  
   145  		content, err := io.ReadAll(mess)
   146  		if err != nil {
   147  			fmt.Printf("failed start reader: %v", err)
   148  
   149  			return
   150  		}
   151  		fmt.Println(string(content))
   152  	}
   153  }