|
| 1 | +# BigTable Sample Application with Native Image |
| 2 | + |
| 3 | +This application uses the [Google Cloud BigTable Client Libraries](https://cloud.google.com/bigtable/docs/reference/libraries) and is compatible with Native Image compilation. |
| 4 | + |
| 5 | +The application runs through some simple BigTable Client Library operations to demonstrate compatibility. |
| 6 | + |
| 7 | +## Setup Instructions |
| 8 | + |
| 9 | +You will need to follow these prerequisite steps in order to run the samples: |
| 10 | + |
| 11 | +1. If you have not already, [create a Google Cloud Platform Project](https://cloud.google.com/resource-manager/docs/creating-managing-projects#creating_a_project). |
| 12 | + |
| 13 | +2. Install the [Google Cloud SDK](https://cloud.google.com/sdk/) which will allow you to run the sample with your project's credentials. |
| 14 | + |
| 15 | + Once installed, log in with Application Default Credentials using the following command: |
| 16 | + |
| 17 | + ``` |
| 18 | + gcloud auth application-default login |
| 19 | + ``` |
| 20 | +
|
| 21 | + **Note:** Authenticating with Application Default Credentials is convenient to use during development, but we recommend [alternate methods of authentication](https://cloud.google.com/docs/authentication/production) during production use. |
| 22 | +
|
| 23 | +3. Install the GraalVM compiler. |
| 24 | +
|
| 25 | + You can follow the [official installation instructions](https://www.graalvm.org/docs/getting-started/#install-graalvm) from the GraalVM website. |
| 26 | + After following the instructions, ensure that you install the native image extension installed by running: |
| 27 | +
|
| 28 | + ``` |
| 29 | + gu install native-image |
| 30 | + ``` |
| 31 | +
|
| 32 | + 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. |
| 33 | +
|
| 34 | + You will see something similar to the below output: |
| 35 | +
|
| 36 | + ``` |
| 37 | + $ java -version |
| 38 | + |
| 39 | + openjdk version "11.0.7" 2020-04-14 |
| 40 | + OpenJDK Runtime Environment GraalVM CE 20.1.0 (build 11.0.7+10-jvmci-20.1-b02) |
| 41 | + OpenJDK 64-Bit Server VM GraalVM CE 20.1.0 (build 11.0.7+10-jvmci-20.1-b02, mixed mode, sharing) |
| 42 | + ``` |
| 43 | + |
| 44 | +## BigTable Environment setup |
| 45 | +The following sections describe how you can run the sample application against the BigTable emulator or a real BigTable instance. |
| 46 | +
|
| 47 | +1. *(Using emulator)* If you wish to run the application against the [BigTable emulator](https://cloud.google.com/bigtable/docs/emulator), ensure that you have the [Google Cloud SDK](https://cloud.google.com/sdk) installed. |
| 48 | +
|
| 49 | + In a new terminal window, start the emulator via `gcloud`: |
| 50 | +
|
| 51 | + ``` |
| 52 | + gcloud beta emulators bigtable start --host-port=localhost:9010 |
| 53 | + ``` |
| 54 | +
|
| 55 | + Leave the emulator running in this terminal for now. |
| 56 | + In the next section, we will run the sample application against the BigTable emulator instance. |
| 57 | +
|
| 58 | +2. *(Using real BigTable instance)* If instead you wish to run the application against a real BigTable instance, ensure you already have a BigTable instance created. |
| 59 | +
|
| 60 | + For example, the following command creates a new BigTable instance named `nativeimage-test-instance`. |
| 61 | +
|
| 62 | + ``` |
| 63 | + gcloud bigtable instances create nativeimage-test-instance \ |
| 64 | + --cluster=nativeimage-test-cluster \ |
| 65 | + --cluster-zone=us-central1-c \ |
| 66 | + --cluster-num-nodes=1 \ |
| 67 | + --display-name=nativeimage-test-instance |
| 68 | + ``` |
| 69 | +
|
| 70 | + You can also manually manage your BigTable resources through the [BigTable Cloud Console view](http://console.cloud.google.com/bigtable). |
| 71 | +
|
| 72 | +## Run with Native Image Compilation |
| 73 | +
|
| 74 | +1. Compile the application with the Native Image compiler. |
| 75 | +
|
| 76 | + ``` |
| 77 | + mvn package -P native -DskipTests |
| 78 | + ``` |
| 79 | +
|
| 80 | +2. **(Optional)** If you're using the emulator, export the `BIGTABLE_EMULATOR_HOST` as an environment variable in your terminal. |
| 81 | +
|
| 82 | + ``` |
| 83 | + export BIGTABLE_EMULATOR_HOST=localhost:9010 |
| 84 | + ``` |
| 85 | +
|
| 86 | + The BigTable Client Libraries will detect this environment variable and automatically connect to the emulator instance if this variable is set. |
| 87 | +
|
| 88 | +3. Run the application. |
| 89 | + Pass in the BigTable instance you wish to use via the `-Dbigtable.instance` property. |
| 90 | +
|
| 91 | + ``` |
| 92 | + ./target/bigtable-sample -Dbigtable.instance={BIGTABLE_INSTANCE_NAME} |
| 93 | + ``` |
| 94 | +
|
| 95 | +4. The application will run through some basic BigTable operations and log some output statements. |
| 96 | +
|
| 97 | + ``` |
| 98 | + Created table: nativeimage-test-table2b5b0031-f4ea-4c39-bc0c-bf6c3c62c90c |
| 99 | + Successfully wrote row: phone#1608775178843000 |
| 100 | + Reading phone data in table: |
| 101 | + Key: phone#1608775178843000 |
| 102 | + connected_cell: @1608775178843000 |
| 103 | + connected_wifi: @1608775178843000 |
| 104 | + os_build: PQ2A.190405.003 @1608775178843000 |
| 105 | + Deleted table: nativeimage-test-table2b5b0031-f4ea-4c39-bc0c-bf6c3c62c90c |
| 106 | + ``` |
| 107 | +## Run integration test for the sample |
| 108 | +
|
| 109 | +In order to run the sample's integration test, call the following command: |
| 110 | +
|
| 111 | + ``` |
| 112 | + mvn test -P native |
| 113 | + ``` |
0 commit comments