1818
1919import com .google .api .gax .rpc .AlreadyExistsException ;
2020import com .google .api .gax .rpc .NotFoundException ;
21+ import com .google .bigtable .admin .v2 .Cluster ;
22+ import com .google .bigtable .admin .v2 .CreateClusterRequest ;
23+ import com .google .bigtable .admin .v2 .CreateInstanceRequest ;
24+ import com .google .bigtable .admin .v2 .DeleteClusterRequest ;
25+ import com .google .bigtable .admin .v2 .DeleteInstanceRequest ;
26+ import com .google .bigtable .admin .v2 .GetClusterRequest ;
27+ import com .google .bigtable .admin .v2 .GetInstanceRequest ;
28+ import com .google .bigtable .admin .v2 .Instance ;
29+ import com .google .bigtable .admin .v2 .ListClustersRequest ;
30+ import com .google .bigtable .admin .v2 .ListClustersResponse ;
31+ import com .google .bigtable .admin .v2 .ListInstancesRequest ;
32+ import com .google .bigtable .admin .v2 .ListInstancesResponse ;
33+ import com .google .bigtable .admin .v2 .StorageType ;
2134import com .google .cloud .bigtable .admin .v2 .BigtableInstanceAdminClient ;
2235import 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 ;
2936import java .io .IOException ;
30- import java .util .List ;
3137import java .util .Map ;
3238
3339/**
4955public class InstanceAdminExample {
5056
5157 private static final String CLUSTER = "cluster" ;
58+ private final String projectId ;
5259 private final String clusterId ;
5360 private final String instanceId ;
5461 private final BigtableInstanceAdminClient adminClient ;
@@ -68,6 +75,7 @@ public static void main(String[] args) throws IOException {
6875
6976 public InstanceAdminExample (String projectId , String instanceId , String clusterId )
7077 throws IOException {
78+ this .projectId = projectId ;
7179 this .instanceId = instanceId ;
7280 this .clusterId = clusterId ;
7381
@@ -103,18 +111,33 @@ public void createProdInstance() {
103111 // [START bigtable_create_prod_instance]
104112 // Creates a Production Instance with the ID "ssd-instance",
105113 // 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 )
114+ String parent = "projects/" + projectId ;
115+ Instance instanceObj =
116+ Instance .newBuilder ()
117+ .setDisplayName (instanceId )
109118 .setType (Instance .Type .PRODUCTION )
110- .addLabel ("department" , "accounting" );
119+ .putLabels ("department" , "accounting" )
120+ .build ();
121+ Cluster clusterObj =
122+ Cluster .newBuilder ()
123+ .setLocation ("projects/" + projectId + "/locations/us-central1-f" )
124+ .setServeNodes (3 )
125+ .setDefaultStorageType (StorageType .SSD )
126+ .build ();
127+ CreateInstanceRequest request =
128+ CreateInstanceRequest .newBuilder ()
129+ .setParent (parent )
130+ .setInstanceId (instanceId )
131+ .setInstance (instanceObj )
132+ .putClusters (clusterId , clusterObj )
133+ .build ();
111134 // Creates a production instance with the given request.
112135 try {
113- Instance instance = adminClient .createInstance ( createInstanceRequest );
114- System .out .printf ("PRODUCTION type instance %s created successfully%n" , instance .getId ());
136+ Instance instance = adminClient .getBaseClient (). createInstanceAsync ( request ). get ( );
137+ System .out .printf ("PRODUCTION type instance %s created successfully%n" , instance .getName ());
115138 } catch (Exception e ) {
116139 System .err .println ("Failed to create instance: " + e .getMessage ());
117- throw e ;
140+ throw new RuntimeException ( e ) ;
118141 }
119142 // [END bigtable_create_prod_instance]
120143 }
@@ -125,14 +148,14 @@ public void listInstances() {
125148 System .out .println ("\n Listing Instances" );
126149 // [START bigtable_list_instances]
127150 try {
128- List <Instance > instances = adminClient .listInstances ();
129- for (Instance instance : instances ) {
130- System .out .println (instance .getId ());
151+ String parent = "projects/" + projectId ;
152+ ListInstancesRequest request = ListInstancesRequest .newBuilder ().setParent (parent ).build ();
153+ ListInstancesResponse response = adminClient .getBaseClient ().listInstances (request );
154+ for (Instance instance : response .getInstancesList ()) {
155+ System .out .println (instance .getName ());
131156 }
132- } catch (PartialListInstancesException e ) {
157+ } catch (Exception e ) {
133158 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 ());
136159 }
137160 // [END bigtable_list_instances]
138161 }
@@ -143,11 +166,13 @@ public Instance getInstance() {
143166 // [START bigtable_get_instance]
144167 Instance instance = null ;
145168 try {
146- instance = adminClient .getInstance (instanceId );
147- System .out .println ("Instance ID: " + instance .getId ());
169+ String name = "projects/" + projectId + "/instances/" + instanceId ;
170+ GetInstanceRequest request = GetInstanceRequest .newBuilder ().setName (name ).build ();
171+ instance = adminClient .getBaseClient ().getInstance (request );
172+ System .out .println ("Instance ID: " + instance .getName ());
148173 System .out .println ("Display Name: " + instance .getDisplayName ());
149174 System .out .print ("Labels: " );
150- Map <String , String > labels = instance .getLabels ();
175+ Map <String , String > labels = instance .getLabelsMap ();
151176 for (String key : labels .keySet ()) {
152177 System .out .printf ("%s - %s" , key , labels .get (key ));
153178 }
@@ -165,9 +190,11 @@ public void listClusters() {
165190 System .out .println ("\n Listing Clusters" );
166191 // [START bigtable_get_clusters]
167192 try {
168- List <Cluster > clusters = adminClient .listClusters (instanceId );
169- for (Cluster cluster : clusters ) {
170- System .out .println (cluster .getId ());
193+ String parent = "projects/" + projectId + "/instances/" + instanceId ;
194+ ListClustersRequest request = ListClustersRequest .newBuilder ().setParent (parent ).build ();
195+ ListClustersResponse response = adminClient .getBaseClient ().listClusters (request );
196+ for (Cluster cluster : response .getClustersList ()) {
197+ System .out .println (cluster .getName ());
171198 }
172199 } catch (NotFoundException e ) {
173200 System .err .println ("Failed to list clusters from a non-existent instance: " + e .getMessage ());
@@ -180,7 +207,9 @@ public void deleteInstance() {
180207 System .out .println ("\n Deleting Instance" );
181208 // [START bigtable_delete_instance]
182209 try {
183- adminClient .deleteInstance (instanceId );
210+ String name = "projects/" + projectId + "/instances/" + instanceId ;
211+ DeleteInstanceRequest request = DeleteInstanceRequest .newBuilder ().setName (name ).build ();
212+ adminClient .getBaseClient ().deleteInstance (request );
184213 System .out .println ("Instance deleted: " + instanceId );
185214 } catch (NotFoundException e ) {
186215 System .err .println ("Failed to delete non-existent instance: " + e .getMessage ());
@@ -193,14 +222,23 @@ public void addCluster() {
193222 System .out .printf ("%nAdding cluster: %s to instance: %s%n" , CLUSTER , instanceId );
194223 // [START bigtable_create_cluster]
195224 try {
196- adminClient .createCluster (
197- CreateClusterRequest .of (instanceId , CLUSTER )
198- .setZone ("us-central1-c" )
225+ String parent = "projects/" + projectId + "/instances/" + instanceId ;
226+ Cluster clusterObj =
227+ Cluster .newBuilder ()
228+ .setLocation ("projects/" + projectId + "/locations/us-central1-c" )
199229 .setServeNodes (3 )
200- .setStorageType (StorageType .SSD ));
230+ .setDefaultStorageType (StorageType .SSD )
231+ .build ();
232+ CreateClusterRequest request =
233+ CreateClusterRequest .newBuilder ()
234+ .setParent (parent )
235+ .setClusterId (CLUSTER )
236+ .setCluster (clusterObj )
237+ .build ();
238+ adminClient .getBaseClient ().createClusterAsync (request ).get ();
201239 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 ());
240+ } catch (Exception e ) {
241+ System .err .println ("Failed to add cluster: " + e .getMessage ());
204242 }
205243 // [END bigtable_create_cluster]
206244 }
@@ -210,7 +248,9 @@ public void deleteCluster() {
210248 System .out .printf ("%nDeleting cluster: %s from instance: %s%n" , CLUSTER , instanceId );
211249 // [START bigtable_delete_cluster]
212250 try {
213- adminClient .deleteCluster (instanceId , CLUSTER );
251+ String name = "projects/" + projectId + "/instances/" + instanceId + "/clusters/" + CLUSTER ;
252+ DeleteClusterRequest request = DeleteClusterRequest .newBuilder ().setName (name ).build ();
253+ adminClient .getBaseClient ().deleteCluster (request );
214254 System .out .printf ("Cluster: %s deleted successfully%n" , CLUSTER );
215255 } catch (NotFoundException e ) {
216256 System .err .println ("Failed to delete a non-existent cluster: " + e .getMessage ());
0 commit comments