| 
 | 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 | +}  | 
0 commit comments