Skip to content

Commit 20db23e

Browse files
committed
godoc
1 parent 93e874f commit 20db23e

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

doc.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Package ydb-go-sdk - native Go's driver for YDB.
2+
/*
3+
Supports table, discovery, coordination, ratelimiter, scheme and scripting clients for YDB.
4+
YDB is an open-source Distributed SQL Database that combines high availability and scalability with strict consistency and ACID transactions.
5+
*/
6+
package ydb

example_table_bulk_upsert_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package ydb_test
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"github.com/ydb-platform/ydb-go-sdk/v3/table/types"
7+
"log"
8+
"time"
9+
10+
"github.com/ydb-platform/ydb-go-sdk/v3"
11+
"github.com/ydb-platform/ydb-go-sdk/v3/table"
12+
)
13+
14+
func Example_tableBulkUpsert() {
15+
ctx := context.Background()
16+
db, err := ydb.New(ctx,
17+
ydb.WithConnectionString("grpcs://localhost:2135/?database=/local"),
18+
ydb.WithAnonymousCredentials(),
19+
)
20+
if err != nil {
21+
log.Fatal(err)
22+
}
23+
defer func() {
24+
_ = db.Close(ctx)
25+
}()
26+
type logMessage struct {
27+
App string
28+
Host string
29+
Timestamp time.Time
30+
HTTPCode uint32
31+
Message string
32+
}
33+
// prepare native go data
34+
const batchSize = 10000
35+
logs := make([]logMessage, 0, batchSize)
36+
for i := 0; i < batchSize; i++ {
37+
message := logMessage{
38+
App: fmt.Sprintf("App_%d", i/256),
39+
Host: fmt.Sprintf("192.168.0.%d", i%256),
40+
Timestamp: time.Now().Add(time.Millisecond * time.Duration(i%1000)),
41+
HTTPCode: 200,
42+
}
43+
if i%2 == 0 {
44+
message.Message = "GET / HTTP/1.1"
45+
} else {
46+
message.Message = "GET /images/logo.png HTTP/1.1"
47+
}
48+
logs = append(logs, message)
49+
}
50+
// execute bulk upsert with native ydb data
51+
err = db.Table().Do( // Do retry operation on errors with best effort
52+
ctx, // context manage exiting from Do
53+
func(ctx context.Context, s table.Session) (err error) { // retry operation
54+
rows := make([]types.Value, 0, len(logs))
55+
for _, msg := range logs {
56+
rows = append(rows, types.StructValue(
57+
types.StructFieldValue("App", types.UTF8Value(msg.App)),
58+
types.StructFieldValue("Host", types.UTF8Value(msg.Host)),
59+
types.StructFieldValue("Timestamp", types.TimestampValueFromTime(msg.Timestamp)),
60+
types.StructFieldValue("HTTPCode", types.Uint32Value(msg.HTTPCode)),
61+
types.StructFieldValue("Message", types.UTF8Value(msg.Message)),
62+
))
63+
}
64+
return s.BulkUpsert(ctx, "/local/bulk_upsert_example", types.ListValue(rows...))
65+
},
66+
)
67+
if err != nil {
68+
log.Printf("unexpected error: %v", err)
69+
}
70+
}

0 commit comments

Comments
 (0)