Skip to content

Commit 514b3ab

Browse files
samples fixes
1 parent 5782c53 commit 514b3ab

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

google-cloud-examples/src/main/java/com/google/cloud/examples/bigtable/HelloWorld.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public static void main(String... args) throws Exception {
4545
}
4646

4747
// [START connecting_to_bigtable]
48-
// create the settings to configure a bigtable data client
48+
// Create the settings to configure a bigtable data client
4949
BigtableDataSettings settings = BigtableDataSettings.newBuilder()
5050
.setInstanceName(InstanceName.of(GCLOUD_PROJECT_ID, INSTANCE_ID)).build();
5151

@@ -87,17 +87,28 @@ public static void main(String... args) throws Exception {
8787
public static Table createTable(BigtableTableAdminClient adminClient, String TABLE_ID,
8888
String COLUMN_FAMILY_ID) {
8989
// [START creating_a_table]
90-
Table table = null;
90+
Table table;
9191
if (!adminClient.exists(TABLE_ID)) {
92-
CreateTableRequest createTableRequest =
93-
CreateTableRequest.of(TABLE_ID).addFamily(COLUMN_FAMILY_ID);
94-
System.out.println("Creating table: " + TABLE_ID);
95-
table = adminClient.createTable(createTableRequest);
92+
table = creatingTable(adminClient, TABLE_ID, COLUMN_FAMILY_ID);
93+
} else {
94+
// Delete and recreate table
95+
System.out.println("Deleting existing table and creating new one");
96+
adminClient.deleteTable(TABLE_ID);
97+
table = creatingTable(adminClient, TABLE_ID, COLUMN_FAMILY_ID);
9698
}
9799
return table;
98100
// [END creating_a_table]
99101
}
100102

103+
private static Table creatingTable(BigtableTableAdminClient adminClient, String TABLE_ID,
104+
String COLUMN_FAMILY_ID) {
105+
CreateTableRequest createTableRequest =
106+
CreateTableRequest.of(TABLE_ID).addFamily(COLUMN_FAMILY_ID);
107+
System.out.println("Creating table: " + TABLE_ID);
108+
Table table = adminClient.createTable(createTableRequest);
109+
return table;
110+
}
111+
101112
public static RowMutation writeToTable(BigtableDataClient dataClient, String TABLE_ID,
102113
String ROW_KEY_PREFIX, String COLUMN_FAMILY_ID, String COLUMN_QUALIFIER) {
103114
// [START writing_rows]
@@ -169,4 +180,4 @@ public static void deleteTable(BigtableTableAdminClient adminClient, String TABL
169180
}
170181
// [END deleting_a_table]
171182
}
172-
}
183+
}

google-cloud-examples/src/main/java/com/google/cloud/examples/bigtable/InstanceAdmin.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,15 @@ public static Instance createProdInstance(BigtableInstanceAdminClient adminClien
8888
} catch (Exception e) {
8989
System.out.println("Error checking if instance exists: " + e.getMessage());
9090
}
91+
// [END bigtable_check_instance_exists]
9192

9293
Instance instance = null;
93-
// Create instance if does not exists
94+
// Create an instance if does not exists
9495
if (!found) {
9596
System.out.println("Instance does not exist, creating a PRODUCTION instance:");
9697
// [START bigtable_create_prod_instance]
97-
// Creates a Production Instance with the ID "ssd-instance"
98-
// with cluster id "ssd-cluster", 3 nodes and location us-central1-f
98+
// Create a Production Instance with the ID "ssd-instance"
99+
// cluster id "ssd-cluster", 3 nodes and location us-central1-f
99100
CreateInstanceRequest createInstanceRequest = CreateInstanceRequest.of(instanceID)
100101
.addCluster(clusterID, "us-central1-f", 3, StorageType.SSD).setType(Type.PRODUCTION)
101102
.addLabel("example", "instance_admin");
@@ -203,7 +204,6 @@ public static Cluster addCluster(BigtableInstanceAdminClient adminClient, String
203204

204205
public static void deleteCluster(BigtableInstanceAdminClient adminClient, String instanceID,
205206
String clusterID) {
206-
// [START bigtable_delete_cluster]
207207
System.out.println("\nDeleting Cluster");
208208
// [START bigtable_delete_cluster]
209209
try {

google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/HelloWorldTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,11 @@ public static void afterClass() throws Exception {
7171
@Test
7272
public void testCreateAndDeleteTable() {
7373
// Create table
74-
Table table = TableAdmin.createTable(adminClient, "fake-table");
74+
Table table = HelloWorld.createTable(adminClient, "fake-table", "fake-column-family-id");
7575
assertNotNull(table);
7676

7777
// Delete table
78-
TableAdmin.deleteTable(adminClient, "fake-table");
78+
HelloWorld.deleteTable(adminClient, "fake-table");
7979
assertTrue(!adminClient.exists("fake-table"));
8080
}
8181

0 commit comments

Comments
 (0)