-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathvstream_client.go
98 lines (90 loc) · 2.57 KB
/
vstream_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
Copyright 2021 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"context"
"fmt"
"io"
"log"
"time"
_ "vitess.io/vitess/go/vt/vtctl/grpcvtctlclient"
_ "vitess.io/vitess/go/vt/vtgate/grpcvtgateconn"
"vitess.io/vitess/go/vt/vtgate/vtgateconn"
binlogdatapb "vitess.io/vitess/go/vt/proto/binlogdata"
topodatapb "vitess.io/vitess/go/vt/proto/topodata"
vtgatepb "vitess.io/vitess/go/vt/proto/vtgate"
)
/*
This is a sample client for streaming using the vstream API. It is setup to work with the local example and you can
either stream from the unsharded commerce keyspace or the customer keyspace after the sharding step.
*/
func main() {
ctx := context.Background()
streamCustomer := true
var vgtid *binlogdatapb.VGtid
if streamCustomer {
vgtid = &binlogdatapb.VGtid{
ShardGtids: []*binlogdatapb.ShardGtid{{
Keyspace: "customer",
Shard: "-80",
// Gtid "" is to stream from the start, "current" is to stream from the current gtid
// you can also specify a gtid to start with.
Gtid: "", //"current" // "MySQL56/36a89abd-978f-11eb-b312-04ed332e05c2:1-265"
}, {
Keyspace: "customer",
Shard: "80-",
Gtid: "",
}}}
} else {
vgtid = &binlogdatapb.VGtid{
ShardGtids: []*binlogdatapb.ShardGtid{{
Keyspace: "commerce",
Shard: "0",
Gtid: "",
}}}
}
filter := &binlogdatapb.Filter{
Rules: []*binlogdatapb.Rule{{
Match: "customer",
Filter: "select * from customer",
}},
}
conn, err := vtgateconn.Dial(ctx, "localhost:15991")
if err != nil {
log.Fatal(err)
}
defer conn.Close()
flags := &vtgatepb.VStreamFlags{
// MinimizeSkew: false,
// HeartbeatInterval: 60, //seconds
// StopOnReshard: true,
// IncludeReshardJournalEvents: true,
}
reader, err := conn.VStream(ctx, topodatapb.TabletType_PRIMARY, vgtid, filter, flags)
if err != nil {
log.Fatal(err)
}
for {
e, err := reader.Recv()
switch err {
case nil:
fmt.Printf("%v\n", e)
case io.EOF:
fmt.Printf("stream ended\n")
return
default:
fmt.Printf("%s:: remote error: %v\n", time.Now(), err)
return
}
}
}