Skip to content
This repository was archived by the owner on May 8, 2026. It is now read-only.

Commit 7ac6ce2

Browse files
committed
samples: Add SchemaBundle admin examples
Change-Id: Ib0cd4ae382dac1984d5f6aaf14ea1d22c4ef5a69
1 parent bfbbe43 commit 7ac6ce2

7 files changed

Lines changed: 382 additions & 2 deletions

File tree

samples/install-without-bom/pom.xml

Lines changed: 1 addition & 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.62.0</version>
3333
</dependency>
3434
<!-- [END bigtable_install_without_bom] -->
3535

samples/snippets/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
<dependency>
3030
<groupId>com.google.cloud</groupId>
3131
<artifactId>libraries-bom</artifactId>
32-
<version>26.50.0</version>
32+
<version>26.69.0</version>
3333
<type>pom</type>
3434
<scope>import</scope>
3535
</dependency>
Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
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+
public class SchemaBundleExample {
36+
37+
private static final String COLUMN_FAMILY = "cf";
38+
private final String tableId;
39+
private final String schemaBundleId;
40+
private final BigtableTableAdminClient adminClient;
41+
42+
public static void main(String[] args) throws IOException {
43+
if (args.length != 2) {
44+
System.out.println("Missing required project id or instance id");
45+
return;
46+
}
47+
String projectId = args[0];
48+
String instanceId = args[1];
49+
50+
SchemaBundleExample example =
51+
new SchemaBundleExample(projectId, instanceId, "test-table", "test-schema-bundle");
52+
example.run();
53+
}
54+
55+
public SchemaBundleExample(
56+
String projectId, String instanceId, String tableId, String schemaBundleId)
57+
throws IOException {
58+
this.tableId = tableId;
59+
this.schemaBundleId = schemaBundleId;
60+
61+
// Creates the settings to configure a bigtable table admin client.
62+
BigtableTableAdminSettings adminSettings =
63+
BigtableTableAdminSettings.newBuilder()
64+
.setProjectId(projectId)
65+
.setInstanceId(instanceId)
66+
.build();
67+
68+
// Creates a bigtable table admin client.
69+
adminClient = BigtableTableAdminClient.create(adminSettings);
70+
}
71+
72+
public void close() {
73+
adminClient.close();
74+
}
75+
76+
public void run() {
77+
createTable();
78+
createSchemaBundle();
79+
updateSchemaBundle();
80+
getSchemaBundle();
81+
listAllSchemaBundles();
82+
deleteSchemaBundle();
83+
deleteTable();
84+
close();
85+
}
86+
87+
public void createTable() {
88+
// Checks if table exists, creates table if it does not exist.
89+
if (!adminClient.exists(tableId)) {
90+
System.out.println("Table does not exist, creating table: " + tableId);
91+
CreateTableRequest createTableRequest =
92+
CreateTableRequest.of(tableId).addFamily(COLUMN_FAMILY);
93+
Table table = adminClient.createTable(createTableRequest);
94+
System.out.printf("Table: %s created successfully%n", table.getId());
95+
}
96+
}
97+
98+
public void deleteTable() {
99+
// Deletes the entire table.
100+
System.out.println("\nDelete table: " + tableId);
101+
try {
102+
adminClient.deleteTable(tableId);
103+
System.out.printf("Table: %s deleted successfully%n", tableId);
104+
} catch (NotFoundException e) {
105+
System.err.println("Failed to delete a non-existent table: " + e.getMessage());
106+
}
107+
}
108+
109+
/** Demonstrates how to create a schema bundle under a table with the specified schema. */
110+
public void createSchemaBundle() {
111+
// Checks if the schema bundle exists, creates it if it does not exist.
112+
try {
113+
adminClient.getSchemaBundle(tableId, schemaBundleId);
114+
} catch (NotFoundException exception) {
115+
System.out.printf("%nCreating schema bundle %s in table %s%n", schemaBundleId, tableId);
116+
// [START bigtable_create_schema_bundle]
117+
try {
118+
InputStream in = getClass().getClassLoader().getResourceAsStream("descriptors.pb");
119+
CreateSchemaBundleRequest request =
120+
CreateSchemaBundleRequest.of(tableId, schemaBundleId)
121+
.setProtoSchema(ByteString.readFrom(in));
122+
SchemaBundle schemaBundle = adminClient.createSchemaBundle(request);
123+
System.out.printf("Schema bundle: %s created successfully%n", schemaBundle.getId());
124+
} catch (NotFoundException e) {
125+
System.err.println(
126+
"Failed to create a schema bundle from a non-existent table: " + e.getMessage());
127+
} catch (IOException e) {
128+
throw new RuntimeException(e);
129+
}
130+
// [END bigtable_create_schema_bundle]
131+
}
132+
}
133+
134+
/** Demonstrates how to modify a schema bundle. */
135+
public void updateSchemaBundle() {
136+
System.out.printf("%nUpdating schema bundle %s in table %s%n", schemaBundleId, tableId);
137+
// [START bigtable_update_schema_bundle]
138+
try {
139+
InputStream in = getClass().getClassLoader().getResourceAsStream("descriptors.pb");
140+
UpdateSchemaBundleRequest request =
141+
UpdateSchemaBundleRequest.of(tableId, schemaBundleId)
142+
.setProtoSchema(ByteString.readFrom(in));
143+
SchemaBundle schemaBundle = adminClient.updateSchemaBundle(request);
144+
System.out.printf("Schema bundle: %s updated successfully%n", schemaBundle.getId());
145+
} catch (NotFoundException e) {
146+
System.err.println("Failed to modify a non-existent schema bundle: " + e.getMessage());
147+
} catch (IOException e) {
148+
throw new RuntimeException(e);
149+
}
150+
// [END bigtable_update_schema_bundle]
151+
}
152+
153+
/** Demonstrates how to get a schema bundle's definition. */
154+
public SchemaBundle getSchemaBundle() {
155+
System.out.printf("%nGetting schema bundle %s in table %s%n", schemaBundleId, tableId);
156+
// [START bigtable_get_schema_bundle]
157+
SchemaBundle schemaBundle = null;
158+
try {
159+
schemaBundle = adminClient.getSchemaBundle(tableId, schemaBundleId);
160+
// Deserialize and print the FileDescriptorSet
161+
DescriptorProtos.FileDescriptorSet fileDescriptorSet =
162+
DescriptorProtos.FileDescriptorSet.parseFrom(schemaBundle.getProtoSchema());
163+
164+
System.out.println("--------- Deserialized FileDescriptorSet ---------");
165+
for (DescriptorProtos.FileDescriptorProto fileDescriptorProto :
166+
fileDescriptorSet.getFileList()) {
167+
System.out.println("File: " + fileDescriptorProto.getName());
168+
System.out.println(" Package: " + fileDescriptorProto.getPackage());
169+
for (DescriptorProtos.DescriptorProto messageType :
170+
fileDescriptorProto.getMessageTypeList()) {
171+
System.out.println(" Message: " + messageType.getName());
172+
}
173+
}
174+
System.out.println("--------------------------------------------------");
175+
} catch (InvalidProtocolBufferException e) {
176+
System.err.println("Failed to parse FileDescriptorSet: " + e.getMessage());
177+
} catch (NotFoundException e) {
178+
System.err.println(
179+
"Failed to retrieve metadata from a non-existent schema bundle: " + e.getMessage());
180+
}
181+
// [END bigtable_get_schema_bundle]
182+
return schemaBundle;
183+
}
184+
185+
/** Demonstrates how to list all schema bundles within a table. */
186+
public List<String> listAllSchemaBundles() {
187+
System.out.printf("%nListing schema bundles in table %s%n", tableId);
188+
// [START bigtable_list_schema_bundles]
189+
List<String> schemaBundleIds = new ArrayList<>();
190+
try {
191+
schemaBundleIds = adminClient.listSchemaBundles(tableId);
192+
for (String schemaBundleId : schemaBundleIds) {
193+
System.out.println(schemaBundleId);
194+
}
195+
} catch (NotFoundException e) {
196+
System.err.println(
197+
"Failed to list schema bundles from a non-existent table: " + e.getMessage());
198+
}
199+
// [END bigtable_list_schema_bundles]
200+
return schemaBundleIds;
201+
}
202+
203+
/** Demonstrates how to delete a schema bundle. */
204+
public void deleteSchemaBundle() {
205+
System.out.printf("%nDeleting schema bundle %s in table %s%n", schemaBundleId, tableId);
206+
// [START bigtable_delete_schema_bundle]
207+
try {
208+
adminClient.deleteSchemaBundle(tableId, schemaBundleId);
209+
System.out.printf("SchemaBundle: %s deleted successfully%n", schemaBundleId);
210+
} catch (NotFoundException e) {
211+
System.err.println("Failed to delete a non-existent schema bundle: " + e.getMessage());
212+
}
213+
// [END bigtable_delete_schema_bundle]
214+
}
215+
}
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+
```
375 Bytes
Binary file not shown.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
syntax = "proto3";
2+
3+
package examples.bigtable.music;
4+
5+
option java_package = "com.example.bigtable";
6+
option java_outer_classname = "SingerProto";
7+
option java_multiple_files = false;
8+
9+
message SingerInfo {
10+
optional int64 singer_id = 1;
11+
optional string birth_date = 2;
12+
optional string nationality = 3;
13+
optional Genre genre = 4;
14+
}
15+
16+
enum Genre {
17+
POP = 0;
18+
JAZZ = 1;
19+
FOLK = 2;
20+
ROCK = 3;
21+
}

0 commit comments

Comments
 (0)