java: Add "example" project to demonstrate how to use the Java client.

This commit is contained in:
Michael Berlin 2015-12-17 22:18:48 -08:00
Родитель f8f1e4f540
Коммит c7f2b17d5c
4 изменённых файлов: 168 добавлений и 0 удалений

90
java/example/pom.xml Normal file
Просмотреть файл

@ -0,0 +1,90 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.youtube.vitess</groupId>
<artifactId>vitess-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>example</artifactId>
<dependencies>
<dependency>
<groupId>com.youtube.vitess</groupId>
<artifactId>client</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.youtube.vitess</groupId>
<artifactId>grpc-client</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-all</artifactId>
<version>${grpc.version}</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
<pluginRepositories>
<pluginRepository>
<releases>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2</url>
</pluginRepository>
<pluginRepository>
<id>protoc-plugin</id>
<url>https://dl.bintray.com/sergei-ivanov/maven/</url>
</pluginRepository>
</pluginRepositories>
<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.2.3.Final</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>com.google.protobuf.tools</groupId>
<artifactId>maven-protoc-plugin</artifactId>
<version>0.4.2</version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:${protobuf.java.version}:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}</pluginArtifact>
<protoSourceRoot>../../proto</protoSourceRoot>
<includes>
<include>query.proto</include>
<include>topodata.proto</include>
<include>vtgate.proto</include>
<include>vtrpc.proto</include>
<include>vtgateservice.proto</include>
</includes>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

Просмотреть файл

@ -0,0 +1,74 @@
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:
*
* $VTROOT/bin/vtctlclient -server <vtctld-host:port> ApplySchema -sql \
* "$(cat create_test_table.sql)" test_keyspace
*/
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,
/* notInTransaction (ignore this because we will soon remove it) */ false);
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,
/* bindVars */ null, 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();
}
}

Просмотреть файл

@ -12,6 +12,7 @@
<inceptionYear>2014</inceptionYear>
<modules>
<module>client</module>
<module>example</module>
<module>gorpc</module>
<module>grpc-client</module>
<module>hadoop</module>

Просмотреть файл

@ -68,5 +68,8 @@ php client.php --server=localhost:15991 || teardown
echo "Run Go client script..."
go run client.go -server=localhost:15991 || teardown
echo "Run Java client script..."
mvn -f ../../java/example/pom.xml exec:java -Dexec.cleanupDaemonThreads=false -Dexec.mainClass="com.youtube.vitess.example.VitessClientExample" -Dexec.args="localhost:15991"
exitcode=0
teardown