1616
1717package com .example .bigtable ;
1818
19- import com .google .api .gax .rpc .AlreadyExistsException ;
2019import com .google .api .gax .rpc .NotFoundException ;
20+ import com .google .bigtable .admin .v2 .Cluster ;
21+ import com .google .bigtable .admin .v2 .CreateClusterRequest ;
22+ import com .google .bigtable .admin .v2 .CreateInstanceRequest ;
23+ import com .google .bigtable .admin .v2 .DeleteClusterRequest ;
24+ import com .google .bigtable .admin .v2 .DeleteInstanceRequest ;
25+ import com .google .bigtable .admin .v2 .GetInstanceRequest ;
26+ import com .google .bigtable .admin .v2 .Instance ;
27+ import com .google .bigtable .admin .v2 .ListClustersRequest ;
28+ import com .google .bigtable .admin .v2 .ListClustersResponse ;
29+ import com .google .bigtable .admin .v2 .ListInstancesRequest ;
30+ import com .google .bigtable .admin .v2 .ListInstancesResponse ;
31+ import com .google .bigtable .admin .v2 .StorageType ;
2132import com .google .cloud .bigtable .admin .v2 .BigtableInstanceAdminClient ;
2233import com .google .cloud .bigtable .admin .v2 .BigtableInstanceAdminSettings ;
23- import com .google .cloud .bigtable .admin .v2 .models .Cluster ;
24- import com .google .cloud .bigtable .admin .v2 .models .CreateClusterRequest ;
25- import com .google .cloud .bigtable .admin .v2 .models .CreateInstanceRequest ;
26- import com .google .cloud .bigtable .admin .v2 .models .Instance ;
27- import com .google .cloud .bigtable .admin .v2 .models .PartialListInstancesException ;
28- import com .google .cloud .bigtable .admin .v2 .models .StorageType ;
2934import java .io .IOException ;
30- import java .util .List ;
3135import java .util .Map ;
3236
3337/**
4953public class InstanceAdminExample {
5054
5155 private static final String CLUSTER = "cluster" ;
56+ private final String projectId ;
5257 private final String clusterId ;
5358 private final String instanceId ;
5459 private final BigtableInstanceAdminClient adminClient ;
@@ -68,6 +73,7 @@ public static void main(String[] args) throws IOException {
6873
6974 public InstanceAdminExample (String projectId , String instanceId , String clusterId )
7075 throws IOException {
76+ this .projectId = projectId ;
7177 this .instanceId = instanceId ;
7278 this .clusterId = clusterId ;
7379
@@ -103,18 +109,33 @@ public void createProdInstance() {
103109 // [START bigtable_create_prod_instance]
104110 // Creates a Production Instance with the ID "ssd-instance",
105111 // cluster id "ssd-cluster", 3 nodes and location "us-central1-f".
106- CreateInstanceRequest createInstanceRequest =
107- CreateInstanceRequest .of (instanceId )
108- .addCluster (clusterId , "us-central1-f" , 3 , StorageType .SSD )
112+ String parent = "projects/" + projectId ;
113+ Instance instanceObj =
114+ Instance .newBuilder ()
115+ .setDisplayName (instanceId )
109116 .setType (Instance .Type .PRODUCTION )
110- .addLabel ("department" , "accounting" );
117+ .putLabels ("department" , "accounting" )
118+ .build ();
119+ Cluster clusterObj =
120+ Cluster .newBuilder ()
121+ .setLocation ("projects/" + projectId + "/locations/us-central1-f" )
122+ .setServeNodes (3 )
123+ .setDefaultStorageType (StorageType .SSD )
124+ .build ();
125+ CreateInstanceRequest request =
126+ CreateInstanceRequest .newBuilder ()
127+ .setParent (parent )
128+ .setInstanceId (instanceId )
129+ .setInstance (instanceObj )
130+ .putClusters (clusterId , clusterObj )
131+ .build ();
111132 // Creates a production instance with the given request.
112133 try {
113- Instance instance = adminClient .createInstance ( createInstanceRequest );
114- System .out .printf ("PRODUCTION type instance %s created successfully%n" , instance .getId ());
134+ Instance instance = adminClient .getBaseClient (). createInstanceAsync ( request ). get ( );
135+ System .out .printf ("PRODUCTION type instance %s created successfully%n" , instance .getName ());
115136 } catch (Exception e ) {
116137 System .err .println ("Failed to create instance: " + e .getMessage ());
117- throw e ;
138+ throw new RuntimeException ( e ) ;
118139 }
119140 // [END bigtable_create_prod_instance]
120141 }
@@ -125,14 +146,14 @@ public void listInstances() {
125146 System .out .println ("\n Listing Instances" );
126147 // [START bigtable_list_instances]
127148 try {
128- List <Instance > instances = adminClient .listInstances ();
129- for (Instance instance : instances ) {
130- System .out .println (instance .getId ());
149+ String parent = "projects/" + projectId ;
150+ ListInstancesRequest request = ListInstancesRequest .newBuilder ().setParent (parent ).build ();
151+ ListInstancesResponse response = adminClient .getBaseClient ().listInstances (request );
152+ for (Instance instance : response .getInstancesList ()) {
153+ System .out .println (instance .getName ());
131154 }
132- } catch (PartialListInstancesException e ) {
155+ } catch (Exception e ) {
133156 System .err .println ("Failed to list instances: " + e .getMessage ());
134- System .err .println ("The following zones are unavailable: " + e .getUnavailableZones ());
135- System .err .println ("But the following instances are reachable: " + e .getInstances ());
136157 }
137158 // [END bigtable_list_instances]
138159 }
@@ -143,11 +164,13 @@ public Instance getInstance() {
143164 // [START bigtable_get_instance]
144165 Instance instance = null ;
145166 try {
146- instance = adminClient .getInstance (instanceId );
147- System .out .println ("Instance ID: " + instance .getId ());
167+ String name = "projects/" + projectId + "/instances/" + instanceId ;
168+ GetInstanceRequest request = GetInstanceRequest .newBuilder ().setName (name ).build ();
169+ instance = adminClient .getBaseClient ().getInstance (request );
170+ System .out .println ("Instance ID: " + instance .getName ());
148171 System .out .println ("Display Name: " + instance .getDisplayName ());
149172 System .out .print ("Labels: " );
150- Map <String , String > labels = instance .getLabels ();
173+ Map <String , String > labels = instance .getLabelsMap ();
151174 for (String key : labels .keySet ()) {
152175 System .out .printf ("%s - %s" , key , labels .get (key ));
153176 }
@@ -165,9 +188,11 @@ public void listClusters() {
165188 System .out .println ("\n Listing Clusters" );
166189 // [START bigtable_get_clusters]
167190 try {
168- List <Cluster > clusters = adminClient .listClusters (instanceId );
169- for (Cluster cluster : clusters ) {
170- System .out .println (cluster .getId ());
191+ String parent = "projects/" + projectId + "/instances/" + instanceId ;
192+ ListClustersRequest request = ListClustersRequest .newBuilder ().setParent (parent ).build ();
193+ ListClustersResponse response = adminClient .getBaseClient ().listClusters (request );
194+ for (Cluster cluster : response .getClustersList ()) {
195+ System .out .println (cluster .getName ());
171196 }
172197 } catch (NotFoundException e ) {
173198 System .err .println ("Failed to list clusters from a non-existent instance: " + e .getMessage ());
@@ -180,7 +205,9 @@ public void deleteInstance() {
180205 System .out .println ("\n Deleting Instance" );
181206 // [START bigtable_delete_instance]
182207 try {
183- adminClient .deleteInstance (instanceId );
208+ String name = "projects/" + projectId + "/instances/" + instanceId ;
209+ DeleteInstanceRequest request = DeleteInstanceRequest .newBuilder ().setName (name ).build ();
210+ adminClient .getBaseClient ().deleteInstance (request );
184211 System .out .println ("Instance deleted: " + instanceId );
185212 } catch (NotFoundException e ) {
186213 System .err .println ("Failed to delete non-existent instance: " + e .getMessage ());
@@ -193,14 +220,23 @@ public void addCluster() {
193220 System .out .printf ("%nAdding cluster: %s to instance: %s%n" , CLUSTER , instanceId );
194221 // [START bigtable_create_cluster]
195222 try {
196- adminClient .createCluster (
197- CreateClusterRequest .of (instanceId , CLUSTER )
198- .setZone ("us-central1-c" )
223+ String parent = "projects/" + projectId + "/instances/" + instanceId ;
224+ Cluster clusterObj =
225+ Cluster .newBuilder ()
226+ .setLocation ("projects/" + projectId + "/locations/us-central1-c" )
199227 .setServeNodes (3 )
200- .setStorageType (StorageType .SSD ));
228+ .setDefaultStorageType (StorageType .SSD )
229+ .build ();
230+ CreateClusterRequest request =
231+ CreateClusterRequest .newBuilder ()
232+ .setParent (parent )
233+ .setClusterId (CLUSTER )
234+ .setCluster (clusterObj )
235+ .build ();
236+ adminClient .getBaseClient ().createClusterAsync (request ).get ();
201237 System .out .printf ("Cluster: %s created successfully%n" , CLUSTER );
202- } catch (AlreadyExistsException e ) {
203- System .err .println ("Failed to add cluster, already exists : " + e .getMessage ());
238+ } catch (Exception e ) {
239+ System .err .println ("Failed to add cluster: " + e .getMessage ());
204240 }
205241 // [END bigtable_create_cluster]
206242 }
@@ -210,7 +246,9 @@ public void deleteCluster() {
210246 System .out .printf ("%nDeleting cluster: %s from instance: %s%n" , CLUSTER , instanceId );
211247 // [START bigtable_delete_cluster]
212248 try {
213- adminClient .deleteCluster (instanceId , CLUSTER );
249+ String name = "projects/" + projectId + "/instances/" + instanceId + "/clusters/" + CLUSTER ;
250+ DeleteClusterRequest request = DeleteClusterRequest .newBuilder ().setName (name ).build ();
251+ adminClient .getBaseClient ().deleteCluster (request );
214252 System .out .printf ("Cluster: %s deleted successfully%n" , CLUSTER );
215253 } catch (NotFoundException e ) {
216254 System .err .println ("Failed to delete a non-existent cluster: " + e .getMessage ());
0 commit comments