Skip to content

Commit a3ca085

Browse files
trollyxiamutianf
andauthored
samples: Add SchemaBundle admin examples (#2690)
* samples: Add SchemaBundle admin examples Change-Id: Ib0cd4ae382dac1984d5f6aaf14ea1d22c4ef5a69 * misc: Address review comments Change-Id: I7fa8e047bc3485509742545f88143782476b2f02 * misc: Move around proto files to follow java convention and change packaging config Change-Id: I1d7befa83ce79e92ec50458e137c243372574a40 * sample: Fix PROTO_FILE_PATH in SchemaBundleExample Change-Id: I9699979a595d77f53e53f619a86b6d03b29512a5 --------- Co-authored-by: Mattie Fu <[email protected]>
1 parent a3c02fe commit a3ca085

File tree

9 files changed

+1585
-3
lines changed

9 files changed

+1585
-3
lines changed

samples/install-without-bom/pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
<dependency>
3030
<groupId>com.google.cloud</groupId>
3131
<artifactId>google-cloud-bigtable</artifactId>
32-
<version>2.50.0</version>
32+
<version>2.67.0</version>
3333
</dependency>
3434
<!-- [END bigtable_install_without_bom] -->
3535

@@ -49,6 +49,11 @@
4949

5050
<!-- compile and run all snippet tests -->
5151
<build>
52+
<resources>
53+
<resource>
54+
<directory>../snippets/src/main/resources</directory>
55+
</resource>
56+
</resources>
5257
<plugins>
5358
<plugin>
5459
<groupId>org.codehaus.mojo</groupId>

samples/snapshot/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@
4848

4949
<!-- compile and run all snippet tests -->
5050
<build>
51+
<resources>
52+
<resource>
53+
<directory>../snippets/src/main/resources</directory>
54+
</resource>
55+
</resources>
5156
<plugins>
5257
<plugin>
5358
<groupId>org.codehaus.mojo</groupId>

samples/snippets/pom.xml

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?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">
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
35
<modelVersion>4.0.0</modelVersion>
46
<groupId>com.google.cloud</groupId>
57
<artifactId>google-cloud-bigtable-snippets</artifactId>
@@ -29,7 +31,7 @@
2931
<dependency>
3032
<groupId>com.google.cloud</groupId>
3133
<artifactId>libraries-bom</artifactId>
32-
<version>26.50.0</version>
34+
<version>26.69.0</version>
3335
<type>pom</type>
3436
<scope>import</scope>
3537
</dependency>
@@ -56,4 +58,18 @@
5658
<scope>test</scope>
5759
</dependency>
5860
</dependencies>
61+
62+
<build>
63+
<plugins>
64+
<plugin>
65+
<groupId>org.apache.maven.plugins</groupId>
66+
<artifactId>maven-checkstyle-plugin</artifactId>
67+
<version>3.3.1</version>
68+
<configuration>
69+
<!-- exclude protoc generated file from checkstyle validation -->
70+
<excludes>**/SingerProto.java</excludes>
71+
</configuration>
72+
</plugin>
73+
</plugins>
74+
</build>
5975
</project>
Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
*http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.bigtable;
18+
19+
import com.google.api.gax.rpc.NotFoundException;
20+
import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient;
21+
import com.google.cloud.bigtable.admin.v2.BigtableTableAdminSettings;
22+
import com.google.cloud.bigtable.admin.v2.models.CreateSchemaBundleRequest;
23+
import com.google.cloud.bigtable.admin.v2.models.CreateTableRequest;
24+
import com.google.cloud.bigtable.admin.v2.models.SchemaBundle;
25+
import com.google.cloud.bigtable.admin.v2.models.Table;
26+
import com.google.cloud.bigtable.admin.v2.models.UpdateSchemaBundleRequest;
27+
import com.google.protobuf.ByteString;
28+
import com.google.protobuf.DescriptorProtos;
29+
import com.google.protobuf.InvalidProtocolBufferException;
30+
import java.io.IOException;
31+
import java.io.InputStream;
32+
import java.util.ArrayList;
33+
import java.util.List;
34+
35+
/**
36+
* This example demonstrates the usage of BigtableTableAdminClient to create, configure and delete a
37+
* Cloud Bigtable schema bundle.
38+
*
39+
* <p>The example follows these steps:
40+
*
41+
* <ol>
42+
* <li>Creates a Bigtable table.
43+
* <li>Creates a schema bundle.
44+
* <li>Updates a schema bundle.
45+
* <li>Gets the schema bundle.
46+
* <li>Lists all schema bundles for the table.
47+
* <li>Deletes the schema bundle.
48+
* <li>Deletes the table.
49+
* </ol>
50+
*/
51+
public class SchemaBundleExample {
52+
53+
private static final String COLUMN_FAMILY = "cf";
54+
private static final String PROTO_FILE_PATH = "com/example/bigtable/descriptors.pb";
55+
private final String tableId;
56+
private final String schemaBundleId;
57+
private final BigtableTableAdminClient adminClient;
58+
59+
public static void main(String[] args) throws IOException {
60+
if (args.length != 2) {
61+
System.out.println("Missing required project id or instance id");
62+
return;
63+
}
64+
String projectId = args[0];
65+
String instanceId = args[1];
66+
67+
SchemaBundleExample example =
68+
new SchemaBundleExample(projectId, instanceId, "test-table", "test-schema-bundle");
69+
example.run();
70+
}
71+
72+
public SchemaBundleExample(
73+
String projectId, String instanceId, String tableId, String schemaBundleId)
74+
throws IOException {
75+
this.tableId = tableId;
76+
this.schemaBundleId = schemaBundleId;
77+
78+
// Creates the settings to configure a bigtable table admin client.
79+
BigtableTableAdminSettings adminSettings =
80+
BigtableTableAdminSettings.newBuilder()
81+
.setProjectId(projectId)
82+
.setInstanceId(instanceId)
83+
.build();
84+
85+
// Creates a bigtable table admin client.
86+
adminClient = BigtableTableAdminClient.create(adminSettings);
87+
}
88+
89+
public void close() {
90+
adminClient.close();
91+
}
92+
93+
public void run() {
94+
createTable();
95+
createSchemaBundle();
96+
updateSchemaBundle();
97+
getSchemaBundle();
98+
listAllSchemaBundles();
99+
deleteSchemaBundle();
100+
deleteTable();
101+
close();
102+
}
103+
104+
public void createTable() {
105+
// Checks if table exists, creates table if it does not exist.
106+
if (!adminClient.exists(tableId)) {
107+
System.out.println("Table does not exist, creating table: " + tableId);
108+
CreateTableRequest createTableRequest =
109+
CreateTableRequest.of(tableId).addFamily(COLUMN_FAMILY);
110+
Table table = adminClient.createTable(createTableRequest);
111+
System.out.printf("Table: %s created successfully%n", table.getId());
112+
}
113+
}
114+
115+
public void deleteTable() {
116+
// Deletes the entire table.
117+
System.out.println("\nDelete table: " + tableId);
118+
try {
119+
adminClient.deleteTable(tableId);
120+
System.out.printf("Table: %s deleted successfully%n", tableId);
121+
} catch (NotFoundException e) {
122+
System.err.println("Failed to delete a non-existent table: " + e.getMessage());
123+
}
124+
}
125+
126+
/** Demonstrates how to create a schema bundle under a table with the specified schema. */
127+
public void createSchemaBundle() {
128+
// Checks if the schema bundle exists, creates it if it does not exist.
129+
try {
130+
adminClient.getSchemaBundle(tableId, schemaBundleId);
131+
} catch (NotFoundException exception) {
132+
System.out.printf("%nCreating schema bundle %s in table %s%n", schemaBundleId, tableId);
133+
// [START bigtable_create_schema_bundle]
134+
try {
135+
InputStream in = getClass().getClassLoader().getResourceAsStream(PROTO_FILE_PATH);
136+
CreateSchemaBundleRequest request =
137+
CreateSchemaBundleRequest.of(tableId, schemaBundleId)
138+
.setProtoSchema(ByteString.readFrom(in));
139+
SchemaBundle schemaBundle = adminClient.createSchemaBundle(request);
140+
System.out.printf("Schema bundle: %s created successfully%n", schemaBundle.getId());
141+
} catch (NotFoundException e) {
142+
System.err.println(
143+
"Failed to create a schema bundle from a non-existent table: " + e.getMessage());
144+
} catch (IOException e) {
145+
throw new RuntimeException(e);
146+
}
147+
// [END bigtable_create_schema_bundle]
148+
}
149+
}
150+
151+
/** Demonstrates how to modify a schema bundle. */
152+
public void updateSchemaBundle() {
153+
System.out.printf("%nUpdating schema bundle %s in table %s%n", schemaBundleId, tableId);
154+
// [START bigtable_update_schema_bundle]
155+
try {
156+
InputStream in = getClass().getClassLoader().getResourceAsStream(PROTO_FILE_PATH);
157+
UpdateSchemaBundleRequest request =
158+
UpdateSchemaBundleRequest.of(tableId, schemaBundleId)
159+
.setProtoSchema(ByteString.readFrom(in));
160+
SchemaBundle schemaBundle = adminClient.updateSchemaBundle(request);
161+
System.out.printf("Schema bundle: %s updated successfully%n", schemaBundle.getId());
162+
} catch (NotFoundException e) {
163+
System.err.println("Failed to modify a non-existent schema bundle: " + e.getMessage());
164+
} catch (IOException e) {
165+
throw new RuntimeException(e);
166+
}
167+
// [END bigtable_update_schema_bundle]
168+
}
169+
170+
/** Demonstrates how to get a schema bundle's definition. */
171+
public SchemaBundle getSchemaBundle() {
172+
System.out.printf("%nGetting schema bundle %s in table %s%n", schemaBundleId, tableId);
173+
// [START bigtable_get_schema_bundle]
174+
SchemaBundle schemaBundle = null;
175+
try {
176+
schemaBundle = adminClient.getSchemaBundle(tableId, schemaBundleId);
177+
// Deserialize and print the FileDescriptorSet
178+
DescriptorProtos.FileDescriptorSet fileDescriptorSet =
179+
DescriptorProtos.FileDescriptorSet.parseFrom(schemaBundle.getProtoSchema());
180+
181+
System.out.println("--------- Deserialized FileDescriptorSet ---------");
182+
for (DescriptorProtos.FileDescriptorProto fileDescriptorProto :
183+
fileDescriptorSet.getFileList()) {
184+
System.out.println("File: " + fileDescriptorProto.getName());
185+
System.out.println(" Package: " + fileDescriptorProto.getPackage());
186+
for (DescriptorProtos.DescriptorProto messageType :
187+
fileDescriptorProto.getMessageTypeList()) {
188+
System.out.println(" Message: " + messageType.getName());
189+
}
190+
}
191+
System.out.println("--------------------------------------------------");
192+
} catch (InvalidProtocolBufferException e) {
193+
System.err.println("Failed to parse FileDescriptorSet: " + e.getMessage());
194+
} catch (NotFoundException e) {
195+
System.err.println(
196+
"Failed to retrieve metadata from a non-existent schema bundle: " + e.getMessage());
197+
}
198+
// [END bigtable_get_schema_bundle]
199+
return schemaBundle;
200+
}
201+
202+
/** Demonstrates how to list all schema bundles within a table. */
203+
public List<String> listAllSchemaBundles() {
204+
System.out.printf("%nListing schema bundles in table %s%n", tableId);
205+
// [START bigtable_list_schema_bundles]
206+
List<String> schemaBundleIds = new ArrayList<>();
207+
try {
208+
schemaBundleIds = adminClient.listSchemaBundles(tableId);
209+
for (String schemaBundleId : schemaBundleIds) {
210+
System.out.println(schemaBundleId);
211+
}
212+
} catch (NotFoundException e) {
213+
System.err.println(
214+
"Failed to list schema bundles from a non-existent table: " + e.getMessage());
215+
}
216+
// [END bigtable_list_schema_bundles]
217+
return schemaBundleIds;
218+
}
219+
220+
/** Demonstrates how to delete a schema bundle. */
221+
public void deleteSchemaBundle() {
222+
System.out.printf("%nDeleting schema bundle %s in table %s%n", schemaBundleId, tableId);
223+
// [START bigtable_delete_schema_bundle]
224+
try {
225+
adminClient.deleteSchemaBundle(tableId, schemaBundleId);
226+
System.out.printf("SchemaBundle: %s deleted successfully%n", schemaBundleId);
227+
} catch (NotFoundException e) {
228+
System.err.println("Failed to delete a non-existent schema bundle: " + e.getMessage());
229+
}
230+
// [END bigtable_delete_schema_bundle]
231+
}
232+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#### To generate SingerProto.java and descriptors.pb file from singer.proto using `protoc`
2+
```shell
3+
cd samples/snippets/src/main/resources/
4+
protoc --proto_path=com/example/bigtable/ --include_imports --descriptor_set_out=com/example/bigtable/descriptors.pb \
5+
--java_out=. com/example/bigtable/singer.proto
6+
```

0 commit comments

Comments
 (0)