Skip to content

Commit 44b5d0f

Browse files
committed
add examples
1 parent 07cff18 commit 44b5d0f

File tree

1 file changed

+74
-3
lines changed

1 file changed

+74
-3
lines changed

topic/example_test.go

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,79 @@ import (
44
"context"
55
"fmt"
66
"io/ioutil"
7+
"log"
8+
"os"
79

810
ydb "github.com/ydb-platform/ydb-go-sdk/v3"
911
"github.com/ydb-platform/ydb-go-sdk/v3/topic/topicoptions"
12+
"github.com/ydb-platform/ydb-go-sdk/v3/topic/topictypes"
1013
)
1114

12-
func Example_topic_read_message() {
15+
func Example_create_topic() {
1316
ctx := context.TODO()
14-
db, err := ydb.Open(ctx, "grpcs://localhost:2135/local")
17+
db, err := connectDB(ctx)
1518
if err != nil {
16-
fmt.Printf("failed connect: %v", err)
19+
log.Fatalf("failed connect: %v", err)
20+
return
21+
}
22+
defer db.Close(ctx) // cleanup resources
23+
24+
err = db.Topic().Create(ctx, "topic-path",
25+
26+
// optional
27+
topicoptions.CreateWithSupportedCodecs(topictypes.CodecRaw, topictypes.CodecGzip),
28+
29+
// optional
30+
topicoptions.CreateWithMinActivePartitions(3),
31+
)
32+
if err != nil {
33+
log.Fatalf("failed create topic: %v", err)
34+
return
35+
}
36+
}
37+
38+
func Example_alter_topic() {
39+
ctx := context.TODO()
40+
db, err := connectDB(ctx)
41+
if err != nil {
42+
log.Fatalf("failed connect: %v", err)
43+
return
44+
}
45+
defer db.Close(ctx) // cleanup resources
46+
47+
err = db.Topic().Alter(ctx, "topic-path",
48+
topicoptions.AlterWithAddConsumers(topictypes.Consumer{
49+
Name: "new-consumer",
50+
SupportedCodecs: []topictypes.Codec{topictypes.CodecRaw, topictypes.CodecGzip}, // optional
51+
}),
52+
)
53+
if err != nil {
54+
log.Fatalf("failed alter topic: %v", err)
55+
return
56+
}
57+
}
58+
59+
func Example_drop_topic() {
60+
ctx := context.TODO()
61+
db, err := connectDB(ctx)
62+
if err != nil {
63+
log.Fatalf("failed connect: %v", err)
64+
return
65+
}
66+
defer db.Close(ctx) // cleanup resources
67+
68+
err = db.Topic().Drop(ctx, "topic-path")
69+
if err != nil {
70+
log.Fatalf("failed drop topic: %v", err)
71+
return
72+
}
73+
}
74+
75+
func Example_read_message() {
76+
ctx := context.TODO()
77+
db, err := connectDB(ctx)
78+
if err != nil {
79+
log.Fatalf("failed connect: %v", err)
1780
return
1881
}
1982
defer db.Close(ctx) // cleanup resources
@@ -39,3 +102,11 @@ func Example_topic_read_message() {
39102
fmt.Println(string(content))
40103
}
41104
}
105+
106+
func connectDB(ctx context.Context) (ydb.Connection, error) {
107+
connectionString := os.Getenv("YDB_CONNECTION_STRING")
108+
if connectionString == "" {
109+
connectionString = "grpc://localhost:2135/local"
110+
}
111+
return ydb.Open(ctx, connectionString)
112+
}

0 commit comments

Comments
 (0)