Skip to content

Add Spanner Leaderboard sample. #1344

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 6, 2019
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
77 changes: 77 additions & 0 deletions spanner/leaderboard/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Java Cloud Spanner Sample Leaderboard Application

A leaderboard sample that uses the Cloud Spanner commit timestamp feature and demonstrates
how to call the [Google Cloud Spanner API](https://cloud.google.com/spanner/docs/)
using the [Google Cloud Client Library for Java](https://github.com/GoogleCloudPlatform/google-cloud-java).

This sample requires [Java](https://www.java.com/en/download/) and [Maven](http://maven.apache.org/) for building the application.

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
that correspond to the steps in Codelab:

* step5 - Create the sample database along with the tables Players and Scores.
* step6 - Populate the Players and Scores tables with sample data.
* step7 - Run sample queries including sorting the results by timestamp.

If you only want to run the complete sample refer to the application in the `complete` directory.


## Build and Run

1. **Follow the set-up instructions in [the documentation](https://cloud.google.com/java/docs/setup).**

2. Enable APIs for your project.
[Click here](https://console.cloud.google.com/flows/enableapi?apiid=spanner.googleapis.com&showconfirmation=true)
to visit Cloud Platform Console and enable the Google Cloud Spanner API.

3. Create a Cloud Spanner instance via the Cloud Plaform Console's
[Cloud Spanner section](http://console.cloud.google.com/spanner).

4. In a terminal shell, change directory to the version of the application you want to run:
```
cd complete
```

5. Run the following Maven command to build the application:
```
mvn install -DskipTests
```

6. Change directory into the `target` directory where the application's jar file gets built to.
```
cd target
```


7. Run the Spanner Leaderboard sample with `java -jar leaderboard.jar` to see a list of available commands:
```
@shell:~/.../target$ java -jar leaderboard.jar

Leaderboard 1.0.0
Usage:
java -jar leaderboard.jar <command> <instance_id> <database_id> [command_option]

Examples:
java -jar leaderboard.jar create my-instance example-db
- Create a sample Cloud Spanner database along with sample tables in your project.

java -jar leaderboard.jar insert my-instance example-db players
- Insert 100 sample Player records into the database.

java -jar leaderboard.jar insert my-instance example-db scores
- Insert sample score data into Scores sample Cloud Spanner database table.

java -jar leaderboard.jar query my-instance example-db
- Query players with top ten scores of all time.

java -jar leaderboard.jar query my-instance example-db 168
- Query players with top ten scores within a timespan specified in hours.

java -jar leaderboard.jar delete my-instance example-db
- Delete sample Cloud Spanner database.
```

```
$ java -jar leaderboard.jar create my-instance my-database
Created database [projects/arc-nl/instances/my-instance/databases/my-database]
```
105 changes: 105 additions & 0 deletions spanner/leaderboard/complete/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?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>

<groupId>com.google.codelabs</groupId>
<artifactId>leaderboard</artifactId>
<version>1.0-SNAPSHOT</version>

<name>leaderboard</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>

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

<!--
The parent pom defines common style checks and testing strategies for our samples.
Removing or replacing it should not affect the execution of the samples in anyway.
-->
<parent>
<groupId>com.google.cloud.samples</groupId>
<artifactId>shared-configuration</artifactId>
<version>1.0.10</version>
</parent>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-bom</artifactId>
<version>0.73.0-alpha</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<!-- Version auto-managed by BOM -->
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-spanner</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13-beta-2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
<version>0.42</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>
<configuration>
<finalName>leaderboard</finalName>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.google.codelabs.App</mainClass>
</manifest>
</archive>
<appendAssemblyId>false</appendAssemblyId>
<attach>false</attach>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.0.0-M3</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin>
</plugins>
</build>
</project>
Loading