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

Commit 0f8b35b

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

7 files changed

Lines changed: 394 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: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
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+
* https://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.Table;
24+
import com.google.cloud.bigtable.admin.v2.models.UpdateSchemaBundleRequest;
25+
import com.google.cloud.bigtable.admin.v2.models.CreateTableRequest;
26+
import com.google.cloud.bigtable.admin.v2.models.SchemaBundle;
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 = new SchemaBundleExample(projectId, instanceId, "test-table",
51+
"test-schema-bundle");
52+
example.run();
53+
}
54+
55+
public SchemaBundleExample(String projectId, String instanceId, String tableId,
56+
String schemaBundleId) throws IOException {
57+
this.tableId = tableId;
58+
this.schemaBundleId = schemaBundleId;
59+
60+
// Creates the settings to configure a bigtable table admin client.
61+
BigtableTableAdminSettings adminSettings =
62+
BigtableTableAdminSettings.newBuilder()
63+
.setProjectId(projectId)
64+
.setInstanceId(instanceId)
65+
.build();
66+
67+
// Creates a bigtable table admin client.
68+
adminClient = BigtableTableAdminClient.create(adminSettings);
69+
}
70+
71+
public void close() {
72+
adminClient.close();
73+
}
74+
75+
public void run() {
76+
createTable();
77+
createSchemaBundle();
78+
updateSchemaBundle();
79+
getSchemaBundle();
80+
listAllSchemaBundles();
81+
deleteSchemaBundle();
82+
deleteTable();
83+
close();
84+
}
85+
86+
public void createTable() {
87+
// Checks if table exists, creates table if it does not exist.
88+
if (!adminClient.exists(tableId)) {
89+
System.out.println("Table does not exist, creating table: " + tableId);
90+
CreateTableRequest createTableRequest =
91+
CreateTableRequest.of(tableId).addFamily(COLUMN_FAMILY);
92+
Table table = adminClient.createTable(createTableRequest);
93+
System.out.printf("Table: %s created successfully%n", table.getId());
94+
}
95+
}
96+
97+
public void deleteTable() {
98+
// Deletes the entire table.
99+
System.out.println("\nDelete table: " + tableId);
100+
try {
101+
adminClient.deleteTable(tableId);
102+
System.out.printf("Table: %s deleted successfully%n", tableId);
103+
} catch (NotFoundException e) {
104+
System.err.println("Failed to delete a non-existent table: " + e.getMessage());
105+
}
106+
}
107+
108+
/**
109+
* Demonstrates how to create a schema bundle under a table with the specified schema.
110+
*/
111+
public void createSchemaBundle() {
112+
// Checks if the schema bundle exists, creates it if it does not exist.
113+
try {
114+
adminClient.getSchemaBundle(tableId, schemaBundleId);
115+
} catch (NotFoundException exception) {
116+
System.out.printf("%nCreating schema bundle %s in table %s%n", schemaBundleId, tableId);
117+
// [START bigtable_create_schema_bundle]
118+
try {
119+
InputStream in = getClass().getClassLoader()
120+
.getResourceAsStream("descriptors.pb");
121+
CreateSchemaBundleRequest request =
122+
CreateSchemaBundleRequest.of(tableId, schemaBundleId)
123+
.setProtoSchema(ByteString.readFrom(in));
124+
SchemaBundle schemaBundle = adminClient.createSchemaBundle(request);
125+
System.out.printf("Schema bundle: %s created successfully%n", schemaBundle.getId());
126+
} catch (NotFoundException e) {
127+
System.err.println(
128+
"Failed to create a schema bundle from a non-existent table: " + e.getMessage());
129+
} catch (IOException e) {
130+
throw new RuntimeException(e);
131+
}
132+
// [END bigtable_create_schema_bundle]
133+
}
134+
}
135+
136+
/**
137+
* Demonstrates how to modify a schema bundle.
138+
*/
139+
public void updateSchemaBundle() {
140+
System.out.printf("%nUpdating schema bundle %s in table %s%n", schemaBundleId, tableId);
141+
// [START bigtable_update_schema_bundle]
142+
try {
143+
InputStream in = getClass().getClassLoader()
144+
.getResourceAsStream("descriptors.pb");
145+
UpdateSchemaBundleRequest request =
146+
UpdateSchemaBundleRequest.of(tableId, schemaBundleId)
147+
.setProtoSchema(ByteString.readFrom(in));
148+
SchemaBundle schemaBundle = adminClient.updateSchemaBundle(request);
149+
System.out.printf("Schema bundle: %s updated successfully%n", schemaBundle.getId());
150+
} catch (NotFoundException e) {
151+
System.err.println("Failed to modify a non-existent schema bundle: " + e.getMessage());
152+
} catch (IOException e) {
153+
throw new RuntimeException(e);
154+
}
155+
// [END bigtable_update_schema_bundle]
156+
}
157+
158+
/**
159+
* Demonstrates how to get a schema bundle's definition.
160+
*/
161+
public SchemaBundle getSchemaBundle() {
162+
System.out.printf("%nGetting schema bundle %s in table %s%n", schemaBundleId, tableId);
163+
// [START bigtable_get_schema_bundle]
164+
SchemaBundle schemaBundle = null;
165+
try {
166+
schemaBundle = adminClient.getSchemaBundle(tableId, schemaBundleId);
167+
// Deserialize and print the FileDescriptorSet
168+
DescriptorProtos.FileDescriptorSet fileDescriptorSet =
169+
DescriptorProtos.FileDescriptorSet.parseFrom(schemaBundle.getProtoSchema());
170+
171+
System.out.println("--------- Deserialized FileDescriptorSet ---------");
172+
for (DescriptorProtos.FileDescriptorProto fileDescriptorProto : fileDescriptorSet.getFileList()) {
173+
System.out.println("File: " + fileDescriptorProto.getName());
174+
System.out.println(" Package: " + fileDescriptorProto.getPackage());
175+
for (DescriptorProtos.DescriptorProto messageType : fileDescriptorProto.getMessageTypeList()) {
176+
System.out.println(" Message: " + messageType.getName());
177+
}
178+
}
179+
System.out.println("--------------------------------------------------");
180+
} catch (InvalidProtocolBufferException e) {
181+
System.err.println("Failed to parse FileDescriptorSet: " + e.getMessage());
182+
} catch (NotFoundException e) {
183+
System.err.println(
184+
"Failed to retrieve metadata from a non-existent schema bundle: " + e.getMessage());
185+
}
186+
// [END bigtable_get_schema_bundle]
187+
return schemaBundle;
188+
}
189+
190+
/**
191+
* Demonstrates how to list all schema bundles within a table.
192+
*/
193+
public List<String> listAllSchemaBundles() {
194+
System.out.printf("%nListing schema bundles in table %s%n", tableId);
195+
// [START bigtable_list_schema_bundles]
196+
List<String> schemaBundleIds = new ArrayList<>();
197+
try {
198+
schemaBundleIds = adminClient.listSchemaBundles(tableId);
199+
for (String schemaBundleId : schemaBundleIds) {
200+
System.out.println(schemaBundleId);
201+
}
202+
} catch (NotFoundException e) {
203+
System.err.println(
204+
"Failed to list schema bundles from a non-existent table: " + e.getMessage());
205+
}
206+
// [END bigtable_list_schema_bundles]
207+
return schemaBundleIds;
208+
}
209+
210+
/**
211+
* Demonstrates how to delete a schema bundle.
212+
*/
213+
public void deleteSchemaBundle() {
214+
System.out.printf("%nDeleting schema bundle %s in table %s%n", schemaBundleId, tableId);
215+
// [START bigtable_delete_schema_bundle]
216+
try {
217+
adminClient.deleteSchemaBundle(tableId, schemaBundleId);
218+
System.out.printf("SchemaBundle: %s deleted successfully%n", schemaBundleId);
219+
} catch (NotFoundException e) {
220+
System.err.println("Failed to delete a non-existent schema bundle: " + e.getMessage());
221+
}
222+
// [END bigtable_delete_schema_bundle]
223+
}
224+
225+
}
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)