A leaderboard sample that uses the Cloud Spanner commit timestamp feature and demonstrates how to call the Google Cloud Spanner API using the Google Cloud Client Library for Java.
This sample requires Java and Maven for building the application.
This sample includes extra directories step4
, step5
, and step6
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:
- step4 - Create the sample database along with the tables Players and Scores.
- step5 - Populate the Players and Scores tables with sample data.
- step6 - 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.
-
Follow the set-up instructions in the documentation.
-
Enable APIs for your project. Click here to visit Cloud Platform Console and enable the Google Cloud Spanner API.
-
Create a Cloud Spanner instance via the Cloud Plaform Console's Cloud Spanner section.
-
In a terminal shell, change directory to the version of the application you want to run:
cd complete
-
Run the following Maven command to build the application:
mvn install -DskipTests
-
Change directory into the
target
directory where the application's jar file gets built to.cd target
-
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]