vitess.io/vitess@v0.16.2/examples/local/vstream_client.go (about)

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