|
| 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 | + ``` |
0 commit comments