1+ package com .google .cloud .examples .bigtable ;
2+
3+ import com .google .bigtable .admin .v2 .ProjectName ;
4+ import com .google .cloud .bigtable .admin .v2 .BigtableInstanceAdminClient ;
5+ import com .google .cloud .bigtable .admin .v2 .BigtableInstanceAdminSettings ;
6+ import com .google .cloud .bigtable .admin .v2 .models .Cluster ;
7+ import com .google .cloud .bigtable .admin .v2 .models .CreateClusterRequest ;
8+ import com .google .cloud .bigtable .admin .v2 .models .CreateInstanceRequest ;
9+ import com .google .cloud .bigtable .admin .v2 .models .Instance ;
10+ import com .google .cloud .bigtable .admin .v2 .models .Instance .Type ;
11+ import com .google .cloud .bigtable .admin .v2 .models .StorageType ;
12+ import java .io .IOException ;
13+ import java .util .List ;
14+ import java .util .Map ;
15+
16+ public class InstanceAdmin {
17+
18+ public static void main (String [] args ) throws IOException {
19+
20+ String GCLOUD_PROJECT = System .getenv ("GCLOUD_PROJECT" );
21+
22+ if (GCLOUD_PROJECT .length () == 0 ) {
23+ throw new Error ("Environment variables GCLOUD_PROJECT must be set!" );
24+ }
25+
26+ BigtableInstanceAdminSettings instanceAdminSettings =
27+ BigtableInstanceAdminSettings .newBuilder ().setProjectName (ProjectName .of (GCLOUD_PROJECT ))
28+ .build ();
29+
30+ BigtableInstanceAdminClient adminClient =
31+ BigtableInstanceAdminClient .create (instanceAdminSettings );
32+
33+ System .out .println ("Create an instance (type: PRODUCTION) and run basic instance-operations" );
34+ runInstanceOperations (adminClient , "ssd-instance" , "ssd-cluster" );
35+
36+ System .out .println ("Create DEVELOPMENT instance" );
37+ createDevInstance (adminClient , "hdd-instance" , "hdd-cluster" );
38+
39+ System .out .println ("Delete the Instance" );
40+ deleteInstance (adminClient , "hdd-instance" );
41+
42+ System .out .println ("Add Cluster" );
43+ addCluster (adminClient , "ssd-instance" , "ssd-cluster" );
44+
45+ System .out .println ("Delete the Cluster" );
46+ deleteCluster (adminClient , "ssd-instance" , "ssd-cluster" );
47+
48+ // end operations with deleting the pro-instance created in `runInstanceOperations`
49+ deleteInstance (adminClient , "ssd-instance" );
50+ }
51+
52+ public static void runInstanceOperations (BigtableInstanceAdminClient adminClient ,
53+ String instanceID , String clusterID ) {
54+ System .out .println ("Check Instance Exists" );
55+ // [START bigtable_check_instance_exists]
56+ boolean found = false ;
57+ try {
58+ found = adminClient .exists (instanceID );
59+ } catch (Exception e ) {
60+ System .out .println ("Error checking if Instance exists: " + e .getMessage ());
61+ }
62+
63+ // Create instance if does not exists
64+ if (!found ) {
65+ System .out .println ("Creating a PRODUCTION Instance" );
66+ // [START bigtable_create_prod_instance]
67+ // Creates a Production Instance with the ID "ssd-instance"
68+ // with cluster id "ssd-cluster", 3 nodes and location us-central1-f
69+ CreateInstanceRequest createInstanceRequest = CreateInstanceRequest .of (instanceID )
70+ .addCluster (clusterID , "us-central1-f" , 3 , StorageType .SSD ).setType (Type .PRODUCTION )
71+ .addLabel ("prod-label" , "prod-label" );
72+ // Create production instance with given request
73+ try {
74+ Instance instance = adminClient .createInstance (createInstanceRequest );
75+ System .out
76+ .println ("PRODUCTION type instance : " + instance .getId () + " created successfully" );
77+
78+ } catch (Exception e ) {
79+ System .out .println ("Error creating prod-instance: " + e .getMessage ());
80+ }
81+ // [END bigtable_create_prod_instance]
82+ } else {
83+ System .out .println ("Instance " + instanceID + " exists" );
84+ }
85+
86+ System .out .println (); //for a new-line
87+ System .out .println ("Listing Instances:" );
88+ // [START bigtable_list_instances]
89+ try {
90+ List <Instance > instances = adminClient .listInstances ();
91+ for (Instance instance : instances ) {
92+ System .out .println (instance .getId ());
93+ }
94+ } catch (Exception e ) {
95+ System .out .println ("Error listing instances: " + e .getMessage ());
96+ }
97+ // [END bigtable_list_instances]
98+
99+ System .out .println (); //for a new-line
100+ System .out .println ("Get Instance:" );
101+ // [START bigtable_get_instance]
102+ try {
103+ Instance instance = adminClient .getInstance (instanceID );
104+ System .out .println ("Instance ID: " + instance .getId ());
105+ System .out .println ("Instance Meta:" );
106+ System .out .println ("Display Name: " + instance .getDisplayName ());
107+ System .out .println ("Labels:" );
108+ Map <String , String > labels = instance .getLabels ();
109+ for (String key : labels .keySet ()) {
110+ System .out .println (key + ": " + labels .get (key ));
111+ }
112+ System .out .println ("State: " + instance .getState ());
113+ System .out .println ("Type: " + instance .getType ());
114+ } catch (Exception e ) {
115+ System .out .println ("Error getting instance: " + e .getMessage ());
116+ }
117+ // [END bigtable_get_instance]
118+
119+ System .out .println (); //for a new-line
120+ System .out .println ("Listing Clusters..." );
121+ // [START bigtable_get_clusters]
122+ try {
123+ List <Cluster > clusters = adminClient .listClusters (instanceID );
124+ for (Cluster cluster : clusters ) {
125+ System .out .println (cluster .getId ());
126+ }
127+ } catch (Exception e ) {
128+ System .out .println ("Error listing clusters: " + e .getMessage ());
129+ }
130+ // [END bigtable_get_clusters]
131+ }
132+
133+ public static void createDevInstance (BigtableInstanceAdminClient adminClient , String instanceID ,
134+ String clusterID ) {
135+ // [START bigtable_create_dev_instance]
136+ System .out .println (); //for a new-line
137+ System .out .println ("Creating a DEVELOPMENT Instance" );
138+ // Creates a Development instance with the ID "hdd-instance"
139+ // with cluster ID "hdd-cluster" and location us-central1-f
140+ // Cluster nodes should not be set while creating Development Instance
141+ CreateInstanceRequest createInstanceRequest = CreateInstanceRequest .of (instanceID )
142+ .addCluster (clusterID , "us-central1-f" , 1 , StorageType .HDD ).setType (Type .DEVELOPMENT )
143+ .addLabel ("dev-label" , "dev-label" );
144+ // Create development instance with given request
145+ try {
146+ Instance instance = adminClient .createInstance (createInstanceRequest );
147+ System .out
148+ .println ("DEVELOPMENT type instance : " + instance .getId () + " created successfully" );
149+ } catch (Exception e ) {
150+ System .out .println ("Error creating dev-instance: " + e .getMessage ());
151+ }
152+ // [END bigtable_create_dev_instance]
153+ }
154+
155+ // Delete the Instance
156+ public static void deleteInstance (BigtableInstanceAdminClient adminClient , String instanceID ) {
157+ // [START bigtable_delete_instance]
158+ System .out .println (); //for a new-line
159+ System .out .println ("Deleting Instance" );
160+ try {
161+ adminClient .deleteInstance (instanceID );
162+ System .out .println ("Instance deleted: " + instanceID );
163+ } catch (Exception e ) {
164+ System .out .println ("Error deleting instance: " + instanceID );
165+ }
166+ // [END bigtable_delete_instance]
167+ }
168+
169+ // Add Cluster
170+ public static void addCluster (BigtableInstanceAdminClient adminClient , String instanceID ,
171+ String clusterID ) {
172+ boolean found = false ;
173+ try {
174+ found = adminClient .exists (instanceID );
175+ } catch (Exception e ) {
176+ System .out .println ("Error checking if Instance exists: " + e .getMessage ());
177+ }
178+ if (!found ) {
179+ System .out .println ("Instance does not exist" );
180+ } else {
181+ System .out .println (); //for a new-line
182+ System .out .println ("Adding Cluster to Instance " + instanceID );
183+ // [START bigtable_create_cluster]
184+ CreateClusterRequest createClusterRequest =
185+ CreateClusterRequest .of (instanceID , clusterID ).setServeNodes (3 )
186+ .setStorageType (StorageType .SSD ).setZone ("us-central1-c" );
187+ try {
188+ Cluster cluster = adminClient .createCluster (createClusterRequest );
189+ System .out .println ("Cluster : " + cluster .getId () + " created successfully" );
190+ } catch (Exception e ) {
191+ System .out .println ("Error creating cluster: " + e .getMessage ());
192+ }
193+ // [END bigtable_create_cluster]
194+ }
195+ }
196+
197+ // Delete the Cluster
198+ public static void deleteCluster (BigtableInstanceAdminClient adminClient , String instanceID ,
199+ String clusterID ) {
200+ // [START bigtable_delete_cluster]
201+ System .out .println (); //for a new-line
202+ System .out .println ("Deleting Cluster" );
203+ // [START bigtable_delete_cluster]
204+ try {
205+ adminClient .deleteCluster (instanceID , clusterID );
206+ System .out .println ("Cluster : " + clusterID + " deleted successfully" );
207+ } catch (Exception e ) {
208+ System .out .println ("Error deleting cluster: " + e .getMessage ());
209+ }
210+ System .out .println ("Cluster deleted: " + clusterID );
211+ // [END bigtable_delete_cluster]
212+ }
213+ }
0 commit comments