Skip to content

Commit 381b81b

Browse files
authored
Spanner - Initial commit (GoogleCloudPlatform#511)
1 parent d4c21f3 commit 381b81b

File tree

7 files changed

+886
-0
lines changed

7 files changed

+886
-0
lines changed

pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@
3737

3838
<maven.compiler.target>1.8</maven.compiler.target>
3939
<maven.compiler.source>1.8</maven.compiler.source>
40+
41+
<spanner.test.instance>default-instance</spanner.test.instance>
42+
<spanner.sample.database>mysample</spanner.sample.database>
43+
<spanner.quickstart.database>quickstart-db</spanner.quickstart.database>
4044
</properties>
4145

4246
<prerequisites>
@@ -112,6 +116,7 @@
112116
<module>monitoring/v2</module>
113117
<module>monitoring/v3</module>
114118
<module>pubsub/cloud-client</module>
119+
<module>spanner/cloud-client</module>
115120
<module>speech/grpc</module>
116121
<module>storage/cloud-client</module>
117122
<module>storage/json-api</module>

spanner/cloud-client/README.md

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Getting Started with Cloud Spanner and the Google Cloud Client libraries
2+
3+
[Cloud Spanner][Spanner] is a horizontally-scalable database-as-a-service
4+
with transactions and SQL support.
5+
These sample Java applications demonstrate how to access the Spanner API using
6+
the [Google Cloud Client Library for Java][google-cloud-java].
7+
8+
[Spanner]: https://cloud.google.com/spanner/
9+
[google-cloud-java]: https://github.com/GoogleCloudPlatform/google-cloud-java
10+
11+
## Quickstart
12+
13+
Install [Maven](http://maven.apache.org/).
14+
15+
Build your project with:
16+
17+
mvn clean package -DskipTests
18+
19+
You can then run a given `ClassName` via:
20+
21+
mvn exec:java -Dexec.mainClass=com.example.spanner.ClassName \
22+
-DpropertyName=propertyValue \
23+
-Dexec.args="any arguments to the app"
24+
25+
### Running a simple query (using the quickstart sample)
26+
27+
mvn exec:java -Dexec.mainClass=com.example.spanner.QuickstartSample -Dexec.args="my-instance my-database"
28+
29+
## Tutorial
30+
31+
### Running the tutorial
32+
mvn exec:java -Dexec.mainClass=com.example.spanner.SpannerSample -Dexec.args="<command> my-instance my-database"
33+
34+
## Test
35+
mvn verify -Dspanner.test.instance=<instance id> -Dspanner.sample.database=<new database id> -Dspanner.quickstart.database=<existing database id>

spanner/cloud-client/pom.xml

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<!--
2+
Copyright 2017 Google Inc.
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+
<project>
17+
<modelVersion>4.0.0</modelVersion>
18+
<groupId>come.example.spanner</groupId>
19+
<artifactId>spanner-google-cloud-samples</artifactId>
20+
<packaging>jar</packaging>
21+
22+
<!-- Parent defines config for testing & linting. -->
23+
<parent>
24+
<artifactId>doc-samples</artifactId>
25+
<groupId>com.google.cloud</groupId>
26+
<version>1.0.0</version>
27+
<relativePath>../..</relativePath>
28+
</parent>
29+
30+
<properties>
31+
<maven.compiler.target>1.8</maven.compiler.target>
32+
<maven.compiler.source>1.8</maven.compiler.source>
33+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
34+
</properties>
35+
36+
<dependencies>
37+
<dependency>
38+
<groupId>com.google.cloud</groupId>
39+
<artifactId>google-cloud-spanner</artifactId>
40+
<version>0.9.2-beta</version>
41+
<exclusions>
42+
<exclusion> <!-- exclude an old version of Guava -->
43+
<groupId>com.google.guava</groupId>
44+
<artifactId>guava-jdk5</artifactId>
45+
</exclusion>
46+
</exclusions>
47+
</dependency>
48+
<dependency>
49+
<groupId>io.netty</groupId>
50+
<artifactId>netty-tcnative-boringssl-static</artifactId>
51+
<version>1.1.33.Fork23</version>
52+
<scope>runtime</scope>
53+
</dependency>
54+
<dependency>
55+
<groupId>com.google.guava</groupId>
56+
<artifactId>guava</artifactId>
57+
<version>20.0</version>
58+
</dependency>
59+
<!-- Test dependencies -->
60+
<dependency>
61+
<groupId>junit</groupId>
62+
<artifactId>junit</artifactId>
63+
<version>4.12</version>
64+
<scope>test</scope>
65+
</dependency>
66+
<dependency>
67+
<groupId>com.google.truth</groupId>
68+
<artifactId>truth</artifactId>
69+
<version>0.31</version>
70+
<scope>test</scope>
71+
</dependency>
72+
</dependencies>
73+
<build>
74+
<plugins>
75+
<plugin>
76+
<artifactId>maven-assembly-plugin</artifactId>
77+
<version>2.5.5</version>
78+
<configuration>
79+
<descriptorRefs>
80+
<descriptorRef>jar-with-dependencies</descriptorRef>
81+
</descriptorRefs>
82+
<archive>
83+
<manifest>
84+
<mainClass>com.example.spanner.SpannerSample</mainClass>
85+
</manifest>
86+
</archive>
87+
</configuration>
88+
<executions>
89+
<execution>
90+
<id>make-assembly</id>
91+
<phase>package</phase>
92+
<goals>
93+
<goal>single</goal>
94+
</goals>
95+
</execution>
96+
</executions>
97+
</plugin>
98+
</plugins>
99+
</build>
100+
101+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
Copyright 2017, Google, Inc.
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 com.example.spanner;
18+
19+
// [START spanner_quickstart]
20+
// Imports the Google Cloud client library
21+
import com.google.cloud.spanner.DatabaseClient;
22+
import com.google.cloud.spanner.DatabaseId;
23+
import com.google.cloud.spanner.ResultSet;
24+
import com.google.cloud.spanner.Spanner;
25+
import com.google.cloud.spanner.SpannerOptions;
26+
import com.google.cloud.spanner.Statement;
27+
28+
/**
29+
* A quick start code for Cloud Spanner. It demonstrates how to setup the Cloud Spanner client and
30+
* execute a simple query using it against an existing database.
31+
*/
32+
public class QuickstartSample {
33+
public static void main(String... args) throws Exception {
34+
35+
if (args.length != 2) {
36+
System.err.println("Usage: QuickStartSample <instance_id> <database_id>");
37+
return;
38+
}
39+
// Instantiates a client
40+
SpannerOptions options = SpannerOptions.newBuilder().build();
41+
Spanner spanner = options.getService();
42+
43+
// Name of your database. Eg: projects/my-project/instances/instanceId/databases/databaseId
44+
String instanceId = args[0];
45+
String databaseId = args[1];
46+
try {
47+
// Creates a database client
48+
DatabaseClient dbClient = spanner.getDatabaseClient(DatabaseId.of(
49+
options.getProjectId(), instanceId, databaseId));
50+
// Queries the database
51+
ResultSet resultSet = dbClient.singleUse().executeQuery(Statement.of("SELECT 1"));
52+
53+
System.out.println("\n\nResults:");
54+
// Prints the results
55+
while (resultSet.next()) {
56+
System.out.printf("%d\n\n", resultSet.getLong(0));
57+
}
58+
} finally {
59+
// Closes the client which will free up the resources used
60+
spanner.closeAsync().get();
61+
}
62+
}
63+
}
64+
// [END spanner_quickstart]

0 commit comments

Comments
 (0)