Created
June 14, 2019 02:26
-
-
Save sougou/a977e2d996b0ae8c63d2cf643cfc72d7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright 2019 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" | |
"flag" | |
"fmt" | |
"vitess.io/vitess/go/mysql" | |
"vitess.io/vitess/go/sqltypes" | |
"vitess.io/vitess/go/vt/log" | |
binlogdatapb "vitess.io/vitess/go/vt/proto/binlogdata" | |
querypb "vitess.io/vitess/go/vt/proto/query" | |
topodatapb "vitess.io/vitess/go/vt/proto/topodata" | |
"vitess.io/vitess/go/vt/vtgate/vtgateconn" | |
_ "vitess.io/vitess/go/vt/vtgate/grpcvtgateconn" | |
) | |
func main() { | |
flag.Parse() | |
ctx, cancel := context.WithCancel(context.Background()) | |
defer cancel() | |
// Obtain binlog positions. | |
var mpos []mysql.Position | |
for _, tab := range []int{2, 3} { | |
mysqlParams := &mysql.ConnParams{ | |
Uname: "vt_dba", | |
UnixSocket: fmt.Sprintf("/home/sougou/dev/demoroot/vt_0000000%d00/mysql.sock", tab), | |
Charset: "utf8", | |
} | |
mconn, err := mysql.Connect(ctx, mysqlParams) | |
if err != nil { | |
log.Exit(err) | |
} | |
pos, err := mconn.MasterPosition() | |
if err != nil { | |
log.Exit(err) | |
} | |
mpos = append(mpos, pos) | |
mconn.Close() | |
} | |
// Build the request. | |
// You can add as many keyspace-shards as you want. | |
vgtid := &binlogdatapb.VGtid{ | |
ShardGtids: []*binlogdatapb.ShardGtid{{ | |
Keyspace: "customer", | |
Shard: "-80", | |
Gtid: fmt.Sprintf("%s/%s", mpos[0].GTIDSet.Flavor(), mpos[0]), | |
}, { | |
Keyspace: "customer", | |
Shard: "80-", | |
Gtid: fmt.Sprintf("%s/%s", mpos[1].GTIDSet.Flavor(), mpos[1]), | |
}}, | |
} | |
filter := &binlogdatapb.Filter{ | |
Rules: []*binlogdatapb.Rule{{ | |
// You can also filter specific tables or columns like this: | |
// Match: "customer", | |
// Filter: "select cid, name from customer", | |
Match: "/.*/", | |
}}, | |
} | |
gconn, err := vtgateconn.Dial(ctx, "localhost:15991") | |
if err != nil { | |
log.Exit(err) | |
} | |
defer gconn.Close() | |
reader, err := gconn.VStream(ctx, topodatapb.TabletType_MASTER, vgtid, filter) | |
if err != nil { | |
log.Exit(err) | |
} | |
tableFields := make(map[string][]*querypb.Field) | |
for { | |
events, err := reader.Recv() | |
if err != nil { | |
log.Exit(err) | |
} | |
for _, event := range events { | |
switch event.Type { | |
case binlogdatapb.VEventType_FIELD: | |
// Update table fields. It's needed for processing ROW events. | |
tableFields[event.FieldEvent.TableName] = event.FieldEvent.Fields | |
case binlogdatapb.VEventType_ROW: | |
fmt.Printf("Row Event: %s\n", event.RowEvent.TableName) | |
for _, change := range event.RowEvent.RowChanges { | |
fmt.Printf(" Befor: %v\n", toResult(tableFields[event.RowEvent.TableName], change.Before)) | |
fmt.Printf(" After: %v\n", toResult(tableFields[event.RowEvent.TableName], change.After)) | |
} | |
case binlogdatapb.VEventType_VGTID: | |
// Remember vgtid here. | |
case binlogdatapb.VEventType_COMMIT: | |
// Save the remembered vgtid after processing COMMIT. | |
// The saved VGTID must be used to continue the stream if interrupted. | |
default: | |
} | |
if event.Type != binlogdatapb.VEventType_ROW { | |
fmt.Printf("%v\n", event) | |
} | |
} | |
} | |
} | |
func toResult(fields []*querypb.Field, row *querypb.Row) []sqltypes.Value { | |
if row == nil { | |
return nil | |
} | |
return sqltypes.Proto3ToResult(&querypb.QueryResult{ | |
Fields: fields, | |
Rows: []*querypb.Row{row}, | |
}).Rows[0] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment