Skip to content

Commit 8dfeab1

Browse files
authored
Merge pull request #407 add topic examples
2 parents 07cff18 + bffa84e commit 8dfeab1

File tree

2 files changed

+107
-4
lines changed

2 files changed

+107
-4
lines changed

topic/client_e2e_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"github.com/ydb-platform/ydb-go-sdk/v3/topic/topictypes"
2121
)
2222

23+
const defaultConnectionString = "grpc://localhost:2136/local"
24+
2325
func TestClient_CreateDropTopic(t *testing.T) {
2426
ctx := xtest.Context(t)
2527
db := connect(t)
@@ -152,7 +154,7 @@ func TestSchemeList(t *testing.T) {
152154
}
153155

154156
func connect(t testing.TB, opts ...ydb.Option) ydb.Connection {
155-
connectionString := "grpc://localhost:2136/local"
157+
connectionString := defaultConnectionString
156158
if cs := os.Getenv("YDB_CONNECTION_STRING"); cs != "" {
157159
connectionString = cs
158160
}

topic/example_test.go

Lines changed: 104 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,120 @@
1+
//nolint:goconst
12
package topic_test
23

34
import (
45
"context"
56
"fmt"
67
"io/ioutil"
8+
"log"
9+
"os"
710

811
ydb "github.com/ydb-platform/ydb-go-sdk/v3"
912
"github.com/ydb-platform/ydb-go-sdk/v3/topic/topicoptions"
13+
"github.com/ydb-platform/ydb-go-sdk/v3/topic/topictypes"
1014
)
1115

12-
func Example_topic_read_message() {
16+
func Example_createTopic() {
1317
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)
15116
if err != nil {
16-
fmt.Printf("failed connect: %v", err)
117+
log.Printf("failed connect: %v", err)
17118
return
18119
}
19120
defer db.Close(ctx) // cleanup resources

0 commit comments

Comments
 (0)