vitess.io/vitess@v0.16.2/examples/compose/client.go (about)

     1  /*
     2   * Copyright 2019 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  // client.go is a sample for using the Vitess Go SQL driver.
    18  //
    19  // Before running this, start up a local example cluster as described in the
    20  // README.md file.
    21  //
    22  // Then run:
    23  // vitess$ . dev.env
    24  // vitess$ cd examples/local
    25  // vitess/examples/local$ go run client.go --server=localhost:15991
    26  package main
    27  
    28  import (
    29  	"fmt"
    30  	"math/rand"
    31  	"os"
    32  	"time"
    33  
    34  	"github.com/spf13/pflag"
    35  
    36  	"vitess.io/vitess/go/vt/vitessdriver"
    37  )
    38  
    39  var (
    40  	server = pflag.String("server", "", "vtgate server to connect to")
    41  )
    42  
    43  func main() {
    44  	pflag.Parse()
    45  	rand.Seed(time.Now().UnixNano())
    46  
    47  	// Connect to vtgate.
    48  	db, err := vitessdriver.Open(*server, "@primary")
    49  	if err != nil {
    50  		fmt.Printf("client error: %v\n", err)
    51  		os.Exit(1)
    52  	}
    53  	defer db.Close()
    54  
    55  	// Insert some messages on random pages.
    56  	fmt.Println("Inserting into primary...")
    57  	for i := 0; i < 3; i++ {
    58  		tx, err := db.Begin()
    59  		if err != nil {
    60  			fmt.Printf("begin failed: %v\n", err)
    61  			os.Exit(1)
    62  		}
    63  		page := rand.Intn(100) + 1
    64  		timeCreated := time.Now().UnixNano()
    65  		if _, err := tx.Exec("INSERT INTO messages (page,time_created_ns,message) VALUES (?,?,?)",
    66  			page, timeCreated, "V is for speed"); err != nil {
    67  			fmt.Printf("exec failed: %v\n", err)
    68  			os.Exit(1)
    69  		}
    70  		if err := tx.Commit(); err != nil {
    71  			fmt.Printf("commit failed: %v\n", err)
    72  			os.Exit(1)
    73  		}
    74  	}
    75  
    76  	// Read it back from the primary.
    77  	fmt.Println("Reading from primary...")
    78  	rows, err := db.Query("SELECT page, time_created_ns, message FROM messages")
    79  	if err != nil {
    80  		fmt.Printf("query failed: %v\n", err)
    81  		os.Exit(1)
    82  	}
    83  	for rows.Next() {
    84  		var page, timeCreated uint64
    85  		var msg string
    86  		if err := rows.Scan(&page, &timeCreated, &msg); err != nil {
    87  			fmt.Printf("scan failed: %v\n", err)
    88  			os.Exit(1)
    89  		}
    90  		fmt.Printf("(%v, %v, %q)\n", page, timeCreated, msg)
    91  	}
    92  	if err := rows.Err(); err != nil {
    93  		fmt.Printf("row iteration failed: %v\n", err)
    94  		os.Exit(1)
    95  	}
    96  
    97  	// Read from a replica.
    98  	// Note that this may be behind primary due to replication lag.
    99  	fmt.Println("Reading from replica...")
   100  
   101  	dbr, err := vitessdriver.Open(*server, "@replica")
   102  	if err != nil {
   103  		fmt.Printf("client error: %v\n", err)
   104  		os.Exit(1)
   105  	}
   106  	defer dbr.Close()
   107  
   108  	rows, err = dbr.Query("SELECT page, time_created_ns, message FROM messages")
   109  	if err != nil {
   110  		fmt.Printf("query failed: %v\n", err)
   111  		os.Exit(1)
   112  	}
   113  	for rows.Next() {
   114  		var page, timeCreated uint64
   115  		var msg string
   116  		if err := rows.Scan(&page, &timeCreated, &msg); err != nil {
   117  			fmt.Printf("scan failed: %v\n", err)
   118  			os.Exit(1)
   119  		}
   120  		fmt.Printf("(%v, %v, %q)\n", page, timeCreated, msg)
   121  	}
   122  	if err := rows.Err(); err != nil {
   123  		fmt.Printf("row iteration failed: %v\n", err)
   124  		os.Exit(1)
   125  	}
   126  }