Skip to content

Commit 58f8726

Browse files
Java samples
1 parent 9fdb163 commit 58f8726

File tree

4 files changed

+582
-0
lines changed

4 files changed

+582
-0
lines changed

google-cloud-examples/pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@
8989
<version>4.12</version>
9090
<scope>test</scope>
9191
</dependency>
92+
<dependency>
93+
<groupId>com.google.cloud</groupId>
94+
<artifactId>google-cloud-bigtable</artifactId>
95+
</dependency>
96+
<dependency>
97+
<groupId>com.google.cloud</groupId>
98+
<artifactId>google-cloud-bigtable-admin</artifactId>
99+
</dependency>
92100
</dependencies>
93101
<build>
94102
<plugins>
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package com.google.cloud.examples.bigtable;
2+
3+
import com.google.api.gax.rpc.ServerStream;
4+
import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient;
5+
import com.google.cloud.bigtable.admin.v2.BigtableTableAdminSettings;
6+
import com.google.cloud.bigtable.admin.v2.models.CreateTableRequest;
7+
import com.google.cloud.bigtable.data.v2.BigtableDataClient;
8+
import com.google.cloud.bigtable.data.v2.BigtableDataSettings;
9+
import com.google.cloud.bigtable.data.v2.models.InstanceName;
10+
11+
import com.google.cloud.bigtable.data.v2.models.Query;
12+
import com.google.cloud.bigtable.data.v2.models.Row;
13+
import com.google.cloud.bigtable.data.v2.models.RowCell;
14+
import com.google.cloud.bigtable.data.v2.models.RowMutation;
15+
import java.sql.Timestamp;
16+
17+
// [START HelloWorld]
18+
public class HelloWorld {
19+
20+
public static void main(String[] args) throws Exception {
21+
22+
// the ID of the cloud bigtable project, instance and table
23+
String TABLE_ID = "Hello-Bigtable";
24+
String COLUMN_FAMILY_ID = "cf1";
25+
String COLUMN_QUALIFIER = "greeting";
26+
String INSTANCE_ID = System.getenv("INSTANCE_ID");
27+
String GCLOUD_PROJECT_ID = System.getenv("GCLOUD_PROJECT_ID");
28+
final String rowKeyPrefix = "rowKey";
29+
30+
if (INSTANCE_ID.length() == 0) {
31+
System.out.println("Environment variables for INSTANCE_ID must be set!");
32+
}
33+
if (GCLOUD_PROJECT_ID.length() == 0) {
34+
throw new Error("Environment variables GCLOUD_PROJECT_ID must be set!");
35+
}
36+
37+
// create the settings to configure a bigtable data client
38+
BigtableDataSettings settings = BigtableDataSettings.newBuilder()
39+
.setInstanceName(InstanceName.of(GCLOUD_PROJECT_ID, INSTANCE_ID)).build();
40+
41+
// create bigtable data client
42+
BigtableDataClient dataClient = BigtableDataClient.create(settings);
43+
44+
BigtableTableAdminSettings adminSettings = BigtableTableAdminSettings.newBuilder()
45+
.setInstanceName(
46+
com.google.bigtable.admin.v2.InstanceName.of(GCLOUD_PROJECT_ID, INSTANCE_ID)).build();
47+
48+
BigtableTableAdminClient adminClient = BigtableTableAdminClient.create(adminSettings);
49+
50+
if (!adminClient.exists(TABLE_ID)) {
51+
CreateTableRequest createTableRequest =
52+
CreateTableRequest.of(TABLE_ID).addFamily(COLUMN_FAMILY_ID);
53+
System.out.println("Creating table " + TABLE_ID);
54+
adminClient.createTable(createTableRequest);
55+
}
56+
57+
try {
58+
System.out.println("Write some greetings to the table");
59+
String[] greetings = { "Hello World!", "Hello Bigtable!", "Hello Node!" };
60+
for (int i = 0; i < greetings.length; i++) {
61+
RowMutation rowMutation = RowMutation.create(TABLE_ID, rowKeyPrefix + i);
62+
long timestamp = new Timestamp(System.currentTimeMillis()).getTime();
63+
rowMutation.setCell(COLUMN_FAMILY_ID, COLUMN_QUALIFIER, timestamp, greetings[i]);
64+
dataClient.mutateRow(rowMutation);
65+
}
66+
67+
System.out.println("Reading a single row by row key");
68+
Row row = dataClient.readRow(TABLE_ID, rowKeyPrefix + 1);
69+
System.out.println(row.getKey().toStringUtf8());
70+
for (RowCell cell : row.getCells()) {
71+
System.out.println(
72+
"Family: " + cell.getFamily() + " Qualifier: " + cell.getQualifier().toStringUtf8()
73+
+ " Timestamp: " + cell.getTimestamp() + " Value: " + cell.getValue()
74+
.toStringUtf8());
75+
}
76+
77+
System.out.println("Reading the entire table");
78+
Query query = Query.create(TABLE_ID);
79+
ServerStream<Row> rowStream = dataClient.readRows(query);
80+
for (Row r : rowStream) {
81+
System.out.println("Row Key: " + r.getKey());
82+
for (RowCell cell : r.getCells()) {
83+
System.out.println(
84+
"Family: " + cell.getFamily() + " Qualifier: " + cell.getQualifier().toStringUtf8()
85+
+ " Timestamp: " + cell.getTimestamp() + " Value: " + cell.getValue()
86+
.toStringUtf8());
87+
}
88+
}
89+
90+
System.out.println("Delete the table");
91+
adminClient.deleteTable(TABLE_ID);
92+
93+
} catch (Exception e) {
94+
System.out.println("Exception while running HelloWorld: " + e.getMessage());
95+
} finally {
96+
dataClient.close();
97+
adminClient.close();
98+
}
99+
}
100+
}
101+
// [END HelloWorld]
Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
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

Comments
 (0)