This sample demonstrates how to use Cloud Spanner from the App Engine Standard with Java 11 environment.
Please feel free to report issues and send pull requests, but note that this application is not officially supported as part of the Cloud Spanner product.
If you haven't already created a project, create one now.
- Open the Cloud Console.
- In the drop-down menu at the top, select Create a project.
- Give your project a name.
- Make a note of the project ID, which might be different from the project name. The project ID is used in commands below.
If you like, you can activate a Cloud Shell for your project now. When using Cloud Shell, the Google Cloud SDK and Maven in the steps below will already be installed.
Install the Google Cloud SDK if it's not already available in your enviromment. Then run:
gcloud init
If this is your first time creating an App engine application:
gcloud app create
This sample uses the Apache Maven build system. If Maven isn't installed in your environment yet, download and install it. When you use Maven as described here, it will automatically download the required client libraries.
To switch to Open JDK 11 in a Cloud Shell session, run:
# Select the usr/lib/jvm/java-11-openjdk-amd64/bin/java version.
sudo update-alternatives --config java
# Set the JAVA_HOME variable for Maven to pick the correct JDK:
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
Clone the repository:
git clone https://github.com/cloudspannerecosystem/appengine-java-sample
Compile the simple-jetty-main Main
class:
cd appengine-java-sample/simple-jetty-main
mvn install
If this fails with an error about the Java target version, make sure you've switched to the Open JDK 11 as described above.
Change to the appengine-java-sample
directory:
cd ..
Next, create a Spanner instance
and update the <SPANNER_INSTANCE>
value in
app.yaml
with your instance ID.
Replace <PROJECT_ID>
below with your project ID:
mvn package appengine:deploy -Dapp.deploy.projectId=<PROJECT_ID>
/spanner
: will run a number of sample operations against the Spanner instance.
Individual tasks can be run using the task
query parameter.
See SpannerTasks
for the supported set of tasks.
To execute the endpoint handler of the deployed sample application, open
https://<PROJECT_ID>.appspot.com/spanner
in a web browser.
Note: by default all the Spanner example operations run in order, so this endpoint handler may take a while to return.