From b24d42f53df9aa34a1308120cf3858d2a344f497 Mon Sep 17 00:00:00 2001 From: elisheva-qlogic Date: Thu, 15 Nov 2018 10:27:40 -0500 Subject: [PATCH 01/10] Java samples --- google-cloud-examples/pom.xml | 8 + .../cloud/examples/bigtable/HelloWorld.java | 101 +++++++ .../examples/bigtable/InstanceAdmin.java | 213 ++++++++++++++ .../cloud/examples/bigtable/TableAdmin.java | 260 ++++++++++++++++++ 4 files changed, 582 insertions(+) create mode 100644 google-cloud-examples/src/main/java/com/google/cloud/examples/bigtable/HelloWorld.java create mode 100644 google-cloud-examples/src/main/java/com/google/cloud/examples/bigtable/InstanceAdmin.java create mode 100644 google-cloud-examples/src/main/java/com/google/cloud/examples/bigtable/TableAdmin.java diff --git a/google-cloud-examples/pom.xml b/google-cloud-examples/pom.xml index 4aded1d268bb..9f4a24b3f0ea 100644 --- a/google-cloud-examples/pom.xml +++ b/google-cloud-examples/pom.xml @@ -89,6 +89,14 @@ 4.12 test + + com.google.cloud + google-cloud-bigtable + + + com.google.cloud + google-cloud-bigtable-admin + 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 new file mode 100644 index 000000000000..fc11a21e325c --- /dev/null +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/bigtable/HelloWorld.java @@ -0,0 +1,101 @@ +package com.google.cloud.examples.bigtable; + +import com.google.api.gax.rpc.ServerStream; +import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient; +import com.google.cloud.bigtable.admin.v2.BigtableTableAdminSettings; +import com.google.cloud.bigtable.admin.v2.models.CreateTableRequest; +import com.google.cloud.bigtable.data.v2.BigtableDataClient; +import com.google.cloud.bigtable.data.v2.BigtableDataSettings; +import com.google.cloud.bigtable.data.v2.models.InstanceName; + +import com.google.cloud.bigtable.data.v2.models.Query; +import com.google.cloud.bigtable.data.v2.models.Row; +import com.google.cloud.bigtable.data.v2.models.RowCell; +import com.google.cloud.bigtable.data.v2.models.RowMutation; +import java.sql.Timestamp; + +// [START HelloWorld] +public class HelloWorld { + + public static void main(String[] args) throws Exception { + + // the ID of the cloud bigtable project, instance and table + String TABLE_ID = "Hello-Bigtable"; + String COLUMN_FAMILY_ID = "cf1"; + String COLUMN_QUALIFIER = "greeting"; + String INSTANCE_ID = System.getenv("INSTANCE_ID"); + String GCLOUD_PROJECT_ID = System.getenv("GCLOUD_PROJECT_ID"); + final String rowKeyPrefix = "rowKey"; + + if (INSTANCE_ID.length() == 0) { + System.out.println("Environment variables for INSTANCE_ID must be set!"); + } + if (GCLOUD_PROJECT_ID.length() == 0) { + throw new Error("Environment variables GCLOUD_PROJECT_ID must be set!"); + } + + // create the settings to configure a bigtable data client + BigtableDataSettings settings = BigtableDataSettings.newBuilder() + .setInstanceName(InstanceName.of(GCLOUD_PROJECT_ID, INSTANCE_ID)).build(); + + // create bigtable data client + BigtableDataClient dataClient = BigtableDataClient.create(settings); + + BigtableTableAdminSettings adminSettings = BigtableTableAdminSettings.newBuilder() + .setInstanceName( + com.google.bigtable.admin.v2.InstanceName.of(GCLOUD_PROJECT_ID, INSTANCE_ID)).build(); + + BigtableTableAdminClient adminClient = BigtableTableAdminClient.create(adminSettings); + + if (!adminClient.exists(TABLE_ID)) { + CreateTableRequest createTableRequest = + CreateTableRequest.of(TABLE_ID).addFamily(COLUMN_FAMILY_ID); + System.out.println("Creating table " + TABLE_ID); + adminClient.createTable(createTableRequest); + } + + try { + System.out.println("Write some greetings to the table"); + String[] greetings = { "Hello World!", "Hello Bigtable!", "Hello Node!" }; + for (int i = 0; i < greetings.length; i++) { + RowMutation rowMutation = RowMutation.create(TABLE_ID, rowKeyPrefix + i); + long timestamp = new Timestamp(System.currentTimeMillis()).getTime(); + rowMutation.setCell(COLUMN_FAMILY_ID, COLUMN_QUALIFIER, timestamp, greetings[i]); + dataClient.mutateRow(rowMutation); + } + + System.out.println("Reading a single row by row key"); + Row row = dataClient.readRow(TABLE_ID, rowKeyPrefix + 1); + System.out.println(row.getKey().toStringUtf8()); + for (RowCell cell : row.getCells()) { + System.out.println( + "Family: " + cell.getFamily() + " Qualifier: " + cell.getQualifier().toStringUtf8() + + " Timestamp: " + cell.getTimestamp() + " Value: " + cell.getValue() + .toStringUtf8()); + } + + System.out.println("Reading the entire table"); + Query query = Query.create(TABLE_ID); + ServerStream rowStream = dataClient.readRows(query); + for (Row r : rowStream) { + System.out.println("Row Key: " + r.getKey()); + for (RowCell cell : r.getCells()) { + System.out.println( + "Family: " + cell.getFamily() + " Qualifier: " + cell.getQualifier().toStringUtf8() + + " Timestamp: " + cell.getTimestamp() + " Value: " + cell.getValue() + .toStringUtf8()); + } + } + + System.out.println("Delete the table"); + adminClient.deleteTable(TABLE_ID); + + } catch (Exception e) { + System.out.println("Exception while running HelloWorld: " + e.getMessage()); + } finally { + dataClient.close(); + adminClient.close(); + } + } +} +// [END HelloWorld] \ 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 new file mode 100644 index 000000000000..714ca196056d --- /dev/null +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/bigtable/InstanceAdmin.java @@ -0,0 +1,213 @@ +package com.google.cloud.examples.bigtable; + +import com.google.bigtable.admin.v2.ProjectName; +import com.google.cloud.bigtable.admin.v2.BigtableInstanceAdminClient; +import com.google.cloud.bigtable.admin.v2.BigtableInstanceAdminSettings; +import com.google.cloud.bigtable.admin.v2.models.Cluster; +import com.google.cloud.bigtable.admin.v2.models.CreateClusterRequest; +import com.google.cloud.bigtable.admin.v2.models.CreateInstanceRequest; +import com.google.cloud.bigtable.admin.v2.models.Instance; +import com.google.cloud.bigtable.admin.v2.models.Instance.Type; +import com.google.cloud.bigtable.admin.v2.models.StorageType; +import java.io.IOException; +import java.util.List; +import java.util.Map; + +public class InstanceAdmin { + + public static void main(String[] args) throws IOException { + + String GCLOUD_PROJECT = System.getenv("GCLOUD_PROJECT"); + + if (GCLOUD_PROJECT.length() == 0) { + throw new Error("Environment variables GCLOUD_PROJECT must be set!"); + } + + BigtableInstanceAdminSettings instanceAdminSettings = + BigtableInstanceAdminSettings.newBuilder().setProjectName(ProjectName.of(GCLOUD_PROJECT)) + .build(); + + BigtableInstanceAdminClient adminClient = + BigtableInstanceAdminClient.create(instanceAdminSettings); + + System.out.println("Create an instance (type: PRODUCTION) and run basic instance-operations"); + runInstanceOperations(adminClient, "ssd-instance", "ssd-cluster"); + + System.out.println("Create DEVELOPMENT instance"); + createDevInstance(adminClient, "hdd-instance", "hdd-cluster"); + + System.out.println("Delete the Instance"); + deleteInstance(adminClient, "hdd-instance"); + + System.out.println("Add Cluster"); + addCluster(adminClient, "ssd-instance", "ssd-cluster"); + + System.out.println("Delete the Cluster"); + deleteCluster(adminClient, "ssd-instance", "ssd-cluster"); + + // end operations with deleting the pro-instance created in `runInstanceOperations` + deleteInstance(adminClient, "ssd-instance"); + } + + public static void runInstanceOperations(BigtableInstanceAdminClient adminClient, + String instanceID, String clusterID) { + System.out.println("Check Instance Exists"); + // [START bigtable_check_instance_exists] + boolean found = false; + try { + found = adminClient.exists(instanceID); + } catch (Exception e) { + System.out.println("Error checking if Instance exists: " + e.getMessage()); + } + + // Create instance if does not exists + if (!found) { + System.out.println("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 + CreateInstanceRequest createInstanceRequest = CreateInstanceRequest.of(instanceID) + .addCluster(clusterID, "us-central1-f", 3, StorageType.SSD).setType(Type.PRODUCTION) + .addLabel("prod-label", "prod-label"); + // Create production instance with given request + try { + Instance instance = adminClient.createInstance(createInstanceRequest); + System.out + .println("PRODUCTION type instance : " + instance.getId() + " created successfully"); + + } catch (Exception e) { + System.out.println("Error creating prod-instance: " + e.getMessage()); + } + // [END bigtable_create_prod_instance] + } else { + System.out.println("Instance " + instanceID + " exists"); + } + + System.out.println(); //for a new-line + System.out.println("Listing Instances:"); + // [START bigtable_list_instances] + try { + List instances = adminClient.listInstances(); + for (Instance instance : instances) { + System.out.println(instance.getId()); + } + } catch (Exception e) { + System.out.println("Error listing instances: " + e.getMessage()); + } + // [END bigtable_list_instances] + + System.out.println(); //for a new-line + System.out.println("Get Instance:"); + // [START bigtable_get_instance] + try { + Instance instance = adminClient.getInstance(instanceID); + System.out.println("Instance ID: " + instance.getId()); + System.out.println("Instance Meta:"); + System.out.println("Display Name: " + instance.getDisplayName()); + System.out.println("Labels:"); + Map labels = instance.getLabels(); + for (String key : labels.keySet()) { + System.out.println(key + ": " + labels.get(key)); + } + System.out.println("State: " + instance.getState()); + System.out.println("Type: " + instance.getType()); + } catch (Exception e) { + System.out.println("Error getting instance: " + e.getMessage()); + } + // [END bigtable_get_instance] + + System.out.println(); //for a new-line + System.out.println("Listing Clusters..."); + // [START bigtable_get_clusters] + try { + List clusters = adminClient.listClusters(instanceID); + for (Cluster cluster : clusters) { + System.out.println(cluster.getId()); + } + } catch (Exception e) { + System.out.println("Error listing clusters: " + e.getMessage()); + } + // [END bigtable_get_clusters] + } + + public static void createDevInstance(BigtableInstanceAdminClient adminClient, String instanceID, + String clusterID) { + // [START bigtable_create_dev_instance] + System.out.println(); //for a new-line + System.out.println("Creating a DEVELOPMENT Instance"); + // Creates a Development instance with the ID "hdd-instance" + // with cluster ID "hdd-cluster" and location us-central1-f + // Cluster nodes should not be set while creating Development Instance + CreateInstanceRequest createInstanceRequest = CreateInstanceRequest.of(instanceID) + .addCluster(clusterID, "us-central1-f", 1, StorageType.HDD).setType(Type.DEVELOPMENT) + .addLabel("dev-label", "dev-label"); + // Create development instance with given request + try { + Instance instance = adminClient.createInstance(createInstanceRequest); + System.out + .println("DEVELOPMENT type instance : " + instance.getId() + " created successfully"); + } catch (Exception e) { + System.out.println("Error creating dev-instance: " + e.getMessage()); + } + // [END bigtable_create_dev_instance] + } + + // Delete the Instance + public static void deleteInstance(BigtableInstanceAdminClient adminClient, String instanceID) { + // [START bigtable_delete_instance] + System.out.println(); //for a new-line + System.out.println("Deleting Instance"); + try { + adminClient.deleteInstance(instanceID); + System.out.println("Instance deleted: " + instanceID); + } catch (Exception e) { + System.out.println("Error deleting instance: " + instanceID); + } + // [END bigtable_delete_instance] + } + + // Add Cluster + public static void addCluster(BigtableInstanceAdminClient adminClient, String instanceID, + String clusterID) { + boolean found = false; + try { + found = adminClient.exists(instanceID); + } catch (Exception e) { + System.out.println("Error checking if Instance exists: " + e.getMessage()); + } + if (!found) { + System.out.println("Instance does not exist"); + } else { + System.out.println(); //for a new-line + System.out.println("Adding Cluster to Instance " + instanceID); + // [START bigtable_create_cluster] + CreateClusterRequest createClusterRequest = + CreateClusterRequest.of(instanceID, clusterID).setServeNodes(3) + .setStorageType(StorageType.SSD).setZone("us-central1-c"); + try { + Cluster cluster = adminClient.createCluster(createClusterRequest); + System.out.println("Cluster : " + cluster.getId() + " created successfully"); + } catch (Exception e) { + System.out.println("Error creating cluster: " + e.getMessage()); + } + // [END bigtable_create_cluster] + } + } + + // Delete the Cluster + public static void deleteCluster(BigtableInstanceAdminClient adminClient, String instanceID, + String clusterID) { + // [START bigtable_delete_cluster] + System.out.println(); //for a new-line + System.out.println("Deleting Cluster"); + // [START bigtable_delete_cluster] + try { + adminClient.deleteCluster(instanceID, clusterID); + System.out.println("Cluster : " + clusterID + " deleted successfully"); + } catch (Exception e) { + System.out.println("Error deleting cluster: " + e.getMessage()); + } + System.out.println("Cluster deleted: " + clusterID); + // [END bigtable_delete_cluster] + } +} \ No newline at end of file diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/bigtable/TableAdmin.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/bigtable/TableAdmin.java new file mode 100644 index 000000000000..4d4b1e745af5 --- /dev/null +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/bigtable/TableAdmin.java @@ -0,0 +1,260 @@ +package com.google.cloud.examples.bigtable; + +import com.google.bigtable.admin.v2.TableName; +import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient; +import com.google.cloud.bigtable.admin.v2.BigtableTableAdminSettings; +import com.google.cloud.bigtable.admin.v2.models.ColumnFamily; +import com.google.cloud.bigtable.admin.v2.models.CreateTableRequest; + +import com.google.cloud.bigtable.admin.v2.models.GCRules; +import com.google.cloud.bigtable.admin.v2.models.ModifyColumnFamiliesRequest; +import com.google.cloud.bigtable.admin.v2.models.Table; +import java.io.IOException; +import java.util.Collection; +import java.util.List; +import java.util.concurrent.TimeUnit; + +public class TableAdmin { + + public static void main(String[] args) { + + String GCLOUD_PROJECT = System.getenv("GCLOUD_PROJECT"); + String INSTANCE_ID = System.getenv("INSTANCE_ID"); + String TABLE_ID = "TABLE_ID"; + + if (GCLOUD_PROJECT.length() == 0 || INSTANCE_ID.length() == 0) { + throw new Error("Environment variables must be set!"); + } + + try { + BigtableTableAdminSettings adminSettings = BigtableTableAdminSettings.newBuilder() + .setInstanceName( + com.google.bigtable.admin.v2.InstanceName.of(GCLOUD_PROJECT, INSTANCE_ID)).build(); + + BigtableTableAdminClient adminClient = BigtableTableAdminClient.create(adminSettings); + + // Check if table exists + if (!adminClient.exists(TABLE_ID)) { + // Create table if does not exist + System.out.println("Table does not exist. Creating table " + TABLE_ID); + // Creating table + try { + CreateTableRequest createTableRequest = CreateTableRequest.of(TABLE_ID); + Table table = adminClient.createTable(createTableRequest); + System.out.println("Table : " + table.getId() + "created successfully"); + } catch (Exception ex) { + System.out.println("Error creating table: " + ex.getMessage()); + } + } else { + System.out.println("Table exists."); + } + + System.out.println("\nListing tables in current project..."); + // [START bigtable_list_tables] + // List tables in current instance + try { + List listTables = adminClient.listTables(); + for (TableName tableName : listTables) { + System.out.println(tableName.getTable()); + } + } catch (Exception ex) { + System.out.println("Error listing tables in current project: " + ex.getMessage()); + } + // [END bigtable_list_tables] + + System.out.println("\nPrinting table metadata..."); + // [START bigtable_get_table_metadata] + // Get table metadata, and apply a view to the table fields + try { + Table table = adminClient.getTable(TABLE_ID); + Collection columnFamilies = table.getColumnFamilies(); + for (ColumnFamily columnFamily : columnFamilies) { + printColumnFamily(columnFamily); + } + } catch (Exception ex) { + System.out.println("Error retrieving table metadata: " + ex.getMessage()); + } + // [END bigtable_get_table_metadata] + + System.out.println("\nCreating column family cf1 with max age GC rule..."); + // [START bigtable_create_family_gc_max_age] + // Create a column family with GC policy : maximum age + // where age = current time minus cell timestamp + + // Define the GC rule to retain data with max age of 5 days + GCRules.DurationRule maxAgeRule1 = GCRules.GCRULES.maxAge(5, TimeUnit.DAYS); + + // Create column family with given GC rule + try { + ModifyColumnFamiliesRequest columnFamiliesRequest1 = + ModifyColumnFamiliesRequest.of(TABLE_ID).addFamily("cf1", maxAgeRule1); + adminClient.modifyFamilies(columnFamiliesRequest1); + System.out.println("Created column family cf1"); + } catch (Exception ex) { + System.out.println("Error creating column family: " + ex.getMessage()); + } + // [END bigtable_create_family_gc_max_age] + + System.out.println("\nCreating column family cf2 with max versions GC rule..."); + // [START bigtable_create_family_gc_max_versions] + // Create a column family with GC policy : most recent N versions + // where 1 = most recent version + + // Define the GC policy to retain only the most recent 2 versions + GCRules.VersionRule versionRule1 = GCRules.GCRULES.maxVersions(2); + + // Create column family with given GC rule + try { + ModifyColumnFamiliesRequest columnFamiliesRequest2 = + ModifyColumnFamiliesRequest.of(TABLE_ID).addFamily("cf2", versionRule1); + adminClient.modifyFamilies(columnFamiliesRequest2); + System.out.println("Created column family cf2"); + } catch (Exception ex) { + System.out.println("Error creating column family: " + ex.getMessage()); + } + // [END bigtable_create_family_gc_max_versions] + + System.out.println("\nCreating column family cf3 with union GC rule..."); + // [START bigtable_create_family_gc_union] + // Create a column family with GC policy to drop data that matches at least one condition. + + // Define a GC rule to drop cells older than 5 days OR not the most recent version + GCRules.DurationRule maxAgeRule2 = GCRules.GCRULES.maxAge(5, TimeUnit.DAYS); + GCRules.VersionRule versionRule2 = GCRules.GCRULES.maxVersions(1); + // Add rules to union rule list + GCRules.UnionRule unionRule1 = GCRules.GCRULES.union().rule(maxAgeRule2).rule(versionRule2); + + // Create column family with given GC rule + try { + ModifyColumnFamiliesRequest columnFamiliesRequest3 = + ModifyColumnFamiliesRequest.of(TABLE_ID).addFamily("cf3", unionRule1); + adminClient.modifyFamilies(columnFamiliesRequest3); + System.out.println("Created column family cf3"); + } catch (Exception ex) { + System.out.println("Error creating column family: " + ex.getMessage()); + } + // [END bigtable_create_family_gc_union] + + System.out.println("\nCreating column family cf4 with intersection GC rule..."); + // [START bigtable_create_family_gc_intersection] + // Create a column family with GC policy to drop data that matches all conditions + + // GC rule: Drop cells older than 5 days AND older than the most recent 2 versions + GCRules.DurationRule maxAgeRule3 = GCRules.GCRULES.maxAge(5, TimeUnit.DAYS); + GCRules.VersionRule versionRule3 = GCRules.GCRULES.maxVersions(2); + GCRules.IntersectionRule intersectionRule1 = + GCRules.GCRULES.intersection().rule(maxAgeRule3).rule(versionRule3); + + // Create column family with given GC rule + try { + ModifyColumnFamiliesRequest columnFamiliesRequest4 = + ModifyColumnFamiliesRequest.of(TABLE_ID).addFamily("cf4", intersectionRule1); + adminClient.modifyFamilies(columnFamiliesRequest4); + System.out.println("Created column family cf4"); + } catch (Exception ex) { + System.out.println("Error creating column family: " + ex.getMessage()); + } + // [END bigtable_create_family_gc_intersection] + + System.out.println("\nCreating column family cf5 with a nested GC rule"); + // [START bigtable_create_family_gc_nested] + // Create a nested GC rule: + // Drop cells that are either older than the 10 recent versions + // OR + // Drop cells that are older than a month AND older than the 2 recent versions + GCRules.VersionRule versionRule4 = GCRules.GCRULES.maxVersions(10); + GCRules.DurationRule maxAgeRule4 = GCRules.GCRULES.maxAge(30, TimeUnit.DAYS); + GCRules.VersionRule versionRule5 = GCRules.GCRULES.maxVersions(2); + GCRules.IntersectionRule intersectionRule2 = + GCRules.GCRULES.intersection().rule(maxAgeRule4).rule(versionRule5); + GCRules.UnionRule unionRule2 = + GCRules.GCRULES.union().rule(intersectionRule2).rule(versionRule4); + + // Create column family with given GC rule + try { + ModifyColumnFamiliesRequest columnFamiliesRequest5 = + ModifyColumnFamiliesRequest.of(TABLE_ID).addFamily("cf5", unionRule2); + adminClient.modifyFamilies(columnFamiliesRequest5); + System.out.println("Created column family cf5"); + } catch (Exception ex) { + System.out.println("Error creating column family: " + ex.getMessage()); + } + // [END bigtable_create_family_gc_nested] + + System.out.println("\nPrinting ID and GC Rule for all column families..."); + // [START bigtable_list_column_families] + // List all families in the table with GC rules + try { + Table table = adminClient.getTable(TABLE_ID); + Collection columnFamilies = table.getColumnFamilies(); + for (ColumnFamily columnFamily : columnFamilies) { + printColumnFamily(columnFamily); + } + } catch (Exception ex) { + System.out.println("Error retrieving families: " + ex.getMessage()); + } + // [END bigtable_list_column_families] + + System.out.println("\nUpdating column family cf1 GC rule..."); + // [START bigtable_update_gc_rule] + // Update the column family metadata to update the GC rule + + // Update a column family GC rule + GCRules.VersionRule versionRule6 = GCRules.GCRULES.maxVersions(1); + try { + // Update column family with given GC rule + ModifyColumnFamiliesRequest updateRequest = + ModifyColumnFamiliesRequest.of(TABLE_ID).updateFamily("cf1", versionRule6); + adminClient.modifyFamilies(updateRequest); + } catch (Exception ex) { + System.out.println("Error updating GC rule for cf1: " + ex.getMessage()); + } + // [END bigtable_update_gc_rule] + + System.out.println("\nPrint updated column family cf1 GC rule..."); + // [START bigtable_family_get_gc_rule] + try { + Table table = adminClient.getTable(TABLE_ID); + Collection columnFamilies = table.getColumnFamilies(); + for (ColumnFamily columnFamily : columnFamilies) { + if (columnFamily.getId().equals("cf1")) { + printColumnFamily(columnFamily); + } + } + } catch (Exception ex) { + System.out.println("Error retrieving family cf1 metadata:" + ex.getMessage()); + } + // [END bigtable_family_get_gc_rule] + + System.out.println("\nDelete a column family cf2..."); + // [START bigtable_delete_family] + // Delete a column family + try { + ModifyColumnFamiliesRequest deletecf2 = + ModifyColumnFamiliesRequest.of(TABLE_ID).dropFamily("cf2"); + adminClient.modifyFamilies(deletecf2); + System.out.println("cf2 deleted successfully\n"); + } catch (Exception ex) { + System.out.println("Error deleting family cf2: " + ex.getMessage()); + } + + // [START bigtable_delete_table] + // Delete the entire table + System.out.println("Delete the table"); + try { + adminClient.deleteTable(TABLE_ID); + System.out.println("Table deleted: " + TABLE_ID); + } catch (Exception ex) { + System.out.println("Error deleting table " + TABLE_ID + ": " + ex.getMessage()); + } + // [END bigtable_delete_table] + } catch (IOException ex) { + System.out.println("Error creating Bigtable Table Admin: " + ex.getMessage()); + } + } + + private static void printColumnFamily(ColumnFamily columnFamily) { + System.out.println( + "Column family: " + columnFamily.getId() + "\nMetadata: " + columnFamily.getGCRule()); + } +} \ No newline at end of file From 7932079be8360b1bfe20ffb67b8bf14984fdc41f Mon Sep 17 00:00:00 2001 From: elisheva-qlogic Date: Wed, 5 Dec 2018 14:33:35 -0500 Subject: [PATCH 02/10] Samples and tests --- .../cloud/examples/bigtable/HelloWorld.java | 159 ++++-- .../examples/bigtable/InstanceAdmin.java | 196 +++---- .../cloud/examples/bigtable/TableAdmin.java | 518 +++++++++++------- .../examples/bigtable/HelloWorldTest.java | 100 ++++ .../examples/bigtable/InstanceAdminTest.java | 107 ++++ .../examples/bigtable/TableAdminTest.java | 136 +++++ 6 files changed, 876 insertions(+), 340 deletions(-) create mode 100644 google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/HelloWorldTest.java create mode 100644 google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/InstanceAdminTest.java create mode 100644 google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/TableAdminTest.java 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 fc11a21e325c..b38f543177a2 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 @@ -1,101 +1,172 @@ +/* + * Copyright 2018 Google LLC. All Rights Reserved. + * + * 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 + * + * https://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.google.cloud.examples.bigtable; import com.google.api.gax.rpc.ServerStream; import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient; import com.google.cloud.bigtable.admin.v2.BigtableTableAdminSettings; import com.google.cloud.bigtable.admin.v2.models.CreateTableRequest; +import com.google.cloud.bigtable.admin.v2.models.Table; import com.google.cloud.bigtable.data.v2.BigtableDataClient; import com.google.cloud.bigtable.data.v2.BigtableDataSettings; import com.google.cloud.bigtable.data.v2.models.InstanceName; - import com.google.cloud.bigtable.data.v2.models.Query; import com.google.cloud.bigtable.data.v2.models.Row; import com.google.cloud.bigtable.data.v2.models.RowCell; import com.google.cloud.bigtable.data.v2.models.RowMutation; -import java.sql.Timestamp; -// [START HelloWorld] public class HelloWorld { - public static void main(String[] args) throws Exception { + public static void main(String... args) throws Exception { - // the ID of the cloud bigtable project, instance and table - String TABLE_ID = "Hello-Bigtable"; - String COLUMN_FAMILY_ID = "cf1"; - String COLUMN_QUALIFIER = "greeting"; - String INSTANCE_ID = System.getenv("INSTANCE_ID"); - String GCLOUD_PROJECT_ID = System.getenv("GCLOUD_PROJECT_ID"); - final String rowKeyPrefix = "rowKey"; + final String GCLOUD_PROJECT_ID = args[0]; + final String INSTANCE_ID = args[1]; + final String TABLE_ID = "Hello-Bigtable"; + final String COLUMN_FAMILY_ID = "cf1"; + final String COLUMN_QUALIFIER = "greeting"; + final String ROW_KEY_PREFIX = "rowKey"; - if (INSTANCE_ID.length() == 0) { - System.out.println("Environment variables for INSTANCE_ID must be set!"); - } - if (GCLOUD_PROJECT_ID.length() == 0) { - throw new Error("Environment variables GCLOUD_PROJECT_ID must be set!"); + if (args.length < 2) { + System.out.println("Missing required project id or instance id"); + return; } + // [START connecting_to_bigtable] // create the settings to configure a bigtable data client BigtableDataSettings settings = BigtableDataSettings.newBuilder() .setInstanceName(InstanceName.of(GCLOUD_PROJECT_ID, INSTANCE_ID)).build(); - // create bigtable data client + // Create bigtable data client BigtableDataClient dataClient = BigtableDataClient.create(settings); + // Create the settings to configure a bigtable admin client BigtableTableAdminSettings adminSettings = BigtableTableAdminSettings.newBuilder() .setInstanceName( com.google.bigtable.admin.v2.InstanceName.of(GCLOUD_PROJECT_ID, INSTANCE_ID)).build(); + // Create bigtable admin client BigtableTableAdminClient adminClient = BigtableTableAdminClient.create(adminSettings); + // [END connecting_to_bigtable] + + try { + // Check if table exists, create table if does not exist + createTable(adminClient, TABLE_ID, COLUMN_FAMILY_ID); + + // Write to table + writeToTable(dataClient, TABLE_ID, ROW_KEY_PREFIX, COLUMN_FAMILY_ID, COLUMN_QUALIFIER); + // Read single row from the table + readSingleRow(dataClient, TABLE_ID, ROW_KEY_PREFIX); + + // Read entire table + readTable(dataClient, TABLE_ID); + + // Delete table + deleteTable(adminClient, TABLE_ID); + } catch (Exception e) { + System.out.println("Exception while running HelloWorld: " + e.getMessage()); + } finally { + dataClient.close(); + adminClient.close(); + } + } + + public static Table createTable(BigtableTableAdminClient adminClient, String TABLE_ID, + String COLUMN_FAMILY_ID) { + // [START creating_a_table] + Table table = null; if (!adminClient.exists(TABLE_ID)) { CreateTableRequest createTableRequest = CreateTableRequest.of(TABLE_ID).addFamily(COLUMN_FAMILY_ID); - System.out.println("Creating table " + TABLE_ID); - adminClient.createTable(createTableRequest); + System.out.println("Creating table: " + TABLE_ID); + table = adminClient.createTable(createTableRequest); } + return table; + // [END creating_a_table] + } + public static RowMutation writeToTable(BigtableDataClient dataClient, String TABLE_ID, + String ROW_KEY_PREFIX, String COLUMN_FAMILY_ID, String COLUMN_QUALIFIER) { + // [START writing_rows] + RowMutation mutation = null; try { - System.out.println("Write some greetings to the table"); - String[] greetings = { "Hello World!", "Hello Bigtable!", "Hello Node!" }; + System.out.println("Write some greetings to the table:"); + String[] greetings = { "Hello World!", "Hello Bigtable!", "Hello Java!" }; for (int i = 0; i < greetings.length; i++) { - RowMutation rowMutation = RowMutation.create(TABLE_ID, rowKeyPrefix + i); - long timestamp = new Timestamp(System.currentTimeMillis()).getTime(); - rowMutation.setCell(COLUMN_FAMILY_ID, COLUMN_QUALIFIER, timestamp, greetings[i]); + RowMutation rowMutation = RowMutation.create(TABLE_ID, ROW_KEY_PREFIX + i); + mutation = rowMutation.setCell(COLUMN_FAMILY_ID, COLUMN_QUALIFIER, greetings[i]); dataClient.mutateRow(rowMutation); + System.out.println(greetings[i]); } + } catch (Exception e) { + System.out.println("Exception while writing to table: " + e.getMessage()); + } + return mutation; + // [END writing_rows] + } - System.out.println("Reading a single row by row key"); - Row row = dataClient.readRow(TABLE_ID, rowKeyPrefix + 1); - System.out.println(row.getKey().toStringUtf8()); + public static Row readSingleRow(BigtableDataClient dataClient, String TABLE_ID, + String ROW_KEY_PREFIX) { + // [START reading_a_row] + Row row = null; + try { + System.out.println("Reading a single row by row key:"); + row = dataClient.readRow(TABLE_ID, ROW_KEY_PREFIX + 0); + System.out.println("Row: " + row.getKey().toStringUtf8()); for (RowCell cell : row.getCells()) { - System.out.println( - "Family: " + cell.getFamily() + " Qualifier: " + cell.getQualifier().toStringUtf8() - + " Timestamp: " + cell.getTimestamp() + " Value: " + cell.getValue() - .toStringUtf8()); + System.out.printf("Family: %s Qualifier: %s Value: %s", cell.getFamily(), + cell.getQualifier().toStringUtf8(), cell.getValue().toStringUtf8()); } + } catch (Exception e) { + System.out.println("Exception while reading a single row: " + e.getMessage()); + } + return row; + // [END reading_a_row] + } - System.out.println("Reading the entire table"); + public static ServerStream readTable(BigtableDataClient dataClient, String TABLE_ID) { + // [START scanning_all_rows] + ServerStream rowStream = null; + try { + System.out.println("Reading the entire table:"); Query query = Query.create(TABLE_ID); - ServerStream rowStream = dataClient.readRows(query); + rowStream = dataClient.readRows(query); for (Row r : rowStream) { System.out.println("Row Key: " + r.getKey()); for (RowCell cell : r.getCells()) { - System.out.println( - "Family: " + cell.getFamily() + " Qualifier: " + cell.getQualifier().toStringUtf8() - + " Timestamp: " + cell.getTimestamp() + " Value: " + cell.getValue() - .toStringUtf8()); + System.out.printf("Family: %s Qualifier: %s Value: %s", cell.getFamily(), + cell.getQualifier().toStringUtf8(), cell.getValue().toStringUtf8()); } } + } catch (Exception e) { + System.out.println("Exception while reading table: " + e.getMessage()); + } + return rowStream; + // [END scanning_all_rows] + } - System.out.println("Delete the table"); + public static void deleteTable(BigtableTableAdminClient adminClient, String TABLE_ID) { + // [START deleting_a_table] + System.out.println("Delete the table:"); + try { adminClient.deleteTable(TABLE_ID); - + System.out.printf("Table: %s deleted successfully", TABLE_ID); } catch (Exception e) { - System.out.println("Exception while running HelloWorld: " + e.getMessage()); - } finally { - dataClient.close(); - adminClient.close(); + System.out.println("Exception while deleting table: " + e.getMessage()); } + // [END deleting_a_table] } } -// [END HelloWorld] \ 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 714ca196056d..0bd48f4cfe38 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 @@ -1,5 +1,21 @@ +/* + * Copyright 2018 Google LLC. All Rights Reserved. + * + * 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 + * + * https://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.google.cloud.examples.bigtable; +import com.google.api.gax.rpc.NotFoundException; import com.google.bigtable.admin.v2.ProjectName; import com.google.cloud.bigtable.admin.v2.BigtableInstanceAdminClient; import com.google.cloud.bigtable.admin.v2.BigtableInstanceAdminSettings; @@ -15,79 +31,96 @@ public class InstanceAdmin { - public static void main(String[] args) throws IOException { + public static void main(String... args) { - String GCLOUD_PROJECT = System.getenv("GCLOUD_PROJECT"); + final String GCLOUD_PROJECT = args[0]; + final String PRODUCTION_INSTANCE = "ssd-instance"; + final String PRODUCTION_CLUSTER = "ssd-cluster"; - if (GCLOUD_PROJECT.length() == 0) { - throw new Error("Environment variables GCLOUD_PROJECT must be set!"); + if (args.length < 1) { + System.out.println("Missing required project id"); + return; } - BigtableInstanceAdminSettings instanceAdminSettings = - BigtableInstanceAdminSettings.newBuilder().setProjectName(ProjectName.of(GCLOUD_PROJECT)) - .build(); + try { + // Create the settings to configure a bigtable admin client + BigtableInstanceAdminSettings instanceAdminSettings = + BigtableInstanceAdminSettings.newBuilder().setProjectName(ProjectName.of(GCLOUD_PROJECT)) + .build(); + + // Create bigtable admin client + BigtableInstanceAdminClient adminClient = + BigtableInstanceAdminClient.create(instanceAdminSettings); + + // Create PRODUCTION instance + createProdInstance(adminClient, PRODUCTION_INSTANCE, PRODUCTION_CLUSTER); - BigtableInstanceAdminClient adminClient = - BigtableInstanceAdminClient.create(instanceAdminSettings); + // List instances + listInstances(adminClient); - System.out.println("Create an instance (type: PRODUCTION) and run basic instance-operations"); - runInstanceOperations(adminClient, "ssd-instance", "ssd-cluster"); + // Get PRODUCTION instance + getInstance(adminClient, PRODUCTION_INSTANCE); - System.out.println("Create DEVELOPMENT instance"); - createDevInstance(adminClient, "hdd-instance", "hdd-cluster"); + // Get PRODUCTION clusters + listClusters(adminClient, PRODUCTION_INSTANCE); - System.out.println("Delete the Instance"); - deleteInstance(adminClient, "hdd-instance"); + // Add cluster to PRODUCTION instance + addCluster(adminClient, PRODUCTION_INSTANCE, PRODUCTION_CLUSTER); - System.out.println("Add Cluster"); - addCluster(adminClient, "ssd-instance", "ssd-cluster"); + // Delete cluster from PRODUCTION instance + deleteCluster(adminClient, PRODUCTION_INSTANCE, PRODUCTION_CLUSTER); - System.out.println("Delete the Cluster"); - deleteCluster(adminClient, "ssd-instance", "ssd-cluster"); + // End operations with deleting PRODUCTION instance + deleteInstance(adminClient, PRODUCTION_INSTANCE); - // end operations with deleting the pro-instance created in `runInstanceOperations` - deleteInstance(adminClient, "ssd-instance"); + } catch (IOException ex) { + System.out.println("Exception while running InstanceAdmin: " + ex.getMessage()); + } } - public static void runInstanceOperations(BigtableInstanceAdminClient adminClient, + public static Instance createProdInstance(BigtableInstanceAdminClient adminClient, String instanceID, String clusterID) { - System.out.println("Check Instance Exists"); + System.out.println("Check if instance exists:"); // [START bigtable_check_instance_exists] boolean found = false; try { found = adminClient.exists(instanceID); } catch (Exception e) { - System.out.println("Error checking if Instance exists: " + e.getMessage()); + System.out.println("Error checking if instance exists: " + e.getMessage()); } + Instance instance = null; // Create instance if does not exists if (!found) { - System.out.println("Creating a PRODUCTION Instance"); + 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 CreateInstanceRequest createInstanceRequest = CreateInstanceRequest.of(instanceID) .addCluster(clusterID, "us-central1-f", 3, StorageType.SSD).setType(Type.PRODUCTION) - .addLabel("prod-label", "prod-label"); + .addLabel("example", "instance_admin"); // Create production instance with given request try { - Instance instance = adminClient.createInstance(createInstanceRequest); - System.out - .println("PRODUCTION type instance : " + instance.getId() + " created successfully"); - + instance = adminClient.createInstance(createInstanceRequest); + System.out.printf("PRODUCTION type instance: %s, created successfully", instance.getId()); } catch (Exception e) { - System.out.println("Error creating prod-instance: " + e.getMessage()); + System.out.println("Error creating PRODUCTION instance: " + e.getMessage()); + System.exit(0); } // [END bigtable_create_prod_instance] } else { - System.out.println("Instance " + instanceID + " exists"); + System.out.printf("Instance: %s exists", instanceID); + instance = adminClient.getInstance(instanceID); } + return instance; + } - System.out.println(); //for a new-line - System.out.println("Listing Instances:"); + public static List listInstances(BigtableInstanceAdminClient adminClient) { + System.out.println("\nListing Instances:"); // [START bigtable_list_instances] + List instances = null; try { - List instances = adminClient.listInstances(); + instances = adminClient.listInstances(); for (Instance instance : instances) { System.out.println(instance.getId()); } @@ -95,19 +128,22 @@ public static void runInstanceOperations(BigtableInstanceAdminClient adminClient System.out.println("Error listing instances: " + e.getMessage()); } // [END bigtable_list_instances] + return instances; + } - System.out.println(); //for a new-line - System.out.println("Get Instance:"); + public static Instance getInstance(BigtableInstanceAdminClient adminClient, String instanceID) { + System.out.println("\nGet Instance:"); // [START bigtable_get_instance] + Instance instance = null; try { - Instance instance = adminClient.getInstance(instanceID); + instance = adminClient.getInstance(instanceID); System.out.println("Instance ID: " + instance.getId()); System.out.println("Instance Meta:"); System.out.println("Display Name: " + instance.getDisplayName()); System.out.println("Labels:"); Map labels = instance.getLabels(); for (String key : labels.keySet()) { - System.out.println(key + ": " + labels.get(key)); + System.out.printf("%s: %s", key, labels.get(key)); } System.out.println("State: " + instance.getState()); System.out.println("Type: " + instance.getType()); @@ -115,12 +151,16 @@ public static void runInstanceOperations(BigtableInstanceAdminClient adminClient System.out.println("Error getting instance: " + e.getMessage()); } // [END bigtable_get_instance] + return instance; + } - System.out.println(); //for a new-line - System.out.println("Listing Clusters..."); + public static List listClusters(BigtableInstanceAdminClient adminClient, + String instanceID) { + System.out.println("\nListing Clusters:"); // [START bigtable_get_clusters] + List clusters = null; try { - List clusters = adminClient.listClusters(instanceID); + clusters = adminClient.listClusters(instanceID); for (Cluster cluster : clusters) { System.out.println(cluster.getId()); } @@ -128,86 +168,50 @@ public static void runInstanceOperations(BigtableInstanceAdminClient adminClient System.out.println("Error listing clusters: " + e.getMessage()); } // [END bigtable_get_clusters] + return clusters; } - public static void createDevInstance(BigtableInstanceAdminClient adminClient, String instanceID, - String clusterID) { - // [START bigtable_create_dev_instance] - System.out.println(); //for a new-line - System.out.println("Creating a DEVELOPMENT Instance"); - // Creates a Development instance with the ID "hdd-instance" - // with cluster ID "hdd-cluster" and location us-central1-f - // Cluster nodes should not be set while creating Development Instance - CreateInstanceRequest createInstanceRequest = CreateInstanceRequest.of(instanceID) - .addCluster(clusterID, "us-central1-f", 1, StorageType.HDD).setType(Type.DEVELOPMENT) - .addLabel("dev-label", "dev-label"); - // Create development instance with given request - try { - Instance instance = adminClient.createInstance(createInstanceRequest); - System.out - .println("DEVELOPMENT type instance : " + instance.getId() + " created successfully"); - } catch (Exception e) { - System.out.println("Error creating dev-instance: " + e.getMessage()); - } - // [END bigtable_create_dev_instance] - } - - // Delete the Instance public static void deleteInstance(BigtableInstanceAdminClient adminClient, String instanceID) { + System.out.println("\nDeleting Instance:"); // [START bigtable_delete_instance] - System.out.println(); //for a new-line - System.out.println("Deleting Instance"); try { adminClient.deleteInstance(instanceID); System.out.println("Instance deleted: " + instanceID); - } catch (Exception e) { + } catch (NotFoundException e) { System.out.println("Error deleting instance: " + instanceID); + System.out.println(e.getMessage()); } // [END bigtable_delete_instance] } - // Add Cluster - public static void addCluster(BigtableInstanceAdminClient adminClient, String instanceID, + public static Cluster addCluster(BigtableInstanceAdminClient adminClient, String instanceID, String clusterID) { - boolean found = false; + Cluster cluster = null; + System.out.println("\nAdding cluster to instance: " + instanceID); + // [START bigtable_create_cluster] try { - found = adminClient.exists(instanceID); + cluster = adminClient.createCluster( + CreateClusterRequest.of(instanceID, clusterID).setZone("us-central1-c").setServeNodes(3) + .setStorageType(StorageType.SSD)); + System.out.printf("Cluster: %s created successfully", cluster.getId()); } catch (Exception e) { - System.out.println("Error checking if Instance exists: " + e.getMessage()); - } - if (!found) { - System.out.println("Instance does not exist"); - } else { - System.out.println(); //for a new-line - System.out.println("Adding Cluster to Instance " + instanceID); - // [START bigtable_create_cluster] - CreateClusterRequest createClusterRequest = - CreateClusterRequest.of(instanceID, clusterID).setServeNodes(3) - .setStorageType(StorageType.SSD).setZone("us-central1-c"); - try { - Cluster cluster = adminClient.createCluster(createClusterRequest); - System.out.println("Cluster : " + cluster.getId() + " created successfully"); - } catch (Exception e) { - System.out.println("Error creating cluster: " + e.getMessage()); - } - // [END bigtable_create_cluster] + System.out.println("Error creating cluster: " + e.getMessage()); } + // [END bigtable_create_cluster] + return cluster; } - // Delete the Cluster public static void deleteCluster(BigtableInstanceAdminClient adminClient, String instanceID, String clusterID) { // [START bigtable_delete_cluster] - System.out.println(); //for a new-line - System.out.println("Deleting Cluster"); + System.out.println("\nDeleting Cluster"); // [START bigtable_delete_cluster] try { adminClient.deleteCluster(instanceID, clusterID); - System.out.println("Cluster : " + clusterID + " deleted successfully"); + System.out.printf("Cluster: %s deleted successfully", clusterID); } catch (Exception e) { System.out.println("Error deleting cluster: " + e.getMessage()); } - System.out.println("Cluster deleted: " + clusterID); // [END bigtable_delete_cluster] } -} \ No newline at end of file +} diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/bigtable/TableAdmin.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/bigtable/TableAdmin.java index 4d4b1e745af5..56786533022c 100644 --- a/google-cloud-examples/src/main/java/com/google/cloud/examples/bigtable/TableAdmin.java +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/bigtable/TableAdmin.java @@ -1,11 +1,24 @@ +/* + * Copyright 2018 Google LLC. All Rights Reserved. + * + * 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 + * + * https://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.google.cloud.examples.bigtable; -import com.google.bigtable.admin.v2.TableName; import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient; import com.google.cloud.bigtable.admin.v2.BigtableTableAdminSettings; import com.google.cloud.bigtable.admin.v2.models.ColumnFamily; import com.google.cloud.bigtable.admin.v2.models.CreateTableRequest; - import com.google.cloud.bigtable.admin.v2.models.GCRules; import com.google.cloud.bigtable.admin.v2.models.ModifyColumnFamiliesRequest; import com.google.cloud.bigtable.admin.v2.models.Table; @@ -16,245 +29,350 @@ public class TableAdmin { - public static void main(String[] args) { + public static void main(String... args) { - String GCLOUD_PROJECT = System.getenv("GCLOUD_PROJECT"); - String INSTANCE_ID = System.getenv("INSTANCE_ID"); - String TABLE_ID = "TABLE_ID"; + final String GCLOUD_PROJECT = args[0]; + final String INSTANCE_ID = args[1]; + final String TABLE_ID = "table"; - if (GCLOUD_PROJECT.length() == 0 || INSTANCE_ID.length() == 0) { - throw new Error("Environment variables must be set!"); + if (args.length < 2) { + System.out.println("Missing required project id or instance id"); + return; } try { + // Create the settings to configure a bigtable admin client BigtableTableAdminSettings adminSettings = BigtableTableAdminSettings.newBuilder() .setInstanceName( com.google.bigtable.admin.v2.InstanceName.of(GCLOUD_PROJECT, INSTANCE_ID)).build(); + // Create bigtable admin client BigtableTableAdminClient adminClient = BigtableTableAdminClient.create(adminSettings); - // Check if table exists - if (!adminClient.exists(TABLE_ID)) { - // Create table if does not exist - System.out.println("Table does not exist. Creating table " + TABLE_ID); - // Creating table - try { - CreateTableRequest createTableRequest = CreateTableRequest.of(TABLE_ID); - Table table = adminClient.createTable(createTableRequest); - System.out.println("Table : " + table.getId() + "created successfully"); - } catch (Exception ex) { - System.out.println("Error creating table: " + ex.getMessage()); - } - } else { - System.out.println("Table exists."); - } + // Check if table exists, create table if does not exist + createTable(adminClient, TABLE_ID); - System.out.println("\nListing tables in current project..."); - // [START bigtable_list_tables] - // List tables in current instance - try { - List listTables = adminClient.listTables(); - for (TableName tableName : listTables) { - System.out.println(tableName.getTable()); - } - } catch (Exception ex) { - System.out.println("Error listing tables in current project: " + ex.getMessage()); - } - // [END bigtable_list_tables] + // List all tables in current instance + listAllTables(adminClient); - System.out.println("\nPrinting table metadata..."); - // [START bigtable_get_table_metadata] - // Get table metadata, and apply a view to the table fields - try { - Table table = adminClient.getTable(TABLE_ID); - Collection columnFamilies = table.getColumnFamilies(); - for (ColumnFamily columnFamily : columnFamilies) { - printColumnFamily(columnFamily); - } - } catch (Exception ex) { - System.out.println("Error retrieving table metadata: " + ex.getMessage()); - } - // [END bigtable_get_table_metadata] + // Print table metadata + getTableMeta(adminClient, TABLE_ID); - System.out.println("\nCreating column family cf1 with max age GC rule..."); - // [START bigtable_create_family_gc_max_age] - // Create a column family with GC policy : maximum age - // where age = current time minus cell timestamp + // Create column family with max age GC rule + maxAgeRule(adminClient, TABLE_ID, "cf1"); - // Define the GC rule to retain data with max age of 5 days - GCRules.DurationRule maxAgeRule1 = GCRules.GCRULES.maxAge(5, TimeUnit.DAYS); + // Create column family with max versions GC rule + maxVersionsRule(adminClient, TABLE_ID, "cf2"); - // Create column family with given GC rule - try { - ModifyColumnFamiliesRequest columnFamiliesRequest1 = - ModifyColumnFamiliesRequest.of(TABLE_ID).addFamily("cf1", maxAgeRule1); - adminClient.modifyFamilies(columnFamiliesRequest1); - System.out.println("Created column family cf1"); - } catch (Exception ex) { - System.out.println("Error creating column family: " + ex.getMessage()); - } - // [END bigtable_create_family_gc_max_age] + // Create column family with union GC rule + unionRule(adminClient, TABLE_ID, "cf3"); - System.out.println("\nCreating column family cf2 with max versions GC rule..."); - // [START bigtable_create_family_gc_max_versions] - // Create a column family with GC policy : most recent N versions - // where 1 = most recent version + // Create column family with intersection GC rule + intersectionRule(adminClient, TABLE_ID, "cf4"); - // Define the GC policy to retain only the most recent 2 versions - GCRules.VersionRule versionRule1 = GCRules.GCRULES.maxVersions(2); + // Create column family with nested GC rule + nestedRule(adminClient, TABLE_ID, "cf5"); - // Create column family with given GC rule - try { - ModifyColumnFamiliesRequest columnFamiliesRequest2 = - ModifyColumnFamiliesRequest.of(TABLE_ID).addFamily("cf2", versionRule1); - adminClient.modifyFamilies(columnFamiliesRequest2); - System.out.println("Created column family cf2"); - } catch (Exception ex) { - System.out.println("Error creating column family: " + ex.getMessage()); - } - // [END bigtable_create_family_gc_max_versions] + // List column families for given table + listColumnFamilies(adminClient, TABLE_ID); - System.out.println("\nCreating column family cf3 with union GC rule..."); - // [START bigtable_create_family_gc_union] - // Create a column family with GC policy to drop data that matches at least one condition. + // Modify column family cf1's GC rule + modifyColumnFamilyRule(adminClient, TABLE_ID, "cf1"); - // Define a GC rule to drop cells older than 5 days OR not the most recent version - GCRules.DurationRule maxAgeRule2 = GCRules.GCRULES.maxAge(5, TimeUnit.DAYS); - GCRules.VersionRule versionRule2 = GCRules.GCRULES.maxVersions(1); - // Add rules to union rule list - GCRules.UnionRule unionRule1 = GCRules.GCRULES.union().rule(maxAgeRule2).rule(versionRule2); + // Print updated column family cf1's rule + printModifiedColumnFamily(adminClient, TABLE_ID, "cf1"); - // Create column family with given GC rule - try { - ModifyColumnFamiliesRequest columnFamiliesRequest3 = - ModifyColumnFamiliesRequest.of(TABLE_ID).addFamily("cf3", unionRule1); - adminClient.modifyFamilies(columnFamiliesRequest3); - System.out.println("Created column family cf3"); - } catch (Exception ex) { - System.out.println("Error creating column family: " + ex.getMessage()); - } - // [END bigtable_create_family_gc_union] + // Delete column family cf2 + deleteColumnFamily(adminClient, TABLE_ID, "cf2"); - System.out.println("\nCreating column family cf4 with intersection GC rule..."); - // [START bigtable_create_family_gc_intersection] - // Create a column family with GC policy to drop data that matches all conditions + // Delete table + deleteTable(adminClient, TABLE_ID); - // GC rule: Drop cells older than 5 days AND older than the most recent 2 versions - GCRules.DurationRule maxAgeRule3 = GCRules.GCRULES.maxAge(5, TimeUnit.DAYS); - GCRules.VersionRule versionRule3 = GCRules.GCRULES.maxVersions(2); - GCRules.IntersectionRule intersectionRule1 = - GCRules.GCRULES.intersection().rule(maxAgeRule3).rule(versionRule3); + } catch (IOException ex) { + System.out.println("Exception while running BigtableTableAdmin: " + ex.getMessage()); + } + } - // Create column family with given GC rule + public static Table createTable(BigtableTableAdminClient adminClient, String TABLE_ID) { + // [START bigtable_create_table] + // Check if table exists + Table table = null; + if (!adminClient.exists(TABLE_ID)) { + // Create table if does not exist + System.out.println("Table does not exist. Creating table: " + TABLE_ID); + // Creating table try { - ModifyColumnFamiliesRequest columnFamiliesRequest4 = - ModifyColumnFamiliesRequest.of(TABLE_ID).addFamily("cf4", intersectionRule1); - adminClient.modifyFamilies(columnFamiliesRequest4); - System.out.println("Created column family cf4"); + CreateTableRequest createTableRequest = CreateTableRequest.of(TABLE_ID).addFamily("cf"); + table = adminClient.createTable(createTableRequest); + System.out.printf("Table: %s created successfully\n", table.getId()); } catch (Exception ex) { - System.out.println("Error creating column family: " + ex.getMessage()); + System.out.println("Error creating table: " + ex.getMessage()); } - // [END bigtable_create_family_gc_intersection] - - System.out.println("\nCreating column family cf5 with a nested GC rule"); - // [START bigtable_create_family_gc_nested] - // Create a nested GC rule: - // Drop cells that are either older than the 10 recent versions - // OR - // Drop cells that are older than a month AND older than the 2 recent versions - GCRules.VersionRule versionRule4 = GCRules.GCRULES.maxVersions(10); - GCRules.DurationRule maxAgeRule4 = GCRules.GCRULES.maxAge(30, TimeUnit.DAYS); - GCRules.VersionRule versionRule5 = GCRules.GCRULES.maxVersions(2); - GCRules.IntersectionRule intersectionRule2 = - GCRules.GCRULES.intersection().rule(maxAgeRule4).rule(versionRule5); - GCRules.UnionRule unionRule2 = - GCRules.GCRULES.union().rule(intersectionRule2).rule(versionRule4); - - // Create column family with given GC rule - try { - ModifyColumnFamiliesRequest columnFamiliesRequest5 = - ModifyColumnFamiliesRequest.of(TABLE_ID).addFamily("cf5", unionRule2); - adminClient.modifyFamilies(columnFamiliesRequest5); - System.out.println("Created column family cf5"); - } catch (Exception ex) { - System.out.println("Error creating column family: " + ex.getMessage()); + } else { + System.out.println("Table exists"); + table = adminClient.getTable(TABLE_ID); + } + // [END bigtable_create_table] + return table; + } + + public static List listAllTables(BigtableTableAdminClient adminClient) { + System.out.println("\nListing tables in current instance:"); + // [START bigtable_list_tables] + // List tables in current instance + List listTables = null; + try { + listTables = adminClient.listTables(); + for (String tableName : listTables) { + System.out.println(tableName); } - // [END bigtable_create_family_gc_nested] + } catch (Exception ex) { + System.out.println("Error listing tables in current instance: " + ex.getMessage()); + } + // [END bigtable_list_tables] + return listTables; + } - System.out.println("\nPrinting ID and GC Rule for all column families..."); - // [START bigtable_list_column_families] - // List all families in the table with GC rules - try { - Table table = adminClient.getTable(TABLE_ID); - Collection columnFamilies = table.getColumnFamilies(); - for (ColumnFamily columnFamily : columnFamilies) { - printColumnFamily(columnFamily); - } - } catch (Exception ex) { - System.out.println("Error retrieving families: " + ex.getMessage()); + public static Table getTableMeta(BigtableTableAdminClient adminClient, String TABLE_ID) { + System.out.println("\nPrinting table metadata:"); + // [START bigtable_get_table_metadata] + // Get table metadata, and apply a view to the table fields + Table table = null; + try { + table = adminClient.getTable(TABLE_ID); + System.out.println("Table: " + table.getId()); + Collection columnFamilies = table.getColumnFamilies(); + for (ColumnFamily columnFamily : columnFamilies) { + printColumnFamily(columnFamily); } - // [END bigtable_list_column_families] + } catch (Exception ex) { + System.out.println("Error retrieving table metadata: " + ex.getMessage()); + } + // [END bigtable_get_table_metadata] + return table; + } - System.out.println("\nUpdating column family cf1 GC rule..."); - // [START bigtable_update_gc_rule] - // Update the column family metadata to update the GC rule + public static Table maxAgeRule(BigtableTableAdminClient adminClient, String TABLE_ID, String cf) { + System.out.printf("\nCreating column family %s with max age GC rule:\n", cf); + // [START bigtable_create_family_gc_max_age] + // Create a column family with GC policy : maximum age + // where age = current time minus cell timestamp - // Update a column family GC rule - GCRules.VersionRule versionRule6 = GCRules.GCRULES.maxVersions(1); - try { - // Update column family with given GC rule - ModifyColumnFamiliesRequest updateRequest = - ModifyColumnFamiliesRequest.of(TABLE_ID).updateFamily("cf1", versionRule6); - adminClient.modifyFamilies(updateRequest); - } catch (Exception ex) { - System.out.println("Error updating GC rule for cf1: " + ex.getMessage()); + // Define the GC rule to retain data with max age of 5 days + GCRules.DurationRule maxAgeRule1 = GCRules.GCRULES.maxAge(5, TimeUnit.DAYS); + + // Create column family with given GC rule + Table table = null; + try { + ModifyColumnFamiliesRequest columnFamiliesRequest1 = + ModifyColumnFamiliesRequest.of(TABLE_ID).addFamily(cf, maxAgeRule1); + table = adminClient.modifyFamilies(columnFamiliesRequest1); + System.out.println("Created column family: " + cf); + } catch (Exception ex) { + System.out.println("Error creating column family: " + ex.getMessage()); + } + // [END bigtable_create_family_gc_max_age] + return table; + } + + public static Table maxVersionsRule(BigtableTableAdminClient adminClient, String TABLE_ID, + String cf) { + System.out.printf("\nCreating column family %s with max versions GC rule:\n", cf); + // [START bigtable_create_family_gc_max_versions] + // Create a column family with GC policy : most recent N versions + // where 1 = most recent version + + // Define the GC policy to retain only the most recent 2 versions + GCRules.VersionRule versionRule1 = GCRules.GCRULES.maxVersions(2); + + // Create column family with given GC rule + Table table = null; + try { + ModifyColumnFamiliesRequest columnFamiliesRequest2 = + ModifyColumnFamiliesRequest.of(TABLE_ID).addFamily(cf, versionRule1); + table = adminClient.modifyFamilies(columnFamiliesRequest2); + System.out.println("Created column family: " + cf); + } catch (Exception ex) { + System.out.println("Error creating column family: " + ex.getMessage()); + } + // [END bigtable_create_family_gc_max_versions] + return table; + } + + public static Table unionRule(BigtableTableAdminClient adminClient, String TABLE_ID, String cf) { + System.out.printf("\nCreating column family %s with union GC rule:\n", cf); + // [START bigtable_create_family_gc_union] + // Create a column family with GC policy to drop data that matches at least one condition. + + // Define a GC rule to drop cells older than 5 days OR not the most recent version + GCRules.DurationRule maxAgeRule2 = GCRules.GCRULES.maxAge(5, TimeUnit.DAYS); + GCRules.VersionRule versionRule2 = GCRules.GCRULES.maxVersions(1); + // Add rules to union rule list + GCRules.UnionRule unionRule1 = GCRules.GCRULES.union().rule(maxAgeRule2).rule(versionRule2); + + // Create column family with given GC rule + Table table = null; + try { + ModifyColumnFamiliesRequest columnFamiliesRequest3 = + ModifyColumnFamiliesRequest.of(TABLE_ID).addFamily(cf, unionRule1); + table = adminClient.modifyFamilies(columnFamiliesRequest3); + System.out.println("Created column family: " + cf); + } catch (Exception ex) { + System.out.println("Error creating column family: " + ex.getMessage()); + } + // [END bigtable_create_family_gc_union] + return table; + } + + public static Table intersectionRule(BigtableTableAdminClient adminClient, String TABLE_ID, + String cf) { + System.out.printf("\nCreating column family %s with intersection GC rule:\n", cf); + // [START bigtable_create_family_gc_intersection] + // Create a column family with GC policy to drop data that matches all conditions + + // GC rule: Drop cells older than 5 days AND older than the most recent 2 versions + GCRules.DurationRule maxAgeRule3 = GCRules.GCRULES.maxAge(5, TimeUnit.DAYS); + GCRules.VersionRule versionRule3 = GCRules.GCRULES.maxVersions(2); + GCRules.IntersectionRule intersectionRule1 = + GCRules.GCRULES.intersection().rule(maxAgeRule3).rule(versionRule3); + + // Create column family with given GC rule + Table table = null; + try { + ModifyColumnFamiliesRequest columnFamiliesRequest4 = + ModifyColumnFamiliesRequest.of(TABLE_ID).addFamily(cf, intersectionRule1); + table = adminClient.modifyFamilies(columnFamiliesRequest4); + System.out.println("Created column family: " + cf); + } catch (Exception ex) { + System.out.println("Error creating column family: " + ex.getMessage()); + } + // [END bigtable_create_family_gc_intersection] + return table; + } + + public static Table nestedRule(BigtableTableAdminClient adminClient, String TABLE_ID, String cf) { + System.out.printf("\nCreating column family %s with a nested GC rule:\n", cf); + // [START bigtable_create_family_gc_nested] + // Create a nested GC rule: + // Drop cells that are either older than the 10 recent versions + // OR + // Drop cells that are older than a month AND older than the 2 recent versions + GCRules.VersionRule versionRule4 = GCRules.GCRULES.maxVersions(10); + GCRules.DurationRule maxAgeRule4 = GCRules.GCRULES.maxAge(30, TimeUnit.DAYS); + GCRules.VersionRule versionRule5 = GCRules.GCRULES.maxVersions(2); + GCRules.IntersectionRule intersectionRule2 = + GCRules.GCRULES.intersection().rule(maxAgeRule4).rule(versionRule5); + GCRules.UnionRule unionRule2 = + GCRules.GCRULES.union().rule(intersectionRule2).rule(versionRule4); + + // Create column family with given GC rule + Table table = null; + try { + ModifyColumnFamiliesRequest columnFamiliesRequest5 = + ModifyColumnFamiliesRequest.of(TABLE_ID).addFamily(cf, unionRule2); + table = adminClient.modifyFamilies(columnFamiliesRequest5); + System.out.println("Created column family: " + cf); + } catch (Exception ex) { + System.out.println("Error creating column family: " + ex.getMessage()); + } + // [END bigtable_create_family_gc_nested] + return table; + } + + public static Collection listColumnFamilies(BigtableTableAdminClient adminClient, + String TABLE_ID) { + System.out.println("\nPrinting ID and GC Rule for all column families:"); + // [START bigtable_list_column_families] + // List all families in the table with GC rules + Collection columnFamilies = null; + try { + Table table = adminClient.getTable(TABLE_ID); + columnFamilies = table.getColumnFamilies(); + for (ColumnFamily columnFamily : columnFamilies) { + printColumnFamily(columnFamily); } - // [END bigtable_update_gc_rule] + } catch (Exception ex) { + System.out.println("Error retrieving families: " + ex.getMessage()); + } + // [END bigtable_list_column_families] + return columnFamilies; + } - System.out.println("\nPrint updated column family cf1 GC rule..."); - // [START bigtable_family_get_gc_rule] - try { - Table table = adminClient.getTable(TABLE_ID); - Collection columnFamilies = table.getColumnFamilies(); - for (ColumnFamily columnFamily : columnFamilies) { - if (columnFamily.getId().equals("cf1")) { - printColumnFamily(columnFamily); - } + public static Table modifyColumnFamilyRule(BigtableTableAdminClient adminClient, String TABLE_ID, + String cf) { + System.out.printf("\nUpdating column family %s GC rule:\n", cf); + // [START bigtable_update_gc_rule] + // Update the column family metadata to update the GC rule + // Update a column family GC rule + GCRules.VersionRule versionRule6 = GCRules.GCRULES.maxVersions(1); + Table table = null; + try { + // Update column family with given GC rule + ModifyColumnFamiliesRequest updateRequest = + ModifyColumnFamiliesRequest.of(TABLE_ID).updateFamily(cf, versionRule6); + table = adminClient.modifyFamilies(updateRequest); + System.out.printf("Column family %s GC rule updated\n", cf); + } catch (Exception ex) { + System.out.printf("Error updating GC rule for %s: %s\n", cf, ex.getMessage()); + } + // [END bigtable_update_gc_rule] + return table; + } + + public static ColumnFamily printModifiedColumnFamily(BigtableTableAdminClient adminClient, + String TABLE_ID, String cf) { + System.out.printf("\nPrint updated GC rule for column family: %s\n", cf); + // [START bigtable_family_get_gc_rule] + ColumnFamily colFamily = null; + try { + Table table = adminClient.getTable(TABLE_ID); + Collection columnFamilies = table.getColumnFamilies(); + for (ColumnFamily columnFamily : columnFamilies) { + if (columnFamily.getId().equals(cf)) { + printColumnFamily(columnFamily); + colFamily = columnFamily; } - } catch (Exception ex) { - System.out.println("Error retrieving family cf1 metadata:" + ex.getMessage()); } - // [END bigtable_family_get_gc_rule] + } catch (Exception ex) { + System.out + .printf("Error retrieving metadata for column family: %s\n%s\n", cf, ex.getMessage()); + } + // [END bigtable_family_get_gc_rule] + return colFamily; + } - System.out.println("\nDelete a column family cf2..."); - // [START bigtable_delete_family] - // Delete a column family - try { - ModifyColumnFamiliesRequest deletecf2 = - ModifyColumnFamiliesRequest.of(TABLE_ID).dropFamily("cf2"); - adminClient.modifyFamilies(deletecf2); - System.out.println("cf2 deleted successfully\n"); - } catch (Exception ex) { - System.out.println("Error deleting family cf2: " + ex.getMessage()); - } + public static Table deleteColumnFamily(BigtableTableAdminClient adminClient, String TABLE_ID, + String cf) { + System.out.println("\nDelete column family: " + cf); + // [START bigtable_delete_family] + // Delete a column family + Table table = null; + try { + ModifyColumnFamiliesRequest deleted = ModifyColumnFamiliesRequest.of(TABLE_ID).dropFamily(cf); + table = adminClient.modifyFamilies(deleted); + System.out.printf("Column family %s deleted successfully\n", cf); + } catch (Exception ex) { + System.out.printf("Error deleting family: %s\n%s\n", cf, ex.getMessage()); + } + // [END bigtable_delete_family] + return table; + } - // [START bigtable_delete_table] - // Delete the entire table - System.out.println("Delete the table"); - try { - adminClient.deleteTable(TABLE_ID); - System.out.println("Table deleted: " + TABLE_ID); - } catch (Exception ex) { - System.out.println("Error deleting table " + TABLE_ID + ": " + ex.getMessage()); - } - // [END bigtable_delete_table] - } catch (IOException ex) { - System.out.println("Error creating Bigtable Table Admin: " + ex.getMessage()); + public static void deleteTable(BigtableTableAdminClient adminClient, String TABLE_ID) { + // [START bigtable_delete_table] + // Delete the entire table + System.out.println("Delete table: " + TABLE_ID); + try { + adminClient.deleteTable(TABLE_ID); + System.out.println("Table deleted: " + TABLE_ID); + } catch (Exception ex) { + System.out.printf("Error deleting table %s: %s\n", TABLE_ID, ex.getMessage()); } + // [END bigtable_delete_table] } private static void printColumnFamily(ColumnFamily columnFamily) { - System.out.println( - "Column family: " + columnFamily.getId() + "\nMetadata: " + columnFamily.getGCRule()); + System.out.printf("Column family: %s\nMetadata: %s\n", columnFamily.getId(), + columnFamily.getGCRule().toString()); } -} \ No newline at end of file +} 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 new file mode 100644 index 000000000000..b769a671c9be --- /dev/null +++ b/google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/HelloWorldTest.java @@ -0,0 +1,100 @@ +/* + * Copyright 2018 Google LLC. All Rights Reserved. + * + * 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 + * + * https://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.google.cloud.examples.bigtable; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import com.google.api.gax.rpc.ServerStream; +import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient; +import com.google.cloud.bigtable.admin.v2.BigtableTableAdminSettings; +import com.google.cloud.bigtable.admin.v2.models.CreateTableRequest; +import com.google.cloud.bigtable.admin.v2.models.Table; +import com.google.cloud.bigtable.data.v2.BigtableDataClient; +import com.google.cloud.bigtable.data.v2.BigtableDataSettings; +import com.google.cloud.bigtable.data.v2.models.InstanceName; +import com.google.cloud.bigtable.data.v2.models.Row; +import com.google.cloud.bigtable.data.v2.models.RowMutation; +import java.io.IOException; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +public class HelloWorldTest { + + private static final String PROJECT_ID = "test-project"; // Replace with your project ID + private static final String INSTANCE_ID = "test-instance"; // Replace with your instance ID + private static final String TABLE_ID = "test-table" + System.currentTimeMillis(); + private static final String COLUMN_FAMILY_ID = "test-cf"; + private static final String COLUMN_QUALIFIER = "test-greeting"; + private static final String ROW_KEY_PREFIX = "test-rowKey"; + private static BigtableDataSettings settings; + private static BigtableDataClient dataClient; + private static BigtableTableAdminSettings adminSettings; + private static BigtableTableAdminClient adminClient; + + @BeforeClass + public static void beforeClass() throws IOException { + settings = + BigtableDataSettings.newBuilder().setInstanceName(InstanceName.of(PROJECT_ID, INSTANCE_ID)) + .build(); + dataClient = BigtableDataClient.create(settings); + adminSettings = BigtableTableAdminSettings.newBuilder() + .setInstanceName(com.google.bigtable.admin.v2.InstanceName.of(PROJECT_ID, INSTANCE_ID)) + .build(); + adminClient = BigtableTableAdminClient.create(adminSettings); + if (!adminClient.exists(TABLE_ID)) { + adminClient.createTable(CreateTableRequest.of(TABLE_ID).addFamily(COLUMN_FAMILY_ID)); + } + } + + @AfterClass + public static void afterClass() throws Exception { + adminClient.deleteTable(TABLE_ID); + dataClient.close(); + adminClient.close(); + } + + @Test + public void testCreateAndDeleteTable() { + // Create table + Table table = TableAdmin.createTable(adminClient, "fake-table"); + assertNotNull(table); + + // Delete table + TableAdmin.deleteTable(adminClient, "fake-table"); + assertTrue(!adminClient.exists("fake-table")); + } + + @Test + public void testWriteToAndReadFromTable() { + // Write to table + RowMutation rowMutation = HelloWorld + .writeToTable(dataClient, TABLE_ID, ROW_KEY_PREFIX, COLUMN_FAMILY_ID, COLUMN_QUALIFIER); + assertNotNull(rowMutation); + + // Read a single row + Row row = HelloWorld.readSingleRow(dataClient, TABLE_ID, ROW_KEY_PREFIX); + assertNotNull(row); + } + + @Test + public void testReadTable() { + // Read whole table + ServerStream stream = HelloWorld.readTable(dataClient, TABLE_ID); + assertNotNull(stream); + } +} diff --git a/google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/InstanceAdminTest.java b/google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/InstanceAdminTest.java new file mode 100644 index 000000000000..85b4ba922349 --- /dev/null +++ b/google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/InstanceAdminTest.java @@ -0,0 +1,107 @@ +/* + * Copyright 2018 Google LLC. All Rights Reserved. + * + * 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 + * + * https://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.google.cloud.examples.bigtable; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import com.google.api.gax.rpc.NotFoundException; +import com.google.bigtable.admin.v2.ProjectName; +import com.google.cloud.bigtable.admin.v2.BigtableInstanceAdminClient; +import com.google.cloud.bigtable.admin.v2.BigtableInstanceAdminSettings; +import com.google.cloud.bigtable.admin.v2.models.Cluster; +import com.google.cloud.bigtable.admin.v2.models.CreateInstanceRequest; +import com.google.cloud.bigtable.admin.v2.models.Instance; +import com.google.cloud.bigtable.admin.v2.models.Instance.Type; +import com.google.cloud.bigtable.admin.v2.models.StorageType; +import java.io.IOException; +import java.util.List; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +public class InstanceAdminTest { + + private static final String GCLOUD_PROJECT = "test-project"; // Replace with your project ID + private static final String PRODUCTION_INSTANCE = "test-instance"; + private static final String PRODUCTION_CLUSTER = "test-cluster" + System.currentTimeMillis(); + private static BigtableInstanceAdminSettings instanceAdminSettings; + private static BigtableInstanceAdminClient adminClient; + + @BeforeClass + public static void beforeClass() throws IOException { + instanceAdminSettings = + BigtableInstanceAdminSettings.newBuilder().setProjectName(ProjectName.of(GCLOUD_PROJECT)) + .build(); + adminClient = BigtableInstanceAdminClient.create(instanceAdminSettings); + if (!adminClient.exists(PRODUCTION_INSTANCE)) { + adminClient.createInstance(CreateInstanceRequest.of(PRODUCTION_INSTANCE) + .addCluster(PRODUCTION_INSTANCE, "us-central1-f", 3, StorageType.SSD) + .setType(Type.PRODUCTION).addLabel("example", "instance_admin")); + } + } + + @AfterClass + public static void afterClass() { + adminClient.deleteInstance(PRODUCTION_INSTANCE); + adminClient.close(); + } + + @Test + public void testCreateAndDeleteInstance() { + // Create instance + Instance instance = + InstanceAdmin.createProdInstance(adminClient, "fake-instance", "fake-cluster"); + assertNotNull(instance); + + // Delete instance + InstanceAdmin.deleteInstance(adminClient, "fake-instance"); + assertTrue(!adminClient.exists("fake-instance")); + } + + @Test + public void testListInstances() { + // List instances + List instance = InstanceAdmin.listInstances(adminClient); + assertTrue(instance.size() > 0); + } + + @Test + public void testGetInstance() { + // Get instance + Instance instance = InstanceAdmin.getInstance(adminClient, PRODUCTION_INSTANCE); + assertNotNull(instance); + } + + @Test + public void testListClusters() { + // List clusters + List clusters = InstanceAdmin.listClusters(adminClient, PRODUCTION_INSTANCE); + assertTrue(clusters.size() > 0); + } + + @Test(expected = NotFoundException.class) + public void testAddAndDeleteCluster() { + // Add cluster + Cluster cluster = + InstanceAdmin.addCluster(adminClient, PRODUCTION_INSTANCE, PRODUCTION_CLUSTER); + assertNotNull(cluster); + + // Delete cluster + InstanceAdmin.deleteCluster(adminClient, PRODUCTION_INSTANCE, PRODUCTION_CLUSTER); + adminClient.getCluster(PRODUCTION_INSTANCE, PRODUCTION_CLUSTER); + } +} diff --git a/google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/TableAdminTest.java b/google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/TableAdminTest.java new file mode 100644 index 000000000000..249c1d515881 --- /dev/null +++ b/google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/TableAdminTest.java @@ -0,0 +1,136 @@ +/* + * Copyright 2018 Google LLC. All Rights Reserved. + * + * 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 + * + * https://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.google.cloud.examples.bigtable; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient; +import com.google.cloud.bigtable.admin.v2.BigtableTableAdminSettings; +import com.google.cloud.bigtable.admin.v2.models.ColumnFamily; +import com.google.cloud.bigtable.admin.v2.models.CreateTableRequest; +import com.google.cloud.bigtable.admin.v2.models.Table; +import java.io.IOException; +import java.util.Collection; +import java.util.List; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +public class TableAdminTest { + + private static final String GCLOUD_PROJECT = "test-project"; // Replace with your project ID + private static final String INSTANCE_ID = "test-instance"; // Replace with your instance ID + private static final String TABLE_ID = "test-table" + System.currentTimeMillis(); + private static BigtableTableAdminSettings adminSettings; + private static BigtableTableAdminClient adminClient; + + @BeforeClass + public static void beforeClass() throws IOException { + adminSettings = BigtableTableAdminSettings.newBuilder() + .setInstanceName(com.google.bigtable.admin.v2.InstanceName.of(GCLOUD_PROJECT, INSTANCE_ID)) + .build(); + adminClient = BigtableTableAdminClient.create(adminSettings); + if (!adminClient.exists(TABLE_ID)) { + adminClient.createTable(CreateTableRequest.of(TABLE_ID).addFamily("cf")); + } + } + + @AfterClass + public static void afterClass() { + adminClient.deleteTable(TABLE_ID); + adminClient.close(); + } + + @Test + public void testCreateAndDeleteTable() { + // Create table + Table table = TableAdmin.createTable(adminClient, "fake-table"); + assertNotNull(table); + + // Delete table + TableAdmin.deleteTable(adminClient, "fake-table"); + assertTrue(!adminClient.exists("fake-table")); + } + + @Test + public void testListAllTables() { + // List all tables in instance + List tables = TableAdmin.listAllTables(adminClient); + assertTrue(tables.size() > 0); + } + + @Test + public void testGetTableMeta() { + // Get table meta + Table table = TableAdmin.getTableMeta(adminClient, TABLE_ID); + assertNotNull(table); + } + + @Test + public void testCreateMaxAgeRuleAndModifyAndPrintColumnFamily() { + // Max age rule + Table table = TableAdmin.maxAgeRule(adminClient, TABLE_ID, "cf1"); + assertNotNull(table); + + // Modify cf1 + table = TableAdmin.modifyColumnFamilyRule(adminClient, TABLE_ID, "cf1"); + assertNotNull(table); + + // Print modified cf1 + ColumnFamily columnFamily = TableAdmin.printModifiedColumnFamily(adminClient, TABLE_ID, "cf1"); + assertNotNull(columnFamily); + } + + @Test + public void testCreateMaxVersionsRuleAndDeleteColumnFamily() { + // Max versions rule + Table table = TableAdmin.maxVersionsRule(adminClient, TABLE_ID, "cf2"); + assertNotNull(table); + + // Delete cf2 + table = TableAdmin.deleteColumnFamily(adminClient, TABLE_ID, "cf2"); + assertNotNull(table); + } + + @Test + public void testCreateUnionRule() { + // Union rule + Table table = TableAdmin.unionRule(adminClient, TABLE_ID, "cf3"); + assertNotNull(table); + } + + @Test + public void testCreateIntersectionRule() { + // Intersection rule + Table table = TableAdmin.intersectionRule(adminClient, TABLE_ID, "cf4"); + assertNotNull(table); + } + + @Test + public void testCreateNestedRule() { + // Nested rule + Table table = TableAdmin.nestedRule(adminClient, TABLE_ID, "cf5"); + assertNotNull(table); + } + + @Test + public void testListColumnFamilies() { + // List column families + Collection columnFamilies = TableAdmin.listColumnFamilies(adminClient, TABLE_ID); + assertTrue(columnFamilies.size() > 0); + } +} From 13b5c1c9a4ea28e3ed8f9eb41ced92e865d3f5d8 Mon Sep 17 00:00:00 2001 From: elisheva-qlogic Date: Thu, 6 Dec 2018 16:28:44 -0500 Subject: [PATCH 03/10] 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")); } From a5231c3c0086ab52d93bf776a024274df0ec02cc Mon Sep 17 00:00:00 2001 From: elisheva-qlogic Date: Thu, 6 Dec 2018 16:47:02 -0500 Subject: [PATCH 04/10] code format fix --- .../cloud/examples/bigtable/HelloWorld.java | 48 +++++++++++-------- .../examples/bigtable/InstanceAdmin.java | 36 ++++++++------ .../cloud/examples/bigtable/TableAdmin.java | 41 ++++++++-------- .../examples/bigtable/HelloWorldTest.java | 15 +++--- .../examples/bigtable/InstanceAdminTest.java | 11 +++-- .../examples/bigtable/TableAdminTest.java | 8 ++-- 6 files changed, 93 insertions(+), 66 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 a54d8f905fcb..cf5536e83460 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 @@ -46,16 +46,20 @@ public static void main(String... args) throws Exception { // [START connecting_to_bigtable] // Create the settings to configure a bigtable data client - BigtableDataSettings settings = BigtableDataSettings.newBuilder() - .setInstanceName(InstanceName.of(GCLOUD_PROJECT_ID, INSTANCE_ID)).build(); + BigtableDataSettings settings = + BigtableDataSettings.newBuilder() + .setInstanceName(InstanceName.of(GCLOUD_PROJECT_ID, INSTANCE_ID)) + .build(); // Create bigtable data client BigtableDataClient dataClient = BigtableDataClient.create(settings); // Create the settings to configure a bigtable admin client - BigtableTableAdminSettings adminSettings = BigtableTableAdminSettings.newBuilder() - .setInstanceName( - com.google.bigtable.admin.v2.InstanceName.of(GCLOUD_PROJECT_ID, INSTANCE_ID)).build(); + BigtableTableAdminSettings adminSettings = + BigtableTableAdminSettings.newBuilder() + .setInstanceName( + com.google.bigtable.admin.v2.InstanceName.of(GCLOUD_PROJECT_ID, INSTANCE_ID)) + .build(); // Create bigtable admin client BigtableTableAdminClient adminClient = BigtableTableAdminClient.create(adminSettings); @@ -84,8 +88,8 @@ public static void main(String... args) throws Exception { } } - public static Table createTable(BigtableTableAdminClient adminClient, String TABLE_ID, - String COLUMN_FAMILY_ID) { + public static Table createTable( + BigtableTableAdminClient adminClient, String TABLE_ID, String COLUMN_FAMILY_ID) { // [START creating_a_table] Table table; if (!adminClient.exists(TABLE_ID)) { @@ -100,8 +104,8 @@ public static Table createTable(BigtableTableAdminClient adminClient, String TAB // [END creating_a_table] } - private static Table creatingTable(BigtableTableAdminClient adminClient, String TABLE_ID, - String COLUMN_FAMILY_ID) { + 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); @@ -109,13 +113,17 @@ private static Table creatingTable(BigtableTableAdminClient adminClient, String return table; } - public static RowMutation writeToTable(BigtableDataClient dataClient, String TABLE_ID, - String ROW_KEY_PREFIX, String COLUMN_FAMILY_ID, String COLUMN_QUALIFIER) { + public static RowMutation writeToTable( + BigtableDataClient dataClient, + String TABLE_ID, + String ROW_KEY_PREFIX, + String COLUMN_FAMILY_ID, + String COLUMN_QUALIFIER) { // [START writing_rows] RowMutation mutation = null; try { System.out.println("Write some greetings to the table:"); - String[] greetings = { "Hello World!", "Hello Bigtable!", "Hello Java!" }; + String[] greetings = {"Hello World!", "Hello Bigtable!", "Hello Java!"}; for (int i = 0; i < greetings.length; i++) { RowMutation rowMutation = RowMutation.create(TABLE_ID, ROW_KEY_PREFIX + i); mutation = rowMutation.setCell(COLUMN_FAMILY_ID, COLUMN_QUALIFIER, greetings[i]); @@ -129,8 +137,8 @@ public static RowMutation writeToTable(BigtableDataClient dataClient, String TAB // [END writing_rows] } - public static Row readSingleRow(BigtableDataClient dataClient, String TABLE_ID, - String ROW_KEY_PREFIX) { + public static Row readSingleRow( + BigtableDataClient dataClient, String TABLE_ID, String ROW_KEY_PREFIX) { // [START reading_a_row] Row row = null; try { @@ -138,8 +146,9 @@ public static Row readSingleRow(BigtableDataClient dataClient, String TABLE_ID, row = dataClient.readRow(TABLE_ID, ROW_KEY_PREFIX + 0); System.out.println("Row: " + row.getKey().toStringUtf8()); for (RowCell cell : row.getCells()) { - System.out.printf("Family: %s Qualifier: %s Value: %s", cell.getFamily(), - cell.getQualifier().toStringUtf8(), cell.getValue().toStringUtf8()); + System.out.printf( + "Family: %s Qualifier: %s Value: %s", + cell.getFamily(), cell.getQualifier().toStringUtf8(), cell.getValue().toStringUtf8()); } } catch (Exception e) { System.out.println("Exception while reading a single row: " + e.getMessage()); @@ -158,8 +167,9 @@ public static ServerStream readTable(BigtableDataClient dataClient, String for (Row r : rowStream) { System.out.println("Row Key: " + r.getKey()); for (RowCell cell : r.getCells()) { - System.out.printf("Family: %s Qualifier: %s Value: %s", cell.getFamily(), - cell.getQualifier().toStringUtf8(), cell.getValue().toStringUtf8()); + System.out.printf( + "Family: %s Qualifier: %s Value: %s", + cell.getFamily(), cell.getQualifier().toStringUtf8(), cell.getValue().toStringUtf8()); } } } catch (Exception e) { @@ -180,4 +190,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 e142debf179a..a6cd1a81fd35 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 @@ -45,7 +45,8 @@ public static void main(String... args) { try { // Create the settings to configure a bigtable admin client BigtableInstanceAdminSettings instanceAdminSettings = - BigtableInstanceAdminSettings.newBuilder().setProjectName(ProjectName.of(GCLOUD_PROJECT)) + BigtableInstanceAdminSettings.newBuilder() + .setProjectName(ProjectName.of(GCLOUD_PROJECT)) .build(); // Create bigtable admin client @@ -78,8 +79,8 @@ public static void main(String... args) { } } - public static Instance createProdInstance(BigtableInstanceAdminClient adminClient, - String instanceID, String clusterID) { + public static Instance createProdInstance( + BigtableInstanceAdminClient adminClient, String instanceID, String clusterID) { System.out.println("Check if instance exists:"); // [START bigtable_check_instance_exists] boolean found = false; @@ -97,9 +98,11 @@ public static Instance createProdInstance(BigtableInstanceAdminClient adminClien // [START bigtable_create_prod_instance] // 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"); + CreateInstanceRequest createInstanceRequest = + CreateInstanceRequest.of(instanceID) + .addCluster(clusterID, "us-central1-f", 3, StorageType.SSD) + .setType(Type.PRODUCTION) + .addLabel("example", "instance_admin"); // Create production instance with given request try { instance = adminClient.createInstance(createInstanceRequest); @@ -155,8 +158,8 @@ public static Instance getInstance(BigtableInstanceAdminClient adminClient, Stri return instance; } - public static List listClusters(BigtableInstanceAdminClient adminClient, - String instanceID) { + public static List listClusters( + BigtableInstanceAdminClient adminClient, String instanceID) { System.out.println("\nListing Clusters:"); // [START bigtable_get_clusters] List clusters = null; @@ -185,15 +188,18 @@ public static void deleteInstance(BigtableInstanceAdminClient adminClient, Strin // [END bigtable_delete_instance] } - public static Cluster addCluster(BigtableInstanceAdminClient adminClient, String instanceID, - String clusterID) { + public static Cluster addCluster( + BigtableInstanceAdminClient adminClient, String instanceID, String clusterID) { Cluster cluster = null; System.out.println("\nAdding cluster to instance: " + instanceID); // [START bigtable_create_cluster] try { - cluster = adminClient.createCluster( - CreateClusterRequest.of(instanceID, clusterID).setZone("us-central1-c").setServeNodes(3) - .setStorageType(StorageType.SSD)); + cluster = + adminClient.createCluster( + CreateClusterRequest.of(instanceID, clusterID) + .setZone("us-central1-c") + .setServeNodes(3) + .setStorageType(StorageType.SSD)); System.out.printf("Cluster: %s created successfully", cluster.getId()); } catch (Exception e) { System.out.println("Error creating cluster: " + e.getMessage()); @@ -202,8 +208,8 @@ public static Cluster addCluster(BigtableInstanceAdminClient adminClient, String return cluster; } - public static void deleteCluster(BigtableInstanceAdminClient adminClient, String instanceID, - String clusterID) { + public static void deleteCluster( + BigtableInstanceAdminClient adminClient, String instanceID, String clusterID) { System.out.println("\nDeleting Cluster"); // [START bigtable_delete_cluster] try { diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/bigtable/TableAdmin.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/bigtable/TableAdmin.java index 56786533022c..f0fbd9a1a9af 100644 --- a/google-cloud-examples/src/main/java/com/google/cloud/examples/bigtable/TableAdmin.java +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/bigtable/TableAdmin.java @@ -42,9 +42,11 @@ public static void main(String... args) { try { // Create the settings to configure a bigtable admin client - BigtableTableAdminSettings adminSettings = BigtableTableAdminSettings.newBuilder() - .setInstanceName( - com.google.bigtable.admin.v2.InstanceName.of(GCLOUD_PROJECT, INSTANCE_ID)).build(); + BigtableTableAdminSettings adminSettings = + BigtableTableAdminSettings.newBuilder() + .setInstanceName( + com.google.bigtable.admin.v2.InstanceName.of(GCLOUD_PROJECT, INSTANCE_ID)) + .build(); // Create bigtable admin client BigtableTableAdminClient adminClient = BigtableTableAdminClient.create(adminSettings); @@ -175,8 +177,8 @@ public static Table maxAgeRule(BigtableTableAdminClient adminClient, String TABL return table; } - public static Table maxVersionsRule(BigtableTableAdminClient adminClient, String TABLE_ID, - String cf) { + public static Table maxVersionsRule( + BigtableTableAdminClient adminClient, String TABLE_ID, String cf) { System.out.printf("\nCreating column family %s with max versions GC rule:\n", cf); // [START bigtable_create_family_gc_max_versions] // Create a column family with GC policy : most recent N versions @@ -224,8 +226,8 @@ public static Table unionRule(BigtableTableAdminClient adminClient, String TABLE return table; } - public static Table intersectionRule(BigtableTableAdminClient adminClient, String TABLE_ID, - String cf) { + public static Table intersectionRule( + BigtableTableAdminClient adminClient, String TABLE_ID, String cf) { System.out.printf("\nCreating column family %s with intersection GC rule:\n", cf); // [START bigtable_create_family_gc_intersection] // Create a column family with GC policy to drop data that matches all conditions @@ -279,8 +281,8 @@ public static Table nestedRule(BigtableTableAdminClient adminClient, String TABL return table; } - public static Collection listColumnFamilies(BigtableTableAdminClient adminClient, - String TABLE_ID) { + public static Collection listColumnFamilies( + BigtableTableAdminClient adminClient, String TABLE_ID) { System.out.println("\nPrinting ID and GC Rule for all column families:"); // [START bigtable_list_column_families] // List all families in the table with GC rules @@ -298,8 +300,8 @@ public static Collection listColumnFamilies(BigtableTableAdminClie return columnFamilies; } - public static Table modifyColumnFamilyRule(BigtableTableAdminClient adminClient, String TABLE_ID, - String cf) { + public static Table modifyColumnFamilyRule( + BigtableTableAdminClient adminClient, String TABLE_ID, String cf) { System.out.printf("\nUpdating column family %s GC rule:\n", cf); // [START bigtable_update_gc_rule] // Update the column family metadata to update the GC rule @@ -319,8 +321,8 @@ public static Table modifyColumnFamilyRule(BigtableTableAdminClient adminClient, return table; } - public static ColumnFamily printModifiedColumnFamily(BigtableTableAdminClient adminClient, - String TABLE_ID, String cf) { + public static ColumnFamily printModifiedColumnFamily( + BigtableTableAdminClient adminClient, String TABLE_ID, String cf) { System.out.printf("\nPrint updated GC rule for column family: %s\n", cf); // [START bigtable_family_get_gc_rule] ColumnFamily colFamily = null; @@ -334,15 +336,15 @@ public static ColumnFamily printModifiedColumnFamily(BigtableTableAdminClient ad } } } catch (Exception ex) { - System.out - .printf("Error retrieving metadata for column family: %s\n%s\n", cf, ex.getMessage()); + System.out.printf( + "Error retrieving metadata for column family: %s\n%s\n", cf, ex.getMessage()); } // [END bigtable_family_get_gc_rule] return colFamily; } - public static Table deleteColumnFamily(BigtableTableAdminClient adminClient, String TABLE_ID, - String cf) { + public static Table deleteColumnFamily( + BigtableTableAdminClient adminClient, String TABLE_ID, String cf) { System.out.println("\nDelete column family: " + cf); // [START bigtable_delete_family] // Delete a column family @@ -372,7 +374,8 @@ public static void deleteTable(BigtableTableAdminClient adminClient, String TABL } private static void printColumnFamily(ColumnFamily columnFamily) { - System.out.printf("Column family: %s\nMetadata: %s\n", columnFamily.getId(), - columnFamily.getGCRule().toString()); + System.out.printf( + "Column family: %s\nMetadata: %s\n", + columnFamily.getId(), columnFamily.getGCRule().toString()); } } 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 2b3e8c277e0b..8b7e489f3f7c 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 @@ -49,12 +49,14 @@ public class HelloWorldTest { @BeforeClass public static void beforeClass() throws IOException { settings = - BigtableDataSettings.newBuilder().setInstanceName(InstanceName.of(PROJECT_ID, INSTANCE_ID)) + BigtableDataSettings.newBuilder() + .setInstanceName(InstanceName.of(PROJECT_ID, INSTANCE_ID)) .build(); dataClient = BigtableDataClient.create(settings); - adminSettings = BigtableTableAdminSettings.newBuilder() - .setInstanceName(com.google.bigtable.admin.v2.InstanceName.of(PROJECT_ID, INSTANCE_ID)) - .build(); + adminSettings = + BigtableTableAdminSettings.newBuilder() + .setInstanceName(com.google.bigtable.admin.v2.InstanceName.of(PROJECT_ID, INSTANCE_ID)) + .build(); adminClient = BigtableTableAdminClient.create(adminSettings); if (!adminClient.exists(TABLE_ID)) { adminClient.createTable(CreateTableRequest.of(TABLE_ID).addFamily(COLUMN_FAMILY_ID)); @@ -82,8 +84,9 @@ public void testCreateAndDeleteTable() { @Test public void testWriteToAndReadFromTable() { // Write to table - RowMutation rowMutation = HelloWorld - .writeToTable(dataClient, TABLE_ID, ROW_KEY_PREFIX, COLUMN_FAMILY_ID, COLUMN_QUALIFIER); + RowMutation rowMutation = + HelloWorld.writeToTable( + dataClient, TABLE_ID, ROW_KEY_PREFIX, COLUMN_FAMILY_ID, COLUMN_QUALIFIER); assertNotNull(rowMutation); // Read a single row diff --git a/google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/InstanceAdminTest.java b/google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/InstanceAdminTest.java index 85b4ba922349..f4f56c7a4320 100644 --- a/google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/InstanceAdminTest.java +++ b/google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/InstanceAdminTest.java @@ -44,13 +44,16 @@ public class InstanceAdminTest { @BeforeClass public static void beforeClass() throws IOException { instanceAdminSettings = - BigtableInstanceAdminSettings.newBuilder().setProjectName(ProjectName.of(GCLOUD_PROJECT)) + BigtableInstanceAdminSettings.newBuilder() + .setProjectName(ProjectName.of(GCLOUD_PROJECT)) .build(); adminClient = BigtableInstanceAdminClient.create(instanceAdminSettings); if (!adminClient.exists(PRODUCTION_INSTANCE)) { - adminClient.createInstance(CreateInstanceRequest.of(PRODUCTION_INSTANCE) - .addCluster(PRODUCTION_INSTANCE, "us-central1-f", 3, StorageType.SSD) - .setType(Type.PRODUCTION).addLabel("example", "instance_admin")); + adminClient.createInstance( + CreateInstanceRequest.of(PRODUCTION_INSTANCE) + .addCluster(PRODUCTION_INSTANCE, "us-central1-f", 3, StorageType.SSD) + .setType(Type.PRODUCTION) + .addLabel("example", "instance_admin")); } } diff --git a/google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/TableAdminTest.java b/google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/TableAdminTest.java index 249c1d515881..567c7eb26824 100644 --- a/google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/TableAdminTest.java +++ b/google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/TableAdminTest.java @@ -40,9 +40,11 @@ public class TableAdminTest { @BeforeClass public static void beforeClass() throws IOException { - adminSettings = BigtableTableAdminSettings.newBuilder() - .setInstanceName(com.google.bigtable.admin.v2.InstanceName.of(GCLOUD_PROJECT, INSTANCE_ID)) - .build(); + adminSettings = + BigtableTableAdminSettings.newBuilder() + .setInstanceName( + com.google.bigtable.admin.v2.InstanceName.of(GCLOUD_PROJECT, INSTANCE_ID)) + .build(); adminClient = BigtableTableAdminClient.create(adminSettings); if (!adminClient.exists(TABLE_ID)) { adminClient.createTable(CreateTableRequest.of(TABLE_ID).addFamily("cf")); From adcafd042afc5636aeb740245f2b6cf3a934c1b4 Mon Sep 17 00:00:00 2001 From: elisheva-qlogic Date: Tue, 18 Dec 2018 16:05:00 -0500 Subject: [PATCH 05/10] HelloWorld changes --- .../cloud/examples/bigtable/HelloWorld.java | 22 +++++-------------- 1 file changed, 6 insertions(+), 16 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 cf5536e83460..868807ff9374 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 @@ -88,29 +88,19 @@ public static void main(String... args) throws Exception { } } - public static Table createTable( - BigtableTableAdminClient adminClient, String TABLE_ID, String COLUMN_FAMILY_ID) { + public static Table createTable(BigtableTableAdminClient adminClient, String TABLE_ID, + String COLUMN_FAMILY_ID) { // [START creating_a_table] - Table table; - if (!adminClient.exists(TABLE_ID)) { - table = creatingTable(adminClient, TABLE_ID, COLUMN_FAMILY_ID); - } else { - // Delete and recreate table - System.out.println("Deleting existing table and creating new one"); + if (adminClient.exists(TABLE_ID)) { + System.out.println("Deleting existing table"); 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; + // [END creating_a_table] } public static RowMutation writeToTable( @@ -184,7 +174,7 @@ public static void deleteTable(BigtableTableAdminClient adminClient, String TABL System.out.println("Delete the table:"); try { adminClient.deleteTable(TABLE_ID); - System.out.printf("Table: %s deleted successfully", TABLE_ID); + System.out.printf("Table: %s deleted successfully\n", TABLE_ID); } catch (Exception e) { System.out.println("Exception while deleting table: " + e.getMessage()); } From 5cc4f1517426c7d9ae7c1e2364e01d7bdedfd2c7 Mon Sep 17 00:00:00 2001 From: elisheva-qlogic Date: Tue, 18 Dec 2018 16:18:46 -0500 Subject: [PATCH 06/10] system property for tests set up --- .../examples/bigtable/HelloWorldTest.java | 32 ++++++++++++------- .../examples/bigtable/InstanceAdminTest.java | 25 +++++++++++---- .../examples/bigtable/TableAdminTest.java | 26 +++++++++++---- 3 files changed, 59 insertions(+), 24 deletions(-) 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 8b7e489f3f7c..6b228d9d98fe 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 @@ -30,33 +30,35 @@ import com.google.cloud.bigtable.data.v2.models.RowMutation; import java.io.IOException; import org.junit.AfterClass; +import org.junit.AssumptionViolatedException; +import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; public class HelloWorldTest { - private static final String PROJECT_ID = "test-project"; // Replace with your project ID - private static final String INSTANCE_ID = "test-instance"; // Replace with your instance ID + private static final String INSTANCE_PROPERTY_NAME = "bigtable.instance"; private static final String TABLE_ID = "test-table" + System.currentTimeMillis(); private static final String COLUMN_FAMILY_ID = "test-cf"; private static final String COLUMN_QUALIFIER = "test-greeting"; private static final String ROW_KEY_PREFIX = "test-rowKey"; - private static BigtableDataSettings settings; private static BigtableDataClient dataClient; - private static BigtableTableAdminSettings adminSettings; private static BigtableTableAdminClient adminClient; @BeforeClass public static void beforeClass() throws IOException { - settings = - BigtableDataSettings.newBuilder() - .setInstanceName(InstanceName.of(PROJECT_ID, INSTANCE_ID)) + String targetInstance = System.getProperty(INSTANCE_PROPERTY_NAME); + if (targetInstance == null) { + dataClient = null; + adminClient = null; + return; + } + BigtableDataSettings settings = + BigtableDataSettings.newBuilder().setInstanceName(InstanceName.parse(targetInstance)) .build(); dataClient = BigtableDataClient.create(settings); - adminSettings = - BigtableTableAdminSettings.newBuilder() - .setInstanceName(com.google.bigtable.admin.v2.InstanceName.of(PROJECT_ID, INSTANCE_ID)) - .build(); + BigtableTableAdminSettings adminSettings = BigtableTableAdminSettings.newBuilder() + .setInstanceName(com.google.bigtable.admin.v2.InstanceName.parse(targetInstance)).build(); adminClient = BigtableTableAdminClient.create(adminSettings); if (!adminClient.exists(TABLE_ID)) { adminClient.createTable(CreateTableRequest.of(TABLE_ID).addFamily(COLUMN_FAMILY_ID)); @@ -70,6 +72,14 @@ public static void afterClass() throws Exception { adminClient.close(); } + @Before + public void setup() { + if (adminClient == null || dataClient == null) { + throw new AssumptionViolatedException( + INSTANCE_PROPERTY_NAME + " property is not set, skipping integration tests."); + } + } + @Test public void testCreateAndDeleteTable() { // Create table diff --git a/google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/InstanceAdminTest.java b/google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/InstanceAdminTest.java index f4f56c7a4320..e8d88c3effae 100644 --- a/google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/InstanceAdminTest.java +++ b/google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/InstanceAdminTest.java @@ -19,6 +19,7 @@ import static org.junit.Assert.assertTrue; import com.google.api.gax.rpc.NotFoundException; +import com.google.bigtable.admin.v2.InstanceName; import com.google.bigtable.admin.v2.ProjectName; import com.google.cloud.bigtable.admin.v2.BigtableInstanceAdminClient; import com.google.cloud.bigtable.admin.v2.BigtableInstanceAdminSettings; @@ -30,23 +31,27 @@ import java.io.IOException; import java.util.List; import org.junit.AfterClass; +import org.junit.AssumptionViolatedException; +import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; public class InstanceAdminTest { - private static final String GCLOUD_PROJECT = "test-project"; // Replace with your project ID + private static final String INSTANCE_PROPERTY_NAME = "bigtable.instance"; private static final String PRODUCTION_INSTANCE = "test-instance"; private static final String PRODUCTION_CLUSTER = "test-cluster" + System.currentTimeMillis(); - private static BigtableInstanceAdminSettings instanceAdminSettings; private static BigtableInstanceAdminClient adminClient; @BeforeClass public static void beforeClass() throws IOException { - instanceAdminSettings = - BigtableInstanceAdminSettings.newBuilder() - .setProjectName(ProjectName.of(GCLOUD_PROJECT)) - .build(); + String targetProject = System.getProperty(INSTANCE_PROPERTY_NAME); + ProjectName projectName = ProjectName.of(InstanceName.parse(targetProject).getProject()); + if (targetProject == null) { + adminClient = null; + } + BigtableInstanceAdminSettings instanceAdminSettings = + BigtableInstanceAdminSettings.newBuilder().setProjectName(projectName).build(); adminClient = BigtableInstanceAdminClient.create(instanceAdminSettings); if (!adminClient.exists(PRODUCTION_INSTANCE)) { adminClient.createInstance( @@ -63,6 +68,14 @@ public static void afterClass() { adminClient.close(); } + @Before + public void setup() { + if (adminClient == null) { + throw new AssumptionViolatedException( + INSTANCE_PROPERTY_NAME + " property is not set, skipping integration tests."); + } + } + @Test public void testCreateAndDeleteInstance() { // Create instance diff --git a/google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/TableAdminTest.java b/google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/TableAdminTest.java index 567c7eb26824..65f4f8ceba02 100644 --- a/google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/TableAdminTest.java +++ b/google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/TableAdminTest.java @@ -18,6 +18,7 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import com.google.bigtable.admin.v2.InstanceName; import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient; import com.google.cloud.bigtable.admin.v2.BigtableTableAdminSettings; import com.google.cloud.bigtable.admin.v2.models.ColumnFamily; @@ -27,23 +28,26 @@ import java.util.Collection; import java.util.List; import org.junit.AfterClass; +import org.junit.AssumptionViolatedException; +import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; public class TableAdminTest { - private static final String GCLOUD_PROJECT = "test-project"; // Replace with your project ID - private static final String INSTANCE_ID = "test-instance"; // Replace with your instance ID + private static final String INSTANCE_PROPERTY_NAME = "bigtable.instance"; private static final String TABLE_ID = "test-table" + System.currentTimeMillis(); - private static BigtableTableAdminSettings adminSettings; private static BigtableTableAdminClient adminClient; @BeforeClass public static void beforeClass() throws IOException { - adminSettings = - BigtableTableAdminSettings.newBuilder() - .setInstanceName( - com.google.bigtable.admin.v2.InstanceName.of(GCLOUD_PROJECT, INSTANCE_ID)) + String targetInstance = System.getProperty(INSTANCE_PROPERTY_NAME); + if (targetInstance == null) { + adminClient = null; + return; + } + BigtableTableAdminSettings adminSettings = + BigtableTableAdminSettings.newBuilder().setInstanceName(InstanceName.parse(targetInstance)) .build(); adminClient = BigtableTableAdminClient.create(adminSettings); if (!adminClient.exists(TABLE_ID)) { @@ -57,6 +61,14 @@ public static void afterClass() { adminClient.close(); } + @Before + public void setup() { + if (adminClient == null) { + throw new AssumptionViolatedException( + INSTANCE_PROPERTY_NAME + " property is not set, skipping integration tests."); + } + } + @Test public void testCreateAndDeleteTable() { // Create table From 7ad20e5907aea1b83be624dd849f10e07fb4065d Mon Sep 17 00:00:00 2001 From: elisheva-qlogic Date: Wed, 19 Dec 2018 09:13:13 -0500 Subject: [PATCH 07/10] code format changes --- .../com/google/cloud/examples/bigtable/HelloWorld.java | 4 ++-- .../google/cloud/examples/bigtable/HelloWorldTest.java | 9 ++++++--- .../google/cloud/examples/bigtable/TableAdminTest.java | 3 ++- 3 files changed, 10 insertions(+), 6 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 868807ff9374..698f2c7df25b 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 @@ -88,8 +88,8 @@ public static void main(String... args) throws Exception { } } - public static Table createTable(BigtableTableAdminClient adminClient, String TABLE_ID, - String COLUMN_FAMILY_ID) { + public static Table createTable( + BigtableTableAdminClient adminClient, String TABLE_ID, String COLUMN_FAMILY_ID) { // [START creating_a_table] if (adminClient.exists(TABLE_ID)) { System.out.println("Deleting existing table"); 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 6b228d9d98fe..8a1969112368 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 @@ -54,11 +54,14 @@ public static void beforeClass() throws IOException { return; } BigtableDataSettings settings = - BigtableDataSettings.newBuilder().setInstanceName(InstanceName.parse(targetInstance)) + BigtableDataSettings.newBuilder() + .setInstanceName(InstanceName.parse(targetInstance)) .build(); dataClient = BigtableDataClient.create(settings); - BigtableTableAdminSettings adminSettings = BigtableTableAdminSettings.newBuilder() - .setInstanceName(com.google.bigtable.admin.v2.InstanceName.parse(targetInstance)).build(); + BigtableTableAdminSettings adminSettings = + BigtableTableAdminSettings.newBuilder() + .setInstanceName(com.google.bigtable.admin.v2.InstanceName.parse(targetInstance)) + .build(); adminClient = BigtableTableAdminClient.create(adminSettings); if (!adminClient.exists(TABLE_ID)) { adminClient.createTable(CreateTableRequest.of(TABLE_ID).addFamily(COLUMN_FAMILY_ID)); diff --git a/google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/TableAdminTest.java b/google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/TableAdminTest.java index 65f4f8ceba02..f3ee8840eaf4 100644 --- a/google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/TableAdminTest.java +++ b/google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/TableAdminTest.java @@ -47,7 +47,8 @@ public static void beforeClass() throws IOException { return; } BigtableTableAdminSettings adminSettings = - BigtableTableAdminSettings.newBuilder().setInstanceName(InstanceName.parse(targetInstance)) + BigtableTableAdminSettings.newBuilder() + .setInstanceName(InstanceName.parse(targetInstance)) .build(); adminClient = BigtableTableAdminClient.create(adminSettings); if (!adminClient.exists(TABLE_ID)) { From e57bb89c9f7296a57fa285c583e03f5f5978ba3b Mon Sep 17 00:00:00 2001 From: elisheva-qlogic Date: Mon, 24 Dec 2018 13:39:07 -0500 Subject: [PATCH 08/10] changes --- google-cloud-examples/pom.xml | 6 + .../cloud/examples/bigtable/HelloWorld.java | 179 ++++++++++-------- .../examples/bigtable/InstanceAdmin.java | 38 ++-- .../cloud/examples/bigtable/TableAdmin.java | 93 +++++---- .../examples/bigtable/HelloWorldTest.java | 87 ++++++--- 5 files changed, 235 insertions(+), 168 deletions(-) diff --git a/google-cloud-examples/pom.xml b/google-cloud-examples/pom.xml index 9f4a24b3f0ea..cf784ce7da15 100644 --- a/google-cloud-examples/pom.xml +++ b/google-cloud-examples/pom.xml @@ -97,6 +97,12 @@ com.google.cloud google-cloud-bigtable-admin + + org.mockito + mockito-all + RELEASE + test + 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 698f2c7df25b..7bd242e89d59 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 @@ -15,11 +15,11 @@ */ package com.google.cloud.examples.bigtable; +import com.google.api.gax.rpc.ApiException; import com.google.api.gax.rpc.ServerStream; import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient; import com.google.cloud.bigtable.admin.v2.BigtableTableAdminSettings; import com.google.cloud.bigtable.admin.v2.models.CreateTableRequest; -import com.google.cloud.bigtable.admin.v2.models.Table; import com.google.cloud.bigtable.data.v2.BigtableDataClient; import com.google.cloud.bigtable.data.v2.BigtableDataSettings; import com.google.cloud.bigtable.data.v2.models.InstanceName; @@ -27,157 +27,178 @@ import com.google.cloud.bigtable.data.v2.models.Row; import com.google.cloud.bigtable.data.v2.models.RowCell; import com.google.cloud.bigtable.data.v2.models.RowMutation; +import java.io.IOException; +import java.util.Random; +import java.util.concurrent.TimeUnit; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class HelloWorld { - public static void main(String... args) throws Exception { + private static String projectId; + private static String instanceId; + private static String tableId; + private static BigtableDataClient dataClient; + private static BigtableTableAdminClient adminClient; - final String GCLOUD_PROJECT_ID = args[0]; - final String INSTANCE_ID = args[1]; - final String TABLE_ID = "Hello-Bigtable"; - final String COLUMN_FAMILY_ID = "cf1"; - final String COLUMN_QUALIFIER = "greeting"; - final String ROW_KEY_PREFIX = "rowKey"; + private static final String tablePrefix = "table"; + private static final String columnFamily = "cf1"; + private static final String columnQualifier = "greeting"; + private static final String rowKeyPrefix = "rowKey"; - if (args.length < 2) { + public static void main(String[] args) throws Exception { + + if (args.length != 2) { System.out.println("Missing required project id or instance id"); - return; } + projectId = args[0]; + instanceId = args[1]; + + HelloWorld helloWorld = new HelloWorld(projectId, instanceId, generateTableId()); + helloWorld.run(); + } + + public HelloWorld(String projectId, String instanceId, String tableId) throws IOException { + this.projectId = projectId; + this.instanceId = instanceId; + this.tableId = tableId; // [START connecting_to_bigtable] // Create the settings to configure a bigtable data client BigtableDataSettings settings = BigtableDataSettings.newBuilder() - .setInstanceName(InstanceName.of(GCLOUD_PROJECT_ID, INSTANCE_ID)) + .setInstanceName(InstanceName.of(projectId, instanceId)) .build(); // Create bigtable data client - BigtableDataClient dataClient = BigtableDataClient.create(settings); + dataClient = BigtableDataClient.create(settings); // Create the settings to configure a bigtable admin client BigtableTableAdminSettings adminSettings = BigtableTableAdminSettings.newBuilder() - .setInstanceName( - com.google.bigtable.admin.v2.InstanceName.of(GCLOUD_PROJECT_ID, INSTANCE_ID)) + .setInstanceName(com.google.bigtable.admin.v2.InstanceName.of(projectId, instanceId)) .build(); // Create bigtable admin client - BigtableTableAdminClient adminClient = BigtableTableAdminClient.create(adminSettings); + adminClient = BigtableTableAdminClient.create(adminSettings); // [END connecting_to_bigtable] + } + public void run() throws Exception { try { - // Check if table exists, create table if does not exist - createTable(adminClient, TABLE_ID, COLUMN_FAMILY_ID); - - // Write to table - writeToTable(dataClient, TABLE_ID, ROW_KEY_PREFIX, COLUMN_FAMILY_ID, COLUMN_QUALIFIER); - - // Read single row from the table - readSingleRow(dataClient, TABLE_ID, ROW_KEY_PREFIX); - - // Read entire table - readTable(dataClient, TABLE_ID); - - // Delete table - deleteTable(adminClient, TABLE_ID); - } catch (Exception e) { - System.out.println("Exception while running HelloWorld: " + e.getMessage()); + createTable(); + writeToTable(); + readSingleRow(); + readTable(); + deleteTable(); + garbageCollect(); + } catch (ApiException e) { + System.err.println("Exception while running HelloWorld: " + e.getMessage()); } finally { dataClient.close(); adminClient.close(); } } - public static Table createTable( - BigtableTableAdminClient adminClient, String TABLE_ID, String COLUMN_FAMILY_ID) { + public void createTable() { // [START creating_a_table] - if (adminClient.exists(TABLE_ID)) { - System.out.println("Deleting existing table"); - adminClient.deleteTable(TABLE_ID); + // Check if table exists, create table if does not exist + if (!adminClient.exists(tableId)) { + CreateTableRequest createTableRequest = + CreateTableRequest.of(tableId).addFamily(columnFamily); + System.out.println("Creating table: " + tableId); + adminClient.createTable(createTableRequest); + System.out.printf("Table %s created successfully%n", tableId); } - CreateTableRequest createTableRequest = - CreateTableRequest.of(TABLE_ID).addFamily(COLUMN_FAMILY_ID); - System.out.println("Creating table: " + TABLE_ID); - Table table = adminClient.createTable(createTableRequest); - return table; // [END creating_a_table] } - public static RowMutation writeToTable( - BigtableDataClient dataClient, - String TABLE_ID, - String ROW_KEY_PREFIX, - String COLUMN_FAMILY_ID, - String COLUMN_QUALIFIER) { + public void writeToTable() { // [START writing_rows] - RowMutation mutation = null; try { - System.out.println("Write some greetings to the table:"); + System.out.println("\nWriting some greetings to the table"); String[] greetings = {"Hello World!", "Hello Bigtable!", "Hello Java!"}; for (int i = 0; i < greetings.length; i++) { - RowMutation rowMutation = RowMutation.create(TABLE_ID, ROW_KEY_PREFIX + i); - mutation = rowMutation.setCell(COLUMN_FAMILY_ID, COLUMN_QUALIFIER, greetings[i]); + RowMutation rowMutation = + RowMutation.create(tableId, rowKeyPrefix + i) + .setCell(columnFamily, columnQualifier, greetings[i]); dataClient.mutateRow(rowMutation); System.out.println(greetings[i]); } - } catch (Exception e) { - System.out.println("Exception while writing to table: " + e.getMessage()); + } catch (ApiException e) { + System.err.println("Exception while writing to table: " + e.getMessage()); } - return mutation; // [END writing_rows] } - public static Row readSingleRow( - BigtableDataClient dataClient, String TABLE_ID, String ROW_KEY_PREFIX) { + public void readSingleRow() { // [START reading_a_row] - Row row = null; try { - System.out.println("Reading a single row by row key:"); - row = dataClient.readRow(TABLE_ID, ROW_KEY_PREFIX + 0); + System.out.println("\nReading a single row by row key"); + Row row = dataClient.readRow(tableId, rowKeyPrefix + 0); System.out.println("Row: " + row.getKey().toStringUtf8()); for (RowCell cell : row.getCells()) { System.out.printf( - "Family: %s Qualifier: %s Value: %s", + "Family: %s Qualifier: %s Value: %s%n", cell.getFamily(), cell.getQualifier().toStringUtf8(), cell.getValue().toStringUtf8()); } - } catch (Exception e) { - System.out.println("Exception while reading a single row: " + e.getMessage()); + } catch (ApiException e) { + System.err.println("Exception while reading a single row: " + e.getMessage()); } - return row; // [END reading_a_row] } - public static ServerStream readTable(BigtableDataClient dataClient, String TABLE_ID) { + public void readTable() { // [START scanning_all_rows] - ServerStream rowStream = null; try { - System.out.println("Reading the entire table:"); - Query query = Query.create(TABLE_ID); - rowStream = dataClient.readRows(query); + System.out.println("\n================= Reading the entire table ================="); + Query query = Query.create(tableId); + ServerStream rowStream = dataClient.readRows(query); for (Row r : rowStream) { - System.out.println("Row Key: " + r.getKey()); + System.out.println("Row Key: " + r.getKey().toStringUtf8()); for (RowCell cell : r.getCells()) { System.out.printf( - "Family: %s Qualifier: %s Value: %s", + "Family: %s Qualifier: %s Value: %s%n", cell.getFamily(), cell.getQualifier().toStringUtf8(), cell.getValue().toStringUtf8()); } } - } catch (Exception e) { - System.out.println("Exception while reading table: " + e.getMessage()); + } catch (ApiException e) { + System.err.println("Exception while reading table: " + e.getMessage()); } - return rowStream; // [END scanning_all_rows] } - public static void deleteTable(BigtableTableAdminClient adminClient, String TABLE_ID) { + public void deleteTable() { // [START deleting_a_table] - System.out.println("Delete the table:"); + System.out.println("\nDeleting table"); try { - adminClient.deleteTable(TABLE_ID); - System.out.printf("Table: %s deleted successfully\n", TABLE_ID); - } catch (Exception e) { - System.out.println("Exception while deleting table: " + e.getMessage()); + adminClient.deleteTable(tableId); + System.out.printf("Table %s deleted successfully%n", tableId); + } catch (ApiException e) { + System.err.println("Exception while deleting table: " + e.getMessage()); } // [END deleting_a_table] } + + private static String generateTableId() { + return String.format( + "%s-%016x-%d", tablePrefix, System.currentTimeMillis(), new Random().nextLong()); + } + + public void garbageCollect() { + Pattern timestampPattern = Pattern.compile(tablePrefix + "-([0-9]+)"); + for (String tableId : adminClient.listTables()) { + Matcher matcher = timestampPattern.matcher(tableId); + if (!matcher.matches()) { + continue; + } + String timestampStr = matcher.group(1); + long timestamp = Long.parseLong(timestampStr); + if (System.currentTimeMillis() - timestamp < TimeUnit.MINUTES.toMillis(15)) { + continue; + } + System.out.println("Garbage collecting orphaned table: " + tableId); + adminClient.deleteTable(tableId); + } + } } 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 a6cd1a81fd35..7c81f85a6077 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 @@ -15,6 +15,7 @@ */ package com.google.cloud.examples.bigtable; +import com.google.api.gax.rpc.ApiException; import com.google.api.gax.rpc.NotFoundException; import com.google.bigtable.admin.v2.ProjectName; import com.google.cloud.bigtable.admin.v2.BigtableInstanceAdminClient; @@ -31,13 +32,13 @@ public class InstanceAdmin { - public static void main(String... args) { + public static void main(String[] args) { final String GCLOUD_PROJECT = args[0]; final String PRODUCTION_INSTANCE = "ssd-instance"; final String PRODUCTION_CLUSTER = "ssd-cluster"; - if (args.length < 1) { + if (args.length != 1) { System.out.println("Missing required project id"); return; } @@ -75,7 +76,7 @@ public static void main(String... args) { deleteInstance(adminClient, PRODUCTION_INSTANCE); } catch (IOException ex) { - System.out.println("Exception while running InstanceAdmin: " + ex.getMessage()); + System.err.println("Exception while running InstanceAdmin: " + ex.getMessage()); } } @@ -86,8 +87,8 @@ public static Instance createProdInstance( boolean found = false; try { found = adminClient.exists(instanceID); - } catch (Exception e) { - System.out.println("Error checking if instance exists: " + e.getMessage()); + } catch (ApiException e) { + System.err.println("Error checking if instance exists: " + e.getMessage()); } // [END bigtable_check_instance_exists] @@ -107,8 +108,8 @@ public static Instance createProdInstance( try { instance = adminClient.createInstance(createInstanceRequest); System.out.printf("PRODUCTION type instance: %s, created successfully", instance.getId()); - } catch (Exception e) { - System.out.println("Error creating PRODUCTION instance: " + e.getMessage()); + } catch (ApiException e) { + System.err.println("Error creating PRODUCTION instance: " + e.getMessage()); System.exit(0); } // [END bigtable_create_prod_instance] @@ -128,8 +129,8 @@ public static List listInstances(BigtableInstanceAdminClient adminClie for (Instance instance : instances) { System.out.println(instance.getId()); } - } catch (Exception e) { - System.out.println("Error listing instances: " + e.getMessage()); + } catch (ApiException e) { + System.err.println("Error listing instances: " + e.getMessage()); } // [END bigtable_list_instances] return instances; @@ -151,8 +152,8 @@ public static Instance getInstance(BigtableInstanceAdminClient adminClient, Stri } System.out.println("State: " + instance.getState()); System.out.println("Type: " + instance.getType()); - } catch (Exception e) { - System.out.println("Error getting instance: " + e.getMessage()); + } catch (ApiException e) { + System.err.println("Error getting instance: " + e.getMessage()); } // [END bigtable_get_instance] return instance; @@ -168,8 +169,8 @@ public static List listClusters( for (Cluster cluster : clusters) { System.out.println(cluster.getId()); } - } catch (Exception e) { - System.out.println("Error listing clusters: " + e.getMessage()); + } catch (ApiException e) { + System.err.println("Error listing clusters: " + e.getMessage()); } // [END bigtable_get_clusters] return clusters; @@ -182,8 +183,7 @@ public static void deleteInstance(BigtableInstanceAdminClient adminClient, Strin adminClient.deleteInstance(instanceID); System.out.println("Instance deleted: " + instanceID); } catch (NotFoundException e) { - System.out.println("Error deleting instance: " + instanceID); - System.out.println(e.getMessage()); + System.err.println("Error deleting instance: " + instanceID + " " + e.getMessage()); } // [END bigtable_delete_instance] } @@ -201,8 +201,8 @@ public static Cluster addCluster( .setServeNodes(3) .setStorageType(StorageType.SSD)); System.out.printf("Cluster: %s created successfully", cluster.getId()); - } catch (Exception e) { - System.out.println("Error creating cluster: " + e.getMessage()); + } catch (ApiException e) { + System.err.println("Error creating cluster: " + e.getMessage()); } // [END bigtable_create_cluster] return cluster; @@ -215,8 +215,8 @@ public static void deleteCluster( try { adminClient.deleteCluster(instanceID, clusterID); System.out.printf("Cluster: %s deleted successfully", clusterID); - } catch (Exception e) { - System.out.println("Error deleting cluster: " + e.getMessage()); + } catch (ApiException e) { + System.err.println("Error deleting cluster: " + e.getMessage()); } // [END bigtable_delete_cluster] } diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/bigtable/TableAdmin.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/bigtable/TableAdmin.java index f0fbd9a1a9af..04e02a659658 100644 --- a/google-cloud-examples/src/main/java/com/google/cloud/examples/bigtable/TableAdmin.java +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/bigtable/TableAdmin.java @@ -15,6 +15,7 @@ */ package com.google.cloud.examples.bigtable; +import com.google.api.gax.rpc.ApiException; import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient; import com.google.cloud.bigtable.admin.v2.BigtableTableAdminSettings; import com.google.cloud.bigtable.admin.v2.models.ColumnFamily; @@ -25,17 +26,20 @@ import java.io.IOException; import java.util.Collection; import java.util.List; +import java.util.Random; import java.util.concurrent.TimeUnit; public class TableAdmin { - public static void main(String... args) { + private static final String tablePrefix = "table"; + + public static void main(String[] args) { final String GCLOUD_PROJECT = args[0]; final String INSTANCE_ID = args[1]; - final String TABLE_ID = "table"; + final String TABLE_ID = generateTableId(); - if (args.length < 2) { + if (args.length != 2) { System.out.println("Missing required project id or instance id"); return; } @@ -91,7 +95,7 @@ public static void main(String... args) { deleteTable(adminClient, TABLE_ID); } catch (IOException ex) { - System.out.println("Exception while running BigtableTableAdmin: " + ex.getMessage()); + System.err.println("Exception while running BigtableTableAdmin: " + ex.getMessage()); } } @@ -106,9 +110,9 @@ public static Table createTable(BigtableTableAdminClient adminClient, String TAB try { CreateTableRequest createTableRequest = CreateTableRequest.of(TABLE_ID).addFamily("cf"); table = adminClient.createTable(createTableRequest); - System.out.printf("Table: %s created successfully\n", table.getId()); - } catch (Exception ex) { - System.out.println("Error creating table: " + ex.getMessage()); + System.out.printf("Table: %s created successfully%n", table.getId()); + } catch (ApiException ex) { + System.err.println("Error creating table: " + ex.getMessage()); } } else { System.out.println("Table exists"); @@ -128,8 +132,8 @@ public static List listAllTables(BigtableTableAdminClient adminClient) { for (String tableName : listTables) { System.out.println(tableName); } - } catch (Exception ex) { - System.out.println("Error listing tables in current instance: " + ex.getMessage()); + } catch (ApiException ex) { + System.err.println("Error listing tables in current instance: " + ex.getMessage()); } // [END bigtable_list_tables] return listTables; @@ -147,15 +151,15 @@ public static Table getTableMeta(BigtableTableAdminClient adminClient, String TA for (ColumnFamily columnFamily : columnFamilies) { printColumnFamily(columnFamily); } - } catch (Exception ex) { - System.out.println("Error retrieving table metadata: " + ex.getMessage()); + } catch (ApiException ex) { + System.err.println("Error retrieving table metadata: " + ex.getMessage()); } // [END bigtable_get_table_metadata] return table; } public static Table maxAgeRule(BigtableTableAdminClient adminClient, String TABLE_ID, String cf) { - System.out.printf("\nCreating column family %s with max age GC rule:\n", cf); + System.out.printf("%nCreating column family %s with max age GC rule:%n", cf); // [START bigtable_create_family_gc_max_age] // Create a column family with GC policy : maximum age // where age = current time minus cell timestamp @@ -170,8 +174,8 @@ public static Table maxAgeRule(BigtableTableAdminClient adminClient, String TABL ModifyColumnFamiliesRequest.of(TABLE_ID).addFamily(cf, maxAgeRule1); table = adminClient.modifyFamilies(columnFamiliesRequest1); System.out.println("Created column family: " + cf); - } catch (Exception ex) { - System.out.println("Error creating column family: " + ex.getMessage()); + } catch (ApiException ex) { + System.err.println("Error creating column family: " + ex.getMessage()); } // [END bigtable_create_family_gc_max_age] return table; @@ -179,7 +183,7 @@ public static Table maxAgeRule(BigtableTableAdminClient adminClient, String TABL public static Table maxVersionsRule( BigtableTableAdminClient adminClient, String TABLE_ID, String cf) { - System.out.printf("\nCreating column family %s with max versions GC rule:\n", cf); + System.out.printf("%nCreating column family %s with max versions GC rule:%n", cf); // [START bigtable_create_family_gc_max_versions] // Create a column family with GC policy : most recent N versions // where 1 = most recent version @@ -194,15 +198,15 @@ public static Table maxVersionsRule( ModifyColumnFamiliesRequest.of(TABLE_ID).addFamily(cf, versionRule1); table = adminClient.modifyFamilies(columnFamiliesRequest2); System.out.println("Created column family: " + cf); - } catch (Exception ex) { - System.out.println("Error creating column family: " + ex.getMessage()); + } catch (ApiException ex) { + System.err.println("Error creating column family: " + ex.getMessage()); } // [END bigtable_create_family_gc_max_versions] return table; } public static Table unionRule(BigtableTableAdminClient adminClient, String TABLE_ID, String cf) { - System.out.printf("\nCreating column family %s with union GC rule:\n", cf); + System.out.printf("%nCreating column family %s with union GC rule:%n", cf); // [START bigtable_create_family_gc_union] // Create a column family with GC policy to drop data that matches at least one condition. @@ -219,8 +223,8 @@ public static Table unionRule(BigtableTableAdminClient adminClient, String TABLE ModifyColumnFamiliesRequest.of(TABLE_ID).addFamily(cf, unionRule1); table = adminClient.modifyFamilies(columnFamiliesRequest3); System.out.println("Created column family: " + cf); - } catch (Exception ex) { - System.out.println("Error creating column family: " + ex.getMessage()); + } catch (ApiException ex) { + System.err.println("Error creating column family: " + ex.getMessage()); } // [END bigtable_create_family_gc_union] return table; @@ -228,7 +232,7 @@ public static Table unionRule(BigtableTableAdminClient adminClient, String TABLE public static Table intersectionRule( BigtableTableAdminClient adminClient, String TABLE_ID, String cf) { - System.out.printf("\nCreating column family %s with intersection GC rule:\n", cf); + System.out.printf("%nCreating column family %s with intersection GC rule:%n", cf); // [START bigtable_create_family_gc_intersection] // Create a column family with GC policy to drop data that matches all conditions @@ -245,15 +249,15 @@ public static Table intersectionRule( ModifyColumnFamiliesRequest.of(TABLE_ID).addFamily(cf, intersectionRule1); table = adminClient.modifyFamilies(columnFamiliesRequest4); System.out.println("Created column family: " + cf); - } catch (Exception ex) { - System.out.println("Error creating column family: " + ex.getMessage()); + } catch (ApiException ex) { + System.err.println("Error creating column family: " + ex.getMessage()); } // [END bigtable_create_family_gc_intersection] return table; } public static Table nestedRule(BigtableTableAdminClient adminClient, String TABLE_ID, String cf) { - System.out.printf("\nCreating column family %s with a nested GC rule:\n", cf); + System.out.printf("%nCreating column family %s with a nested GC rule:%n", cf); // [START bigtable_create_family_gc_nested] // Create a nested GC rule: // Drop cells that are either older than the 10 recent versions @@ -274,8 +278,8 @@ public static Table nestedRule(BigtableTableAdminClient adminClient, String TABL ModifyColumnFamiliesRequest.of(TABLE_ID).addFamily(cf, unionRule2); table = adminClient.modifyFamilies(columnFamiliesRequest5); System.out.println("Created column family: " + cf); - } catch (Exception ex) { - System.out.println("Error creating column family: " + ex.getMessage()); + } catch (ApiException ex) { + System.err.println("Error creating column family: " + ex.getMessage()); } // [END bigtable_create_family_gc_nested] return table; @@ -293,8 +297,8 @@ public static Collection listColumnFamilies( for (ColumnFamily columnFamily : columnFamilies) { printColumnFamily(columnFamily); } - } catch (Exception ex) { - System.out.println("Error retrieving families: " + ex.getMessage()); + } catch (ApiException ex) { + System.err.println("Error retrieving families: " + ex.getMessage()); } // [END bigtable_list_column_families] return columnFamilies; @@ -302,7 +306,7 @@ public static Collection listColumnFamilies( public static Table modifyColumnFamilyRule( BigtableTableAdminClient adminClient, String TABLE_ID, String cf) { - System.out.printf("\nUpdating column family %s GC rule:\n", cf); + System.out.printf("%nUpdating column family %s GC rule:%n", cf); // [START bigtable_update_gc_rule] // Update the column family metadata to update the GC rule // Update a column family GC rule @@ -313,9 +317,9 @@ public static Table modifyColumnFamilyRule( ModifyColumnFamiliesRequest updateRequest = ModifyColumnFamiliesRequest.of(TABLE_ID).updateFamily(cf, versionRule6); table = adminClient.modifyFamilies(updateRequest); - System.out.printf("Column family %s GC rule updated\n", cf); - } catch (Exception ex) { - System.out.printf("Error updating GC rule for %s: %s\n", cf, ex.getMessage()); + System.out.printf("Column family %s GC rule updated%n", cf); + } catch (ApiException ex) { + System.err.printf("Error updating GC rule for %s: %s%n", cf, ex.getMessage()); } // [END bigtable_update_gc_rule] return table; @@ -323,7 +327,7 @@ public static Table modifyColumnFamilyRule( public static ColumnFamily printModifiedColumnFamily( BigtableTableAdminClient adminClient, String TABLE_ID, String cf) { - System.out.printf("\nPrint updated GC rule for column family: %s\n", cf); + System.out.printf("%nPrint updated GC rule for column family: %s%n", cf); // [START bigtable_family_get_gc_rule] ColumnFamily colFamily = null; try { @@ -335,9 +339,9 @@ public static ColumnFamily printModifiedColumnFamily( colFamily = columnFamily; } } - } catch (Exception ex) { - System.out.printf( - "Error retrieving metadata for column family: %s\n%s\n", cf, ex.getMessage()); + } catch (ApiException ex) { + System.err.printf( + "Error retrieving metadata for column family: %s%n%s%n", cf, ex.getMessage()); } // [END bigtable_family_get_gc_rule] return colFamily; @@ -352,9 +356,9 @@ public static Table deleteColumnFamily( try { ModifyColumnFamiliesRequest deleted = ModifyColumnFamiliesRequest.of(TABLE_ID).dropFamily(cf); table = adminClient.modifyFamilies(deleted); - System.out.printf("Column family %s deleted successfully\n", cf); - } catch (Exception ex) { - System.out.printf("Error deleting family: %s\n%s\n", cf, ex.getMessage()); + System.out.printf("Column family %s deleted successfully%n", cf); + } catch (ApiException ex) { + System.err.printf("Error deleting family: %s%n%s%n", cf, ex.getMessage()); } // [END bigtable_delete_family] return table; @@ -367,15 +371,20 @@ public static void deleteTable(BigtableTableAdminClient adminClient, String TABL try { adminClient.deleteTable(TABLE_ID); System.out.println("Table deleted: " + TABLE_ID); - } catch (Exception ex) { - System.out.printf("Error deleting table %s: %s\n", TABLE_ID, ex.getMessage()); + } catch (ApiException ex) { + System.err.printf("Error deleting table %s: %s%n", TABLE_ID, ex.getMessage()); } // [END bigtable_delete_table] } private static void printColumnFamily(ColumnFamily columnFamily) { System.out.printf( - "Column family: %s\nMetadata: %s\n", + "Column family: %s%nMetadata: %s%n", columnFamily.getId(), columnFamily.getGCRule().toString()); } + + private static String generateTableId() { + return String.format( + "%s-%016x-%d", tablePrefix, System.currentTimeMillis(), new Random().nextLong()); + } } 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 8a1969112368..13a6ce160b4f 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 @@ -15,20 +15,21 @@ */ package com.google.cloud.examples.bigtable; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; -import com.google.api.gax.rpc.ServerStream; import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient; import com.google.cloud.bigtable.admin.v2.BigtableTableAdminSettings; import com.google.cloud.bigtable.admin.v2.models.CreateTableRequest; -import com.google.cloud.bigtable.admin.v2.models.Table; import com.google.cloud.bigtable.data.v2.BigtableDataClient; import com.google.cloud.bigtable.data.v2.BigtableDataSettings; import com.google.cloud.bigtable.data.v2.models.InstanceName; import com.google.cloud.bigtable.data.v2.models.Row; -import com.google.cloud.bigtable.data.v2.models.RowMutation; +import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.io.OutputStream; +import java.io.PrintStream; import org.junit.AfterClass; import org.junit.AssumptionViolatedException; import org.junit.Before; @@ -38,79 +39,109 @@ public class HelloWorldTest { private static final String INSTANCE_PROPERTY_NAME = "bigtable.instance"; - private static final String TABLE_ID = "test-table" + System.currentTimeMillis(); - private static final String COLUMN_FAMILY_ID = "test-cf"; - private static final String COLUMN_QUALIFIER = "test-greeting"; - private static final String ROW_KEY_PREFIX = "test-rowKey"; + private static String tableId; + private static final String columnFamily = "cf1"; private static BigtableDataClient dataClient; private static BigtableTableAdminClient adminClient; + private static HelloWorld helloWorld; + private static InstanceName instanceName; @BeforeClass public static void beforeClass() throws IOException { String targetInstance = System.getProperty(INSTANCE_PROPERTY_NAME); + instanceName = InstanceName.parse(targetInstance); if (targetInstance == null) { dataClient = null; adminClient = null; return; } BigtableDataSettings settings = - BigtableDataSettings.newBuilder() - .setInstanceName(InstanceName.parse(targetInstance)) - .build(); + BigtableDataSettings.newBuilder().setInstanceName(instanceName).build(); dataClient = BigtableDataClient.create(settings); BigtableTableAdminSettings adminSettings = BigtableTableAdminSettings.newBuilder() .setInstanceName(com.google.bigtable.admin.v2.InstanceName.parse(targetInstance)) .build(); adminClient = BigtableTableAdminClient.create(adminSettings); - if (!adminClient.exists(TABLE_ID)) { - adminClient.createTable(CreateTableRequest.of(TABLE_ID).addFamily(COLUMN_FAMILY_ID)); - } } @AfterClass public static void afterClass() throws Exception { - adminClient.deleteTable(TABLE_ID); + adminClient.deleteTable(tableId); dataClient.close(); adminClient.close(); } @Before - public void setup() { + public void setup() throws IOException { if (adminClient == null || dataClient == null) { throw new AssumptionViolatedException( INSTANCE_PROPERTY_NAME + " property is not set, skipping integration tests."); } + tableId = "test-table"; + helloWorld = new HelloWorld(instanceName.getProject(), instanceName.getInstance(), tableId); + if (!adminClient.exists(tableId)) { + adminClient.createTable(CreateTableRequest.of(tableId).addFamily(columnFamily)); + } } @Test - public void testCreateAndDeleteTable() { + public void testCreateAndDeleteTable() throws IOException { // Create table - Table table = HelloWorld.createTable(adminClient, "fake-table", "fake-column-family-id"); - assertNotNull(table); + String fakeTable = "fake-table"; + HelloWorld testHelloWorld = + new HelloWorld(instanceName.getProject(), instanceName.getInstance(), fakeTable); + testHelloWorld.createTable(); + assertTrue(adminClient.exists(fakeTable)); // Delete table - HelloWorld.deleteTable(adminClient, "fake-table"); - assertTrue(!adminClient.exists("fake-table")); + testHelloWorld.deleteTable(); + assertTrue(!adminClient.exists(fakeTable)); } @Test public void testWriteToAndReadFromTable() { // Write to table - RowMutation rowMutation = - HelloWorld.writeToTable( - dataClient, TABLE_ID, ROW_KEY_PREFIX, COLUMN_FAMILY_ID, COLUMN_QUALIFIER); - assertNotNull(rowMutation); + helloWorld.writeToTable(); + Row row = dataClient.readRow(tableId, "rowKey0"); + assertNotNull(row); // Read a single row - Row row = HelloWorld.readSingleRow(dataClient, TABLE_ID, ROW_KEY_PREFIX); - assertNotNull(row); + OutputStream outputStream = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outputStream)); + helloWorld.readSingleRow(); + assertTrue(outputStream.toString().contains("Reading a single row by row key")); + assertTrue(outputStream.toString().contains("Row: rowKey0")); + assertTrue( + outputStream + .toString() + .contains("Family: cf1 Qualifier: greeting Value: Hello World!")); + + // Restore normal output + System.setOut(new PrintStream(System.out)); } @Test public void testReadTable() { // Read whole table - ServerStream stream = HelloWorld.readTable(dataClient, TABLE_ID); - assertNotNull(stream); + OutputStream outputStream = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outputStream)); + helloWorld.readTable(); + assertTrue( + outputStream.toString().contains("============= Reading the entire table =============")); + + // Restore normal output + System.setOut(new PrintStream(System.out)); + } + + @Test + public void testGarbageCollect() { + boolean exceptionThrown = false; + try { + helloWorld.garbageCollect(); + } catch (Exception e) { + exceptionThrown = true; + } + assertFalse(exceptionThrown); } } From b8b4f22c79cac8287a52f7e10ff9f9471c857b58 Mon Sep 17 00:00:00 2001 From: elisheva-qlogic Date: Thu, 27 Dec 2018 15:22:26 -0500 Subject: [PATCH 09/10] HelloWorld updates --- .../cloud/examples/bigtable/HelloWorld.java | 82 ++++++------------- ...{HelloWorldTest.java => ITHelloWorld.java} | 49 +++++++---- ...nceAdminTest.java => ITInstanceAdmin.java} | 5 +- ...{TableAdminTest.java => ITTableAdmin.java} | 2 +- 4 files changed, 61 insertions(+), 77 deletions(-) rename google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/{HelloWorldTest.java => ITHelloWorld.java} (79%) rename google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/{InstanceAdminTest.java => ITInstanceAdmin.java} (98%) rename google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/{TableAdminTest.java => ITTableAdmin.java} (99%) 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 7bd242e89d59..e96f291bbdda 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 @@ -28,33 +28,28 @@ import com.google.cloud.bigtable.data.v2.models.RowCell; import com.google.cloud.bigtable.data.v2.models.RowMutation; import java.io.IOException; -import java.util.Random; -import java.util.concurrent.TimeUnit; -import java.util.regex.Matcher; -import java.util.regex.Pattern; public class HelloWorld { - private static String projectId; - private static String instanceId; - private static String tableId; - private static BigtableDataClient dataClient; - private static BigtableTableAdminClient adminClient; + private static final String COLUMN_FAMILY = "cf1"; + private static final String COLUMN_QUALIFIER = "greeting"; + private static final String ROW_KEY_PREFIX = "rowKey"; - private static final String tablePrefix = "table"; - private static final String columnFamily = "cf1"; - private static final String columnQualifier = "greeting"; - private static final String rowKeyPrefix = "rowKey"; + private final String projectId; + private final String instanceId; + private final String tableId; + private final BigtableDataClient dataClient; + private final BigtableTableAdminClient adminClient; public static void main(String[] args) throws Exception { if (args.length != 2) { System.out.println("Missing required project id or instance id"); } - projectId = args[0]; - instanceId = args[1]; + String projectId = args[0]; + String instanceId = args[1]; - HelloWorld helloWorld = new HelloWorld(projectId, instanceId, generateTableId()); + HelloWorld helloWorld = new HelloWorld(projectId, instanceId, "test-table"); helloWorld.run(); } @@ -85,28 +80,23 @@ public HelloWorld(String projectId, String instanceId, String tableId) throws IO } public void run() throws Exception { - try { - createTable(); - writeToTable(); - readSingleRow(); - readTable(); - deleteTable(); - garbageCollect(); - } catch (ApiException e) { - System.err.println("Exception while running HelloWorld: " + e.getMessage()); - } finally { - dataClient.close(); - adminClient.close(); - } + createTable(); + writeToTable(); + readSingleRow(); + readTable(); + deleteTable(); + + dataClient.close(); + adminClient.close(); } public void createTable() { // [START creating_a_table] // Check if table exists, create table if does not exist if (!adminClient.exists(tableId)) { - CreateTableRequest createTableRequest = - CreateTableRequest.of(tableId).addFamily(columnFamily); System.out.println("Creating table: " + tableId); + CreateTableRequest createTableRequest = + CreateTableRequest.of(tableId).addFamily(COLUMN_FAMILY); adminClient.createTable(createTableRequest); System.out.printf("Table %s created successfully%n", tableId); } @@ -120,8 +110,8 @@ public void writeToTable() { String[] greetings = {"Hello World!", "Hello Bigtable!", "Hello Java!"}; for (int i = 0; i < greetings.length; i++) { RowMutation rowMutation = - RowMutation.create(tableId, rowKeyPrefix + i) - .setCell(columnFamily, columnQualifier, greetings[i]); + RowMutation.create(tableId, ROW_KEY_PREFIX + i) + .setCell(COLUMN_FAMILY, COLUMN_QUALIFIER, greetings[i]); dataClient.mutateRow(rowMutation); System.out.println(greetings[i]); } @@ -135,7 +125,7 @@ public void readSingleRow() { // [START reading_a_row] try { System.out.println("\nReading a single row by row key"); - Row row = dataClient.readRow(tableId, rowKeyPrefix + 0); + Row row = dataClient.readRow(tableId, ROW_KEY_PREFIX + 0); System.out.println("Row: " + row.getKey().toStringUtf8()); for (RowCell cell : row.getCells()) { System.out.printf( @@ -151,7 +141,7 @@ public void readSingleRow() { public void readTable() { // [START scanning_all_rows] try { - System.out.println("\n================= Reading the entire table ================="); + System.out.println("\nReading the entire table"); Query query = Query.create(tableId); ServerStream rowStream = dataClient.readRows(query); for (Row r : rowStream) { @@ -179,26 +169,4 @@ public void deleteTable() { } // [END deleting_a_table] } - - private static String generateTableId() { - return String.format( - "%s-%016x-%d", tablePrefix, System.currentTimeMillis(), new Random().nextLong()); - } - - public void garbageCollect() { - Pattern timestampPattern = Pattern.compile(tablePrefix + "-([0-9]+)"); - for (String tableId : adminClient.listTables()) { - Matcher matcher = timestampPattern.matcher(tableId); - if (!matcher.matches()) { - continue; - } - String timestampStr = matcher.group(1); - long timestamp = Long.parseLong(timestampStr); - if (System.currentTimeMillis() - timestamp < TimeUnit.MINUTES.toMillis(15)) { - continue; - } - System.out.println("Garbage collecting orphaned table: " + tableId); - adminClient.deleteTable(tableId); - } - } } 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/ITHelloWorld.java similarity index 79% rename from google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/HelloWorldTest.java rename to google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/ITHelloWorld.java index 13a6ce160b4f..aff75e4d48b8 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/ITHelloWorld.java @@ -15,7 +15,6 @@ */ package com.google.cloud.examples.bigtable; -import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; @@ -30,17 +29,22 @@ import java.io.IOException; import java.io.OutputStream; import java.io.PrintStream; +import java.util.Random; +import java.util.concurrent.TimeUnit; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.junit.AfterClass; import org.junit.AssumptionViolatedException; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; -public class HelloWorldTest { +public class ITHelloWorld { private static final String INSTANCE_PROPERTY_NAME = "bigtable.instance"; + private static final String TABLE_PREFIX = "table"; private static String tableId; - private static final String columnFamily = "cf1"; + private static final String COLUMN_FAMILY = "cf1"; private static BigtableDataClient dataClient; private static BigtableTableAdminClient adminClient; private static HelloWorld helloWorld; @@ -49,12 +53,12 @@ public class HelloWorldTest { @BeforeClass public static void beforeClass() throws IOException { String targetInstance = System.getProperty(INSTANCE_PROPERTY_NAME); - instanceName = InstanceName.parse(targetInstance); if (targetInstance == null) { dataClient = null; adminClient = null; return; } + instanceName = InstanceName.parse(targetInstance); BigtableDataSettings settings = BigtableDataSettings.newBuilder().setInstanceName(instanceName).build(); dataClient = BigtableDataClient.create(settings); @@ -68,6 +72,7 @@ public static void beforeClass() throws IOException { @AfterClass public static void afterClass() throws Exception { adminClient.deleteTable(tableId); + garbageCollect(); dataClient.close(); adminClient.close(); } @@ -78,17 +83,17 @@ public void setup() throws IOException { throw new AssumptionViolatedException( INSTANCE_PROPERTY_NAME + " property is not set, skipping integration tests."); } - tableId = "test-table"; + tableId = generateTableId(); helloWorld = new HelloWorld(instanceName.getProject(), instanceName.getInstance(), tableId); if (!adminClient.exists(tableId)) { - adminClient.createTable(CreateTableRequest.of(tableId).addFamily(columnFamily)); + adminClient.createTable(CreateTableRequest.of(tableId).addFamily(COLUMN_FAMILY)); } } @Test public void testCreateAndDeleteTable() throws IOException { // Create table - String fakeTable = "fake-table"; + String fakeTable = generateTableId(); HelloWorld testHelloWorld = new HelloWorld(instanceName.getProject(), instanceName.getInstance(), fakeTable); testHelloWorld.createTable(); @@ -127,21 +132,31 @@ public void testReadTable() { OutputStream outputStream = new ByteArrayOutputStream(); System.setOut(new PrintStream(outputStream)); helloWorld.readTable(); - assertTrue( - outputStream.toString().contains("============= Reading the entire table =============")); + assertTrue(outputStream.toString().contains("Reading the entire table")); // Restore normal output System.setOut(new PrintStream(System.out)); } - @Test - public void testGarbageCollect() { - boolean exceptionThrown = false; - try { - helloWorld.garbageCollect(); - } catch (Exception e) { - exceptionThrown = true; + private static String generateTableId() { + return String.format( + "%s-%016x-%d", TABLE_PREFIX, System.currentTimeMillis(), new Random().nextLong()); + } + + public static void garbageCollect() { + Pattern timestampPattern = Pattern.compile(TABLE_PREFIX + "-([0-9]+)"); + for (String tableId : adminClient.listTables()) { + Matcher matcher = timestampPattern.matcher(tableId); + if (!matcher.matches()) { + continue; + } + String timestampStr = matcher.group(1); + long timestamp = Long.parseLong(timestampStr); + if (System.currentTimeMillis() - timestamp < TimeUnit.MINUTES.toMillis(15)) { + continue; + } + System.out.println("Garbage collecting orphaned table: " + tableId); + adminClient.deleteTable(tableId); } - assertFalse(exceptionThrown); } } diff --git a/google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/InstanceAdminTest.java b/google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/ITInstanceAdmin.java similarity index 98% rename from google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/InstanceAdminTest.java rename to google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/ITInstanceAdmin.java index e8d88c3effae..7694bc5f23ec 100644 --- a/google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/InstanceAdminTest.java +++ b/google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/ITInstanceAdmin.java @@ -36,7 +36,7 @@ import org.junit.BeforeClass; import org.junit.Test; -public class InstanceAdminTest { +public class ITInstanceAdmin { private static final String INSTANCE_PROPERTY_NAME = "bigtable.instance"; private static final String PRODUCTION_INSTANCE = "test-instance"; @@ -46,10 +46,11 @@ public class InstanceAdminTest { @BeforeClass public static void beforeClass() throws IOException { String targetProject = System.getProperty(INSTANCE_PROPERTY_NAME); - ProjectName projectName = ProjectName.of(InstanceName.parse(targetProject).getProject()); if (targetProject == null) { adminClient = null; + return; } + ProjectName projectName = ProjectName.of(InstanceName.parse(targetProject).getProject()); BigtableInstanceAdminSettings instanceAdminSettings = BigtableInstanceAdminSettings.newBuilder().setProjectName(projectName).build(); adminClient = BigtableInstanceAdminClient.create(instanceAdminSettings); diff --git a/google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/TableAdminTest.java b/google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/ITTableAdmin.java similarity index 99% rename from google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/TableAdminTest.java rename to google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/ITTableAdmin.java index f3ee8840eaf4..cb28bbbfe453 100644 --- a/google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/TableAdminTest.java +++ b/google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/ITTableAdmin.java @@ -33,7 +33,7 @@ import org.junit.BeforeClass; import org.junit.Test; -public class TableAdminTest { +public class ITTableAdmin { private static final String INSTANCE_PROPERTY_NAME = "bigtable.instance"; private static final String TABLE_ID = "test-table" + System.currentTimeMillis(); From 99b5c634eb624040e7a0c74e4735d1196d53e797 Mon Sep 17 00:00:00 2001 From: elisheva-qlogic Date: Fri, 28 Dec 2018 10:13:49 -0500 Subject: [PATCH 10/10] exceptions specified --- .../cloud/examples/bigtable/HelloWorld.java | 17 ++++++----------- .../cloud/examples/bigtable/ITHelloWorld.java | 2 +- 2 files changed, 7 insertions(+), 12 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 e96f291bbdda..cac8d1febacc 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 @@ -15,7 +15,7 @@ */ package com.google.cloud.examples.bigtable; -import com.google.api.gax.rpc.ApiException; +import com.google.api.gax.rpc.NotFoundException; import com.google.api.gax.rpc.ServerStream; import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient; import com.google.cloud.bigtable.admin.v2.BigtableTableAdminSettings; @@ -34,9 +34,6 @@ public class HelloWorld { private static final String COLUMN_FAMILY = "cf1"; private static final String COLUMN_QUALIFIER = "greeting"; private static final String ROW_KEY_PREFIX = "rowKey"; - - private final String projectId; - private final String instanceId; private final String tableId; private final BigtableDataClient dataClient; private final BigtableTableAdminClient adminClient; @@ -54,8 +51,6 @@ public static void main(String[] args) throws Exception { } public HelloWorld(String projectId, String instanceId, String tableId) throws IOException { - this.projectId = projectId; - this.instanceId = instanceId; this.tableId = tableId; // [START connecting_to_bigtable] @@ -115,7 +110,7 @@ public void writeToTable() { dataClient.mutateRow(rowMutation); System.out.println(greetings[i]); } - } catch (ApiException e) { + } catch (NotFoundException e) { System.err.println("Exception while writing to table: " + e.getMessage()); } // [END writing_rows] @@ -132,7 +127,7 @@ public void readSingleRow() { "Family: %s Qualifier: %s Value: %s%n", cell.getFamily(), cell.getQualifier().toStringUtf8(), cell.getValue().toStringUtf8()); } - } catch (ApiException e) { + } catch (NotFoundException e) { System.err.println("Exception while reading a single row: " + e.getMessage()); } // [END reading_a_row] @@ -152,7 +147,7 @@ public void readTable() { cell.getFamily(), cell.getQualifier().toStringUtf8(), cell.getValue().toStringUtf8()); } } - } catch (ApiException e) { + } catch (NotFoundException e) { System.err.println("Exception while reading table: " + e.getMessage()); } // [END scanning_all_rows] @@ -160,11 +155,11 @@ public void readTable() { public void deleteTable() { // [START deleting_a_table] - System.out.println("\nDeleting table"); + System.out.println("\nDeleting table: " + tableId); try { adminClient.deleteTable(tableId); System.out.printf("Table %s deleted successfully%n", tableId); - } catch (ApiException e) { + } catch (NotFoundException e) { System.err.println("Exception while deleting table: " + e.getMessage()); } // [END deleting_a_table] diff --git a/google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/ITHelloWorld.java b/google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/ITHelloWorld.java index aff75e4d48b8..306ee34d1a7a 100644 --- a/google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/ITHelloWorld.java +++ b/google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/ITHelloWorld.java @@ -43,8 +43,8 @@ public class ITHelloWorld { private static final String INSTANCE_PROPERTY_NAME = "bigtable.instance"; private static final String TABLE_PREFIX = "table"; - private static String tableId; private static final String COLUMN_FAMILY = "cf1"; + private static String tableId; private static BigtableDataClient dataClient; private static BigtableTableAdminClient adminClient; private static HelloWorld helloWorld;