From 514b3abb2a65336ef5622b16f961d233da0b86e6 Mon Sep 17 00:00:00 2001 From: elisheva-qlogic Date: Thu, 6 Dec 2018 16:28:44 -0500 Subject: [PATCH] samples fixes --- .../cloud/examples/bigtable/HelloWorld.java | 25 +++++++++++++------ .../examples/bigtable/InstanceAdmin.java | 8 +++--- .../examples/bigtable/HelloWorldTest.java | 4 +-- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/bigtable/HelloWorld.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/bigtable/HelloWorld.java index b38f543177a2..a54d8f905fcb 100644 --- a/google-cloud-examples/src/main/java/com/google/cloud/examples/bigtable/HelloWorld.java +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/bigtable/HelloWorld.java @@ -45,7 +45,7 @@ public static void main(String... args) throws Exception { } // [START connecting_to_bigtable] - // create the settings to configure a bigtable data client + // Create the settings to configure a bigtable data client BigtableDataSettings settings = BigtableDataSettings.newBuilder() .setInstanceName(InstanceName.of(GCLOUD_PROJECT_ID, INSTANCE_ID)).build(); @@ -87,17 +87,28 @@ public static void main(String... args) throws Exception { public static Table createTable(BigtableTableAdminClient adminClient, String TABLE_ID, String COLUMN_FAMILY_ID) { // [START creating_a_table] - Table table = null; + Table table; if (!adminClient.exists(TABLE_ID)) { - CreateTableRequest createTableRequest = - CreateTableRequest.of(TABLE_ID).addFamily(COLUMN_FAMILY_ID); - System.out.println("Creating table: " + TABLE_ID); - table = adminClient.createTable(createTableRequest); + table = creatingTable(adminClient, TABLE_ID, COLUMN_FAMILY_ID); + } else { + // Delete and recreate table + System.out.println("Deleting existing table and creating new one"); + adminClient.deleteTable(TABLE_ID); + table = creatingTable(adminClient, TABLE_ID, COLUMN_FAMILY_ID); } return table; // [END creating_a_table] } + private static Table creatingTable(BigtableTableAdminClient adminClient, String TABLE_ID, + String COLUMN_FAMILY_ID) { + CreateTableRequest createTableRequest = + CreateTableRequest.of(TABLE_ID).addFamily(COLUMN_FAMILY_ID); + System.out.println("Creating table: " + TABLE_ID); + Table table = adminClient.createTable(createTableRequest); + return table; + } + public static RowMutation writeToTable(BigtableDataClient dataClient, String TABLE_ID, String ROW_KEY_PREFIX, String COLUMN_FAMILY_ID, String COLUMN_QUALIFIER) { // [START writing_rows] @@ -169,4 +180,4 @@ public static void deleteTable(BigtableTableAdminClient adminClient, String TABL } // [END deleting_a_table] } -} +} \ No newline at end of file diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/bigtable/InstanceAdmin.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/bigtable/InstanceAdmin.java index 0bd48f4cfe38..e142debf179a 100644 --- a/google-cloud-examples/src/main/java/com/google/cloud/examples/bigtable/InstanceAdmin.java +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/bigtable/InstanceAdmin.java @@ -88,14 +88,15 @@ public static Instance createProdInstance(BigtableInstanceAdminClient adminClien } catch (Exception e) { System.out.println("Error checking if instance exists: " + e.getMessage()); } + // [END bigtable_check_instance_exists] Instance instance = null; - // Create instance if does not exists + // Create an instance if does not exists if (!found) { 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" - // with cluster id "ssd-cluster", 3 nodes and location us-central1-f + // Create a Production Instance with the ID "ssd-instance" + // cluster id "ssd-cluster", 3 nodes and location us-central1-f CreateInstanceRequest createInstanceRequest = CreateInstanceRequest.of(instanceID) .addCluster(clusterID, "us-central1-f", 3, StorageType.SSD).setType(Type.PRODUCTION) .addLabel("example", "instance_admin"); @@ -203,7 +204,6 @@ public static Cluster addCluster(BigtableInstanceAdminClient adminClient, String public static void deleteCluster(BigtableInstanceAdminClient adminClient, String instanceID, String clusterID) { - // [START bigtable_delete_cluster] System.out.println("\nDeleting Cluster"); // [START bigtable_delete_cluster] try { diff --git a/google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/HelloWorldTest.java b/google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/HelloWorldTest.java index b769a671c9be..2b3e8c277e0b 100644 --- a/google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/HelloWorldTest.java +++ b/google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/HelloWorldTest.java @@ -71,11 +71,11 @@ public static void afterClass() throws Exception { @Test public void testCreateAndDeleteTable() { // Create table - Table table = TableAdmin.createTable(adminClient, "fake-table"); + Table table = HelloWorld.createTable(adminClient, "fake-table", "fake-column-family-id"); assertNotNull(table); // Delete table - TableAdmin.deleteTable(adminClient, "fake-table"); + HelloWorld.deleteTable(adminClient, "fake-table"); assertTrue(!adminClient.exists("fake-table")); }