Skip to content

Commit 143aaee

Browse files
docs(sample): Add sample for native image support in Bigtable (#1165)
* docs(sample): add sample for native image support * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * add setup instructions to readme Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 8d2ca86 commit 143aaee

File tree

6 files changed

+524
-0
lines changed

6 files changed

+524
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-bigtable/tree
472472

473473
| Sample | Source Code | Try it |
474474
| --------------------------- | --------------------------------- | ------ |
475+
| Native Image Bigtable Sample | [source code](https://github.com/googleapis/java-bigtable/blob/main/samples/native-image-sample/src/main/java/com/example/bigtable/NativeImageBigtableSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigtable&page=editor&open_in_editor=samples/native-image-sample/src/main/java/com/example/bigtable/NativeImageBigtableSample.java) |
475476
| Configure Connection Pool | [source code](https://github.com/googleapis/java-bigtable/blob/main/samples/snippets/src/main/java/com/example/bigtable/ConfigureConnectionPool.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigtable&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigtable/ConfigureConnectionPool.java) |
476477
| Filters | [source code](https://github.com/googleapis/java-bigtable/blob/main/samples/snippets/src/main/java/com/example/bigtable/Filters.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigtable&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigtable/Filters.java) |
477478
| Hello World | [source code](https://github.com/googleapis/java-bigtable/blob/main/samples/snippets/src/main/java/com/example/bigtable/HelloWorld.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigtable&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigtable/HelloWorld.java) |

samples/native-image-sample/README.md

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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+
```

samples/native-image-sample/pom.xml

+139
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.example.bigtable</groupId>
5+
<artifactId>native-image-sample</artifactId>
6+
<name>Native Image Sample</name>
7+
<url>https://github.com/googleapis/java-bigtable</url>
8+
9+
<!--
10+
The parent pom defines common style checks and testing strategies for our samples.
11+
Removing or replacing it should not affect the execution of the samples in anyway.
12+
-->
13+
<parent>
14+
<groupId>com.google.cloud.samples</groupId>
15+
<artifactId>shared-configuration</artifactId>
16+
<version>1.2.0</version>
17+
</parent>
18+
19+
<properties>
20+
<maven.compiler.target>1.8</maven.compiler.target>
21+
<maven.compiler.source>1.8</maven.compiler.source>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
</properties>
24+
25+
26+
<!-- [START bigtable_install_with_bom] -->
27+
<dependencyManagement>
28+
<dependencies>
29+
<dependency>
30+
<groupId>com.google.cloud</groupId>
31+
<artifactId>libraries-bom</artifactId>
32+
<version>24.2.0</version>
33+
<type>pom</type>
34+
<scope>import</scope>
35+
</dependency>
36+
</dependencies>
37+
</dependencyManagement>
38+
39+
<dependencies>
40+
<dependency>
41+
<groupId>com.google.cloud</groupId>
42+
<artifactId>google-cloud-bigtable</artifactId>
43+
</dependency>
44+
<!-- [END bigtable_install_with_bom] -->
45+
46+
<dependency>
47+
<groupId>junit</groupId>
48+
<artifactId>junit</artifactId>
49+
<version>4.13.2</version>
50+
<scope>test</scope>
51+
</dependency>
52+
<dependency>
53+
<groupId>com.google.truth</groupId>
54+
<artifactId>truth</artifactId>
55+
<version>1.1.3</version>
56+
<scope>test</scope>
57+
</dependency>
58+
</dependencies>
59+
60+
<build>
61+
<plugins>
62+
<plugin>
63+
<groupId>org.apache.maven.plugins</groupId>
64+
<artifactId>maven-jar-plugin</artifactId>
65+
<configuration>
66+
<archive>
67+
<manifest>
68+
<mainClass>com.example.bigquery.NativeImageBigtableSample
69+
</mainClass>
70+
</manifest>
71+
</archive>
72+
</configuration>
73+
</plugin>
74+
</plugins>
75+
</build>
76+
77+
<!-- Native Profile-->
78+
<profiles>
79+
<profile>
80+
<id>native</id>
81+
82+
<dependencies>
83+
<dependency>
84+
<groupId>com.google.cloud</groupId>
85+
<artifactId>native-image-support</artifactId>
86+
<version>0.10.0</version>
87+
</dependency>
88+
<dependency>
89+
<groupId>org.junit.vintage</groupId>
90+
<artifactId>junit-vintage-engine</artifactId>
91+
<version>5.8.2</version>
92+
<scope>test</scope>
93+
</dependency>
94+
<dependency>
95+
<groupId>org.graalvm.buildtools</groupId>
96+
<artifactId>junit-platform-native</artifactId>
97+
<version>0.9.9</version>
98+
<scope>test</scope>
99+
</dependency>
100+
</dependencies>
101+
102+
<build>
103+
<plugins>
104+
<plugin>
105+
<groupId>org.graalvm.buildtools</groupId>
106+
<artifactId>native-maven-plugin</artifactId>
107+
<version>0.9.9</version>
108+
<extensions>true</extensions>
109+
<configuration>
110+
<mainClass>com.example.bigtable.NativeImageBigtableSample
111+
</mainClass>
112+
<buildArgs>
113+
<buildArg>--no-fallback</buildArg>
114+
<buildArg>--no-server</buildArg>
115+
</buildArgs>
116+
</configuration>
117+
<executions>
118+
<execution>
119+
<id>build-native</id>
120+
<goals>
121+
<goal>build</goal>
122+
<goal>test</goal>
123+
</goals>
124+
<phase>package</phase>
125+
</execution>
126+
<execution>
127+
<id>test-native</id>
128+
<goals>
129+
<goal>test</goal>
130+
</goals>
131+
<phase>test</phase>
132+
</execution>
133+
</executions>
134+
</plugin>
135+
</plugins>
136+
</build>
137+
</profile>
138+
</profiles>
139+
</project>

0 commit comments

Comments
 (0)