Skip to content

Commit 118cf6e

Browse files
authored
Add Spanner Leaderboard sample. (GoogleCloudPlatform#1344)
* Add Spanner Leaderboard sample. * Address review comments. * Address review comments. * Fix typo.
1 parent 7f8aad0 commit 118cf6e

File tree

10 files changed

+1641
-0
lines changed

10 files changed

+1641
-0
lines changed

spanner/leaderboard/README.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Java Cloud Spanner Sample Leaderboard Application
2+
3+
A leaderboard sample that uses the Cloud Spanner commit timestamp feature and demonstrates
4+
how to call the [Google Cloud Spanner API](https://cloud.google.com/spanner/docs/)
5+
using the [Google Cloud Client Library for Java](https://github.com/GoogleCloudPlatform/google-cloud-java).
6+
7+
This sample requires [Java](https://www.java.com/en/download/) and [Maven](http://maven.apache.org/) for building the application.
8+
9+
This sample includes extra directories `step5`, `step6`, and `step7` that contain partial versions of this sample application. These directories are intended to provide guidance as part of a separate Codelab walk-through where the application is built in the following stages
10+
that correspond to the steps in Codelab:
11+
12+
* step5 - Create the sample database along with the tables Players and Scores.
13+
* step6 - Populate the Players and Scores tables with sample data.
14+
* step7 - Run sample queries including sorting the results by timestamp.
15+
16+
If you only want to run the complete sample refer to the application in the `complete` directory.
17+
18+
19+
## Build and Run
20+
21+
1. **Follow the set-up instructions in [the documentation](https://cloud.google.com/java/docs/setup).**
22+
23+
2. Enable APIs for your project.
24+
[Click here](https://console.cloud.google.com/flows/enableapi?apiid=spanner.googleapis.com&showconfirmation=true)
25+
to visit Cloud Platform Console and enable the Google Cloud Spanner API.
26+
27+
3. Create a Cloud Spanner instance via the Cloud Plaform Console's
28+
[Cloud Spanner section](http://console.cloud.google.com/spanner).
29+
30+
4. In a terminal shell, change directory to the version of the application you want to run:
31+
```
32+
cd complete
33+
```
34+
35+
5. Run the following Maven command to build the application:
36+
```
37+
mvn install -DskipTests
38+
```
39+
40+
6. Change directory into the `target` directory where the application's jar file gets built to.
41+
```
42+
cd target
43+
```
44+
45+
46+
7. Run the Spanner Leaderboard sample with `java -jar leaderboard.jar` to see a list of available commands:
47+
```
48+
@shell:~/.../target$ java -jar leaderboard.jar
49+
50+
Leaderboard 1.0.0
51+
Usage:
52+
java -jar leaderboard.jar <command> <instance_id> <database_id> [command_option]
53+
54+
Examples:
55+
java -jar leaderboard.jar create my-instance example-db
56+
- Create a sample Cloud Spanner database along with sample tables in your project.
57+
58+
java -jar leaderboard.jar insert my-instance example-db players
59+
- Insert 100 sample Player records into the database.
60+
61+
java -jar leaderboard.jar insert my-instance example-db scores
62+
- Insert sample score data into Scores sample Cloud Spanner database table.
63+
64+
java -jar leaderboard.jar query my-instance example-db
65+
- Query players with top ten scores of all time.
66+
67+
java -jar leaderboard.jar query my-instance example-db 168
68+
- Query players with top ten scores within a timespan specified in hours.
69+
70+
java -jar leaderboard.jar delete my-instance example-db
71+
- Delete sample Cloud Spanner database.
72+
```
73+
74+
```
75+
$ java -jar leaderboard.jar create my-instance my-database
76+
Created database [projects/arc-nl/instances/my-instance/databases/my-database]
77+
```

spanner/leaderboard/complete/pom.xml

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.google.codelabs</groupId>
8+
<artifactId>leaderboard</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<name>leaderboard</name>
12+
<!-- FIXME change it to the project's website -->
13+
<url>http://www.example.com</url>
14+
15+
<properties>
16+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
17+
<maven.compiler.source>1.8</maven.compiler.source>
18+
<maven.compiler.target>1.8</maven.compiler.target>
19+
</properties>
20+
21+
<!--
22+
The parent pom defines common style checks and testing strategies for our samples.
23+
Removing or replacing it should not affect the execution of the samples in anyway.
24+
-->
25+
<parent>
26+
<groupId>com.google.cloud.samples</groupId>
27+
<artifactId>shared-configuration</artifactId>
28+
<version>1.0.10</version>
29+
</parent>
30+
31+
<dependencyManagement>
32+
<dependencies>
33+
<dependency>
34+
<groupId>com.google.cloud</groupId>
35+
<artifactId>google-cloud-bom</artifactId>
36+
<version>0.73.0-alpha</version>
37+
<type>pom</type>
38+
<scope>import</scope>
39+
</dependency>
40+
</dependencies>
41+
</dependencyManagement>
42+
<dependencies>
43+
<dependency>
44+
<!-- Version auto-managed by BOM -->
45+
<groupId>com.google.cloud</groupId>
46+
<artifactId>google-cloud-spanner</artifactId>
47+
</dependency>
48+
<dependency>
49+
<groupId>junit</groupId>
50+
<artifactId>junit</artifactId>
51+
<version>4.13-beta-2</version>
52+
<scope>test</scope>
53+
</dependency>
54+
<dependency>
55+
<groupId>com.google.truth</groupId>
56+
<artifactId>truth</artifactId>
57+
<version>0.42</version>
58+
<scope>test</scope>
59+
</dependency>
60+
</dependencies>
61+
62+
<build>
63+
<plugins>
64+
<plugin>
65+
<artifactId>maven-assembly-plugin</artifactId>
66+
<version>2.5.5</version>
67+
<configuration>
68+
<finalName>leaderboard</finalName>
69+
<descriptorRefs>
70+
<descriptorRef>jar-with-dependencies</descriptorRef>
71+
</descriptorRefs>
72+
<archive>
73+
<manifest>
74+
<mainClass>com.google.codelabs.App</mainClass>
75+
</manifest>
76+
</archive>
77+
<appendAssemblyId>false</appendAssemblyId>
78+
<attach>false</attach>
79+
</configuration>
80+
<executions>
81+
<execution>
82+
<id>make-assembly</id>
83+
<phase>package</phase>
84+
<goals>
85+
<goal>single</goal>
86+
</goals>
87+
</execution>
88+
</executions>
89+
</plugin>
90+
<plugin>
91+
<groupId>org.apache.maven.plugins</groupId>
92+
<artifactId>maven-failsafe-plugin</artifactId>
93+
<version>3.0.0-M3</version>
94+
</plugin>
95+
<plugin>
96+
<groupId>org.apache.maven.plugins</groupId>
97+
<artifactId>maven-surefire-plugin</artifactId>
98+
<version>3.0.0-M3</version>
99+
<configuration>
100+
<useSystemClassLoader>false</useSystemClassLoader>
101+
</configuration>
102+
</plugin>
103+
</plugins>
104+
</build>
105+
</project>

0 commit comments

Comments
 (0)