|
| 1 | +//nolint:goconst |
1 | 2 | package topic_test |
2 | 3 |
|
3 | 4 | import ( |
4 | 5 | "context" |
5 | 6 | "fmt" |
6 | 7 | "io/ioutil" |
| 8 | + "log" |
| 9 | + "os" |
7 | 10 |
|
8 | 11 | ydb "github.com/ydb-platform/ydb-go-sdk/v3" |
9 | 12 | "github.com/ydb-platform/ydb-go-sdk/v3/topic/topicoptions" |
| 13 | + "github.com/ydb-platform/ydb-go-sdk/v3/topic/topictypes" |
10 | 14 | ) |
11 | 15 |
|
12 | | -func Example_topic_read_message() { |
| 16 | +func Example_createTopic() { |
13 | 17 | ctx := context.TODO() |
14 | | - db, err := ydb.Open(ctx, "grpcs://localhost:2135/local") |
| 18 | + connectionString := os.Getenv("YDB_CONNECTION_STRING") |
| 19 | + if connectionString == "" { |
| 20 | + connectionString = "grpc://localhost:2136/local" |
| 21 | + } |
| 22 | + db, err := ydb.Open(ctx, connectionString) |
| 23 | + if err != nil { |
| 24 | + log.Printf("failed connect: %v", err) |
| 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 | + return |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func Example_alterTopic() { |
| 44 | + ctx := context.TODO() |
| 45 | + connectionString := os.Getenv("YDB_CONNECTION_STRING") |
| 46 | + if connectionString == "" { |
| 47 | + connectionString = "grpc://localhost:2136/local" |
| 48 | + } |
| 49 | + db, err := ydb.Open(ctx, connectionString) |
| 50 | + if err != nil { |
| 51 | + log.Printf("failed connect: %v", err) |
| 52 | + return |
| 53 | + } |
| 54 | + defer db.Close(ctx) // cleanup resources |
| 55 | + |
| 56 | + err = db.Topic().Alter(ctx, "topic-path", |
| 57 | + topicoptions.AlterWithAddConsumers(topictypes.Consumer{ |
| 58 | + Name: "new-consumer", |
| 59 | + SupportedCodecs: []topictypes.Codec{topictypes.CodecRaw, topictypes.CodecGzip}, // optional |
| 60 | + }), |
| 61 | + ) |
| 62 | + if err != nil { |
| 63 | + log.Printf("failed alter topic: %v", err) |
| 64 | + return |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func Example_describeTopic() { |
| 69 | + ctx := context.TODO() |
| 70 | + connectionString := os.Getenv("YDB_CONNECTION_STRING") |
| 71 | + if connectionString == "" { |
| 72 | + connectionString = "grpc://localhost:2136/local" |
| 73 | + } |
| 74 | + db, err := ydb.Open(ctx, connectionString) |
| 75 | + if err != nil { |
| 76 | + log.Printf("failed connect: %v", err) |
| 77 | + return |
| 78 | + } |
| 79 | + defer db.Close(ctx) // cleanup resources |
| 80 | + |
| 81 | + descResult, err := db.Topic().Describe(ctx, "topic-path") |
| 82 | + if err != nil { |
| 83 | + log.Printf("failed drop topic: %v", err) |
| 84 | + return |
| 85 | + } |
| 86 | + fmt.Printf("describe: %#v\n", descResult) |
| 87 | +} |
| 88 | + |
| 89 | +func Example_dropTopic() { |
| 90 | + ctx := context.TODO() |
| 91 | + connectionString := os.Getenv("YDB_CONNECTION_STRING") |
| 92 | + if connectionString == "" { |
| 93 | + connectionString = "grpc://localhost:2136/local" |
| 94 | + } |
| 95 | + db, err := ydb.Open(ctx, connectionString) |
| 96 | + if err != nil { |
| 97 | + log.Printf("failed connect: %v", err) |
| 98 | + return |
| 99 | + } |
| 100 | + defer db.Close(ctx) // cleanup resources |
| 101 | + |
| 102 | + err = db.Topic().Drop(ctx, "topic-path") |
| 103 | + if err != nil { |
| 104 | + log.Printf("failed drop topic: %v", err) |
| 105 | + return |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +func Example_readMessage() { |
| 110 | + ctx := context.TODO() |
| 111 | + connectionString := os.Getenv("YDB_CONNECTION_STRING") |
| 112 | + if connectionString == "" { |
| 113 | + connectionString = "grpc://localhost:2136/local" |
| 114 | + } |
| 115 | + db, err := ydb.Open(ctx, connectionString) |
15 | 116 | if err != nil { |
16 | | - fmt.Printf("failed connect: %v", err) |
| 117 | + log.Printf("failed connect: %v", err) |
17 | 118 | return |
18 | 119 | } |
19 | 120 | defer db.Close(ctx) // cleanup resources |
|
0 commit comments