This is a sample application which uses the Cloud Spanner client libraries and demonstrates compatibility with Native Image compilation.
The application creates a new Spanner instance and database, and it runs basic operations including queries and Spanner mutations.
You will need to follow these prerequisite steps in order to run these samples:
-
If you have not already, create a Google Cloud Platform Project.
-
Install the Google Cloud SDK which will allow you to run the sample with your project's credentials.
Once installed, log in with Application Default Credentials using the following command:
gcloud auth application-default login
Note: Authenticating with Application Default Credentials is convenient to use during development, but we recommend alternate methods of authentication during production use.
-
Install the GraalVM compiler.
You can follow the official installation instructions from the GraalVM website. After following the instructions, ensure that you install the Native Image extension installed by running:
gu install native-image
Once you finish following the instructions, verify that the default version of Java is set to the GraalVM version by running
java -version
in a terminal.You will see something similar to the below output:
$ java -version openjdk version "11.0.15" 2022-04-19 OpenJDK Runtime Environment GraalVM CE 22.1.0 (build 11.0.15+10-jvmci-22.1-b06) OpenJDK 64-Bit Server VM GraalVM CE 22.1.0 (build 11.0.15+10-jvmci-22.1-b06, mixed mode, sharing)
-
(Optional) If you wish to run the application against the Spanner emulator, make sure that you have the Google Cloud SDK installed.
In a new terminal window, start the emulator via
gcloud
:gcloud beta emulators spanner start
You may leave the emulator running for now. In the next section, we will run the sample application against the Spanner emulator instsance.
-
Navigate to this directory and compile the application with the Native Image compiler.
mvn package -P native -DskipTests
-
(Optional) If you're using the emulator, export the
SPANNER_EMULATOR_HOST
as an environment variable in your terminal.export SPANNER_EMULATOR_HOST=localhost:9010
The Spanner Client Libraries will detect this environment variable and will automatically connect to the emulator instance if this variable is set.
-
Run the application.
./target/native-image
-
The application will run through some basic Spanner operations and log some output statements.
Running the Spanner Sample. Singers Registered in Spanner: Bob Loblaw Virginia Watson
In order to run the sample integration test as a native image, call the following command:
mvn test -Pnative