Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@

<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>

<spanner.test.instance>default-instance</spanner.test.instance>
<spanner.sample.database>mysample</spanner.sample.database>
<spanner.quickstart.database>quickstart-db</spanner.quickstart.database>
</properties>

<prerequisites>
Expand Down Expand Up @@ -112,6 +116,7 @@
<module>monitoring/v2</module>
<module>monitoring/v3</module>
<module>pubsub/cloud-client</module>
<module>spanner/cloud-client</module>
<module>speech/grpc</module>
<module>storage/cloud-client</module>
<module>storage/json-api</module>
Expand Down
35 changes: 35 additions & 0 deletions spanner/cloud-client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Getting Started with Cloud Spanner and the Google Cloud Client libraries

[Cloud Spanner][Spanner] is a horizontally-scalable database-as-a-service
with transactions and SQL support.
These sample Java applications demonstrate how to access the Spanner API using
the [Google Cloud Client Library for Java][google-cloud-java].

[Spanner]: https://cloud.google.com/spanner/
[google-cloud-java]: https://github.com/GoogleCloudPlatform/google-cloud-java

## Quickstart

Install [Maven](http://maven.apache.org/).

Build your project with:

mvn clean package -DskipTests

You can then run a given `ClassName` via:

mvn exec:java -Dexec.mainClass=com.example.spanner.ClassName \
-DpropertyName=propertyValue \
-Dexec.args="any arguments to the app"

### Running a simple query (using the quickstart sample)

mvn exec:java -Dexec.mainClass=com.example.spanner.QuickstartSample -Dexec.args="my-instance my-database"

## Tutorial

### Running the tutorial
mvn exec:java -Dexec.mainClass=com.example.spanner.SpannerSample -Dexec.args="<command> my-instance my-database"

## Test
mvn verify -Dspanner.test.instance=<instance id> -Dspanner.sample.database=<new database id> -Dspanner.quickstart.database=<existing database id>
101 changes: 101 additions & 0 deletions spanner/cloud-client/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<!--
Copyright 2017 Google Inc.

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.
-->
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>come.example.spanner</groupId>
<artifactId>spanner-google-cloud-samples</artifactId>
<packaging>jar</packaging>

<!-- Parent defines config for testing & linting. -->
<parent>
<artifactId>doc-samples</artifactId>
<groupId>com.google.cloud</groupId>
<version>1.0.0</version>
<relativePath>../..</relativePath>
</parent>

<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-spanner</artifactId>
<version>0.9.2-beta</version>
<exclusions>
<exclusion> <!-- exclude an old version of Guava -->
<groupId>com.google.guava</groupId>
<artifactId>guava-jdk5</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-tcnative-boringssl-static</artifactId>
<version>1.1.33.Fork23</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>20.0</version>
</dependency>
<!-- Test dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
<version>0.31</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.example.spanner.SpannerSample</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
Copyright 2017, Google, Inc.

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 com.example.spanner;

// [START spanner_quickstart]
// Imports the Google Cloud client library
import com.google.cloud.spanner.DatabaseClient;
import com.google.cloud.spanner.DatabaseId;
import com.google.cloud.spanner.ResultSet;
import com.google.cloud.spanner.Spanner;
import com.google.cloud.spanner.SpannerOptions;
import com.google.cloud.spanner.Statement;

/**
* A quick start code for Cloud Spanner. It demonstrates how to setup the Cloud Spanner client and
* execute a simple query using it against an existing database.
*/
public class QuickstartSample {
public static void main(String... args) throws Exception {

if (args.length != 2) {
System.err.println("Usage: QuickStartSample <instance_id> <database_id>");
return;
}
// Instantiates a client
SpannerOptions options = SpannerOptions.newBuilder().build();
Spanner spanner = options.getService();

// Name of your database. Eg: projects/my-project/instances/instanceId/databases/databaseId
String instanceId = args[0];
String databaseId = args[1];
try {
// Creates a database client
DatabaseClient dbClient = spanner.getDatabaseClient(DatabaseId.of(
options.getProjectId(), instanceId, databaseId));
// Queries the database
ResultSet resultSet = dbClient.singleUse().executeQuery(Statement.of("SELECT 1"));

System.out.println("\n\nResults:");
// Prints the results
while (resultSet.next()) {
System.out.printf("%d\n\n", resultSet.getLong(0));
}
} finally {
// Closes the client which will free up the resources used
spanner.closeAsync().get();
}
}
}
// [END spanner_quickstart]
Loading