package com.youtube.vitess.example;

import com.google.common.collect.ImmutableMap;
import com.google.common.net.HostAndPort;

import com.youtube.vitess.client.Context;
import com.youtube.vitess.client.RpcClient;
import com.youtube.vitess.client.VTGateConn;
import com.youtube.vitess.client.VTGateTx;
import com.youtube.vitess.client.cursor.Cursor;
import com.youtube.vitess.client.cursor.Row;
import com.youtube.vitess.client.grpc.GrpcClientFactory;
import com.youtube.vitess.proto.Topodata.TabletType;

import org.joda.time.Duration;

import java.net.InetSocketAddress;
import java.util.Arrays;
import java.util.Map;

/**
 * VitessClientExample.java is a sample for using the Vitess Java Client with an unsharded keyspace.
 *
 * Before running this, start up a local example cluster as described in the
 * examples/local/README.md file.
 *
 * Alternatively, load the schema examples/local/create_test_table.sql into your instance:
 *
 * <pre>
 *   $VTROOT/bin/vtctlclient -server <vtctld-host:port> ApplySchema -sql \
 *   "$(cat create_test_table.sql)" test_keyspace
 * </pre>
 */
public class VitessClientExample {
  public static void main(String[] args) throws Exception {
    if (args.length != 1) {
      System.out.println("usage: VitessClientExample <vtgate-host:port>");
      System.exit(1);
    }

    // Connect to vtgate.
    HostAndPort hostAndPort = HostAndPort.fromString(args[0]);
    Context ctx = Context.getDefault().withDeadlineAfter(Duration.millis(5 * 1000));
    RpcClient client =
        new GrpcClientFactory().create(ctx, new InetSocketAddress(hostAndPort.getHostText(),
            hostAndPort.getPort()));
    VTGateConn conn = new VTGateConn(client);

    String keyspace = "test_keyspace";
    Iterable<String> shards = Arrays.asList("0");
    Map<String, Object> bindVars =
        new ImmutableMap.Builder<String, Object>().put("msg", "V is for speed").build();

    // Insert something.
    System.out.println("Inserting into master...");
    VTGateTx tx = conn.begin(ctx);
    tx.executeShards(ctx, "INSERT INTO test_table (msg) VALUES (:msg)", keyspace, shards, bindVars,
        TabletType.MASTER, false /* notInTransaction (ignore this because we will soon remove it) */);
    tx.commit(ctx);

    // Read it back from the master.
    System.out.println("Reading from master...");
    Cursor cursor =
        conn.executeShards(ctx, "SELECT id, msg FROM test_table", keyspace, shards,
            null /* bindVars */, TabletType.MASTER);
    Row row;
    while ((row = cursor.next()) != null) {
      long id = row.getLong("id");
      byte[] msg = row.getBytes("msg");
      System.out.format("(%d, %s)\n", id, new String(msg));
    }
    cursor.close();

    conn.close();
    client.close();
  }
}

@@ -26,24 +26,139 @@
 s.example;%0A%0A
+import java.net.InetSocketAddress;%0Aimport java.util.Arrays;%0Aimport java.util.Map;%0A%0Aimport org.joda.time.Duration;%0A%0A
 import com.g
@@ -234,17 +234,16 @@
 ndPort;%0A
-%0A
 import c
@@ -616,123 +616,8 @@
 e;%0A%0A
-import org.joda.time.Duration;%0A%0Aimport java.net.InetSocketAddress;%0Aimport java.util.Arrays;%0Aimport java.util.Map;%0A%0A
 /**%0A
@@ -1392,16 +1392,126 @@
 gs%5B0%5D);%0A
+    InetSocketAddress addr =%0A        new InetSocketAddress(hostAndPort.getHostText(), hostAndPort.getPort());%0A
     Cont
@@ -1591,16 +1591,21 @@
 ));%0A    
+try (
 RpcClien
@@ -1614,24 +1614,16 @@
 client =
-%0A       
  new Grp
@@ -1655,94 +1655,19 @@
 tx, 
-new InetSocketAddress(hostAndPort.getHostText(),%0A            hostAndPort.getPort()));%0A
+addr);%0A    
     
@@ -1707,18 +1707,22 @@
 client);
-%0A%0A
+) %7B%0A  
     Stri
@@ -1756,16 +1756,18 @@
 e%22;%0A    
+  
 Iterable
@@ -1808,16 +1808,18 @@
 %22);%0A    
+  
 Map%3CStri
@@ -1841,16 +1841,18 @@
 dVars =%0A
+  
         
@@ -1937,16 +1937,18 @@
 );%0A%0A    
+  
 // Inser
@@ -1960,16 +1960,18 @@
 ething.%0A
+  
     Syst
@@ -2014,24 +2014,26 @@
 r...%22);%0A    
+ 
+ 
 VTGateTx tx 
@@ -2051,16 +2051,18 @@
 n(ctx);%0A
+  
     tx.e
@@ -2142,16 +2142,26 @@
  shards,
+%0A         
  bindVar
@@ -2162,24 +2162,16 @@
 indVars,
-%0A       
  TabletT
@@ -2262,16 +2262,18 @@
 /);%0A    
+  
 tx.commi
@@ -2277,24 +2277,26 @@
 mmit(ctx);%0A%0A
+  
     // Read 
@@ -2320,24 +2320,26 @@
 master.%0A    
+  
 System.out.p
@@ -2376,16 +2376,23 @@
 %22);%0A    
+  try (
 Cursor c
@@ -2395,24 +2395,26 @@
 or cursor =%0A
+  
         conn
@@ -2497,16 +2497,18 @@
         
+  
 null /* 
@@ -2539,17 +2539,24 @@
 MASTER);
+) %7B
 %0A
+    
     Row 
@@ -2560,16 +2560,20 @@
 ow row;%0A
+    
     whil
@@ -2614,16 +2614,20 @@
 %7B%0A      
+  
+  
 long id 
@@ -2643,24 +2643,28 @@
 Long(%22id%22);%0A
+    
       byte%5B%5D
@@ -2687,24 +2687,28 @@
 tes(%22msg%22);%0A
+    
       System
@@ -2763,68 +2763,27 @@
     
-%7D%0A
     
-cursor.close();%0A%0A    conn.close();%0A    client.close();
+%7D%0A      %7D%0A    %7D
 %0A  %7D

Use try-resource blocks instead of explicit close() statements.