-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathInstanceAdminExample.java
More file actions
320 lines (298 loc) · 12.6 KB
/
InstanceAdminExample.java
File metadata and controls
320 lines (298 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
* Copyright 2019 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.bigtable;
import com.google.api.gax.rpc.NotFoundException;
import com.google.bigtable.admin.v2.Cluster;
import com.google.bigtable.admin.v2.CreateClusterRequest;
import com.google.bigtable.admin.v2.CreateInstanceRequest;
import com.google.bigtable.admin.v2.DeleteClusterRequest;
import com.google.bigtable.admin.v2.DeleteInstanceRequest;
import com.google.bigtable.admin.v2.GetInstanceRequest;
import com.google.bigtable.admin.v2.Instance;
import com.google.bigtable.admin.v2.ListClustersRequest;
import com.google.bigtable.admin.v2.ListClustersResponse;
import com.google.bigtable.admin.v2.ListInstancesRequest;
import com.google.bigtable.admin.v2.ListInstancesResponse;
import com.google.bigtable.admin.v2.StorageType;
import com.google.cloud.bigtable.admin.v2.BigtableInstanceAdminClient;
import com.google.cloud.bigtable.admin.v2.BigtableInstanceAdminSettings;
import java.io.IOException;
import java.util.Map;
/**
* An example of using Google Cloud Bigtable.
*
* <p>This example demonstrates the usage of BigtableInstanceAdminClient to create, configure, and
* delete Cloud Bigtable Instances and Clusters.
*
* <ul>
* <li>creates production instance
* <li>lists instances
* <li>gets instance
* <li>lists clusters
* <li>adds cluster
* <li>deletes cluster
* <li>deletes instance
* </ul>
*/
public class InstanceAdminExample {
private static final String CLUSTER = "cluster";
private final String projectId;
private final String clusterId;
private final String instanceId;
private final BigtableInstanceAdminClient adminClient;
public static void main(String[] args) throws IOException {
if (args.length != 1) {
System.out.println("Missing required project id");
return;
}
String projectId = args[0];
InstanceAdminExample instanceAdmin =
new InstanceAdminExample(projectId, "ssd-instance", "ssd-cluster");
instanceAdmin.run();
}
public InstanceAdminExample(String projectId, String instanceId, String clusterId)
throws IOException {
this.projectId = projectId;
this.instanceId = instanceId;
this.clusterId = clusterId;
// Creates the settings to configure a bigtable instance admin client.
BigtableInstanceAdminSettings instanceAdminSettings =
BigtableInstanceAdminSettings.newBuilder().setProjectId(projectId).build();
// Creates a bigtable instance admin client.
adminClient = BigtableInstanceAdminClient.create(instanceAdminSettings);
}
public void run() {
createProdInstance();
/* * OPTIONAL: Testing with tags
* If you want to test creating an instance with resource tags, comment out
* createProdInstance() above and uncomment createProdInstanceWithTags() below.
*/
// createProdInstanceWithTags();
listInstances();
getInstance();
listClusters();
addCluster();
deleteCluster();
deleteInstance();
close();
}
// Close the client
void close() {
adminClient.close();
}
/** Demonstrates how to create an instance within a provided project. */
public void createProdInstance() {
// Checks if instance exists, creates instance if does not exists.
if (!adminClient.exists(instanceId)) {
System.out.println("Instance does not exist, creating a PRODUCTION instance");
// [START bigtable_create_prod_instance]
// Creates a Production Instance with the ID "ssd-instance",
// cluster id "ssd-cluster", 3 nodes and location "us-central1-f".
String parent = "projects/" + projectId;
Instance instanceObj =
Instance.newBuilder()
.setDisplayName(instanceId)
.setType(Instance.Type.PRODUCTION)
.putLabels("department", "accounting")
.build();
Cluster clusterObj =
Cluster.newBuilder()
.setLocation("projects/" + projectId + "/locations/us-central1-f")
.setServeNodes(3)
.setDefaultStorageType(StorageType.SSD)
.build();
CreateInstanceRequest request =
CreateInstanceRequest.newBuilder()
.setParent(parent)
.setInstanceId(instanceId)
.setInstance(instanceObj)
.putClusters(clusterId, clusterObj)
.build();
// Creates a production instance with the given request.
try {
Instance instance = adminClient.getBaseClient().createInstanceAsync(request).get();
System.out.printf("PRODUCTION type instance %s created successfully%n", instance.getName());
} catch (Exception e) {
System.err.println("Failed to create instance: " + e.getMessage());
throw new RuntimeException(e);
}
// [END bigtable_create_prod_instance]
}
}
/**
* Demonstrates how to create a Production instance within a provided project with tags.
*
* <p>Tags are a way to organize and govern resources across Google Cloud, see
* [Creating and managing tags](https://cloud.google.com/resource-manager/docs/tags/tags-creating-and-managing)
*
*
* NOTE: Unlike labels, a tag (Key and Value) must be created before it can be
* attached to a resource.
* See [Creating and managing tags](https://cloud.google.com/resource-manager/docs/tags/tags-creating-and-managing)
* and [Tags overview](https://docs.cloud.google.com/bigtable/docs/tags) for more information.
*/
public void createProdInstanceWithTags() {
// Creates an instance if it doesn't exist.
if (!adminClient.exists(instanceId)) {
System.out.println("Instance does not exist, creating a PRODUCTION instance with tags");
// These are placeholders. You must create these in your GCP Organization/Project first.
String tagKey = "tagKeys/12345";
String tagValue = "tagValues/6789";
// [START bigtable_create_prod_instance_with_tags]
// Creates a Production Instance with the ID "ssd-instance",
// cluster id "ssd-cluster", 3 nodes and location "us-central1-f".
String parent = "projects/" + projectId;
Instance instanceObj =
Instance.newBuilder()
.setDisplayName(instanceId)
.setType(Instance.Type.PRODUCTION)
.putLabels("department", "accounting")
.putTags(tagKey, tagValue)
.build();
Cluster clusterObj =
Cluster.newBuilder()
.setLocation("projects/" + projectId + "/locations/us-central1-f")
.setServeNodes(3)
.setDefaultStorageType(StorageType.SSD)
.build();
CreateInstanceRequest request =
CreateInstanceRequest.newBuilder()
.setParent(parent)
.setInstanceId(instanceId)
.setInstance(instanceObj)
.putClusters(clusterId, clusterObj)
.build();
// Creates a production instance with the given request.
try {
Instance instance = adminClient.getBaseClient().createInstanceAsync(request).get();
System.out.printf("PRODUCTION type instance %s with tags created successfully%n", instance.getName());
} catch (Exception e) {
System.err.println("Failed to create instance: " + e.getMessage());
throw new RuntimeException(e);
}
// [END bigtable_create_prod_instance_with_tags]
}
}
/** Demonstrates how to list all instances within a project. */
public void listInstances() {
System.out.println("\nListing Instances");
// [START bigtable_list_instances]
try {
String parent = "projects/" + projectId;
ListInstancesRequest request = ListInstancesRequest.newBuilder().setParent(parent).build();
ListInstancesResponse response = adminClient.getBaseClient().listInstances(request);
for (Instance instance : response.getInstancesList()) {
System.out.println(instance.getName());
}
} catch (Exception e) {
System.err.println("Failed to list instances: " + e.getMessage());
}
// [END bigtable_list_instances]
}
/** Demonstrates how to get an instance. */
public Instance getInstance() {
System.out.println("\nGet Instance");
// [START bigtable_get_instance]
Instance instance = null;
try {
String name = "projects/" + projectId + "/instances/" + instanceId;
GetInstanceRequest request = GetInstanceRequest.newBuilder().setName(name).build();
instance = adminClient.getBaseClient().getInstance(request);
System.out.println("Instance ID: " + instance.getName());
System.out.println("Display Name: " + instance.getDisplayName());
System.out.print("Labels: ");
Map<String, String> labels = instance.getLabelsMap();
for (String key : labels.keySet()) {
System.out.printf("%s - %s", key, labels.get(key));
}
System.out.println("\nState: " + instance.getState());
System.out.println("Type: " + instance.getType());
} catch (NotFoundException e) {
System.err.println("Failed to get non-existent instance: " + e.getMessage());
}
// [END bigtable_get_instance]
return instance;
}
/** Demonstrates how to list clusters within an instance. */
public void listClusters() {
System.out.println("\nListing Clusters");
// [START bigtable_get_clusters]
try {
String parent = "projects/" + projectId + "/instances/" + instanceId;
ListClustersRequest request = ListClustersRequest.newBuilder().setParent(parent).build();
ListClustersResponse response = adminClient.getBaseClient().listClusters(request);
for (Cluster cluster : response.getClustersList()) {
System.out.println(cluster.getName());
}
} catch (NotFoundException e) {
System.err.println("Failed to list clusters from a non-existent instance: " + e.getMessage());
}
// [END bigtable_get_clusters]
}
/** Demonstrates how to delete an instance. */
public void deleteInstance() {
System.out.println("\nDeleting Instance");
// [START bigtable_delete_instance]
try {
String name = "projects/" + projectId + "/instances/" + instanceId;
DeleteInstanceRequest request = DeleteInstanceRequest.newBuilder().setName(name).build();
adminClient.getBaseClient().deleteInstance(request);
System.out.println("Instance deleted: " + instanceId);
} catch (NotFoundException e) {
System.err.println("Failed to delete non-existent instance: " + e.getMessage());
}
// [END bigtable_delete_instance]
}
/** Demonstrates how to add a cluster to an instance. */
public void addCluster() {
System.out.printf("%nAdding cluster: %s to instance: %s%n", CLUSTER, instanceId);
// [START bigtable_create_cluster]
try {
String parent = "projects/" + projectId + "/instances/" + instanceId;
Cluster clusterObj =
Cluster.newBuilder()
.setLocation("projects/" + projectId + "/locations/us-central1-c")
.setServeNodes(3)
.setDefaultStorageType(StorageType.SSD)
.build();
CreateClusterRequest request =
CreateClusterRequest.newBuilder()
.setParent(parent)
.setClusterId(CLUSTER)
.setCluster(clusterObj)
.build();
adminClient.getBaseClient().createClusterAsync(request).get();
System.out.printf("Cluster: %s created successfully%n", CLUSTER);
} catch (Exception e) {
System.err.println("Failed to add cluster: " + e.getMessage());
}
// [END bigtable_create_cluster]
}
/** Demonstrates how to delete a cluster from an instance. */
public void deleteCluster() {
System.out.printf("%nDeleting cluster: %s from instance: %s%n", CLUSTER, instanceId);
// [START bigtable_delete_cluster]
try {
String name = "projects/" + projectId + "/instances/" + instanceId + "/clusters/" + CLUSTER;
DeleteClusterRequest request = DeleteClusterRequest.newBuilder().setName(name).build();
adminClient.getBaseClient().deleteCluster(request);
System.out.printf("Cluster: %s deleted successfully%n", CLUSTER);
} catch (NotFoundException e) {
System.err.println("Failed to delete a non-existent cluster: " + e.getMessage());
}
// [END bigtable_delete_cluster]
}
}