From 303a913aba28e4df0882b969cbaebcb41d26b30a Mon Sep 17 00:00:00 2001 From: elisheva-qlogic Date: Mon, 14 Jan 2019 13:14:30 -0500 Subject: [PATCH 1/3] comments added in HelloWorld and ITHelloWorld --- .../cloud/examples/bigtable/HelloWorld.java | 30 +++++++++++++++---- .../cloud/examples/bigtable/ITHelloWorld.java | 17 ++++++----- 2 files changed, 33 insertions(+), 14 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 7d4ebde8787b..b604ed520e23 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,5 +1,5 @@ /* - * Copyright 2018 Google LLC. All Rights Reserved. + * Copyright 2019 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. @@ -29,6 +29,19 @@ import com.google.cloud.bigtable.data.v2.models.RowMutation; import java.io.IOException; +/** + * An example of using Google Cloud Bigtable. + * + *

This example is a very simple "hello world" application, that illustrates how to create a new + * table, write to the table, read the data back, and delete the table. + * + *

+ */ public class HelloWorld { private static final String COLUMN_FAMILY = "cf1"; @@ -55,22 +68,22 @@ public HelloWorld(String projectId, String instanceId, String tableId) throws IO this.tableId = tableId; // [START connecting_to_bigtable] - // Create the settings to configure a bigtable data client + // Creates the settings to configure a bigtable data client. BigtableDataSettings settings = BigtableDataSettings.newBuilder() .setInstanceName(InstanceName.of(projectId, instanceId)) .build(); - // Create bigtable data client + // Creates a bigtable data client. dataClient = BigtableDataClient.create(settings); - // Create the settings to configure a bigtable table admin client + // Creates the settings to configure a bigtable table admin client. BigtableTableAdminSettings adminSettings = BigtableTableAdminSettings.newBuilder() .setInstanceName(com.google.bigtable.admin.v2.InstanceName.of(projectId, instanceId)) .build(); - // Create bigtable table admin client + // Creates a bigtable table admin client. adminClient = BigtableTableAdminClient.create(adminSettings); // [END connecting_to_bigtable] } @@ -85,9 +98,10 @@ public void run() throws Exception { adminClient.close(); } + /** Demonstrates how to create a table. */ public void createTable() { // [START creating_a_table] - // Check if table exists, create table if does not exist + // Checks if table exists, creates table if does not exist. if (!adminClient.exists(tableId)) { System.out.println("Creating table: " + tableId); CreateTableRequest createTableRequest = @@ -98,6 +112,7 @@ public void createTable() { // [END creating_a_table] } + /** Demonstrates how to write some rows to a table. */ public void writeToTable() { // [START writing_rows] try { @@ -116,6 +131,7 @@ public void writeToTable() { // [END writing_rows] } + /** Demonstrates how to read a single row from a table. */ public void readSingleRow() { // [START reading_a_row] try { @@ -133,6 +149,7 @@ public void readSingleRow() { // [END reading_a_row] } + /** Demonstrates how to read an entire table. */ public void readTable() { // [START scanning_all_rows] try { @@ -153,6 +170,7 @@ public void readTable() { // [END scanning_all_rows] } + /** Demonstrates how to delete a table. */ public void deleteTable() { // [START deleting_a_table] System.out.println("\nDeleting table: " + tableId); 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 ba8010046fc0..0c14cf1056b2 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 @@ -1,5 +1,5 @@ /* - * Copyright 2018 Google LLC. All Rights Reserved. + * Copyright 2019 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. @@ -37,6 +37,7 @@ import org.junit.BeforeClass; import org.junit.Test; +/** Integration tests for {@link HelloWorld} */ public class ITHelloWorld { private static final String INSTANCE_PROPERTY_NAME = "bigtable.instance"; @@ -93,21 +94,21 @@ public void after() { @Test public void testCreateAndDeleteTable() throws IOException { - // Create table - String fakeTable = generateTableId(); + // Creates a table. + String testTable = generateTableId(); HelloWorld testHelloWorld = - new HelloWorld(instanceName.getProject(), instanceName.getInstance(), fakeTable); + new HelloWorld(instanceName.getProject(), instanceName.getInstance(), testTable); testHelloWorld.createTable(); - assertTrue(adminClient.exists(fakeTable)); + assertTrue(adminClient.exists(testTable)); - // Delete table + // Deletes a table. testHelloWorld.deleteTable(); - assertTrue(!adminClient.exists(fakeTable)); + assertTrue(!adminClient.exists(testTable)); } @Test public void testWriteToTable() { - // Write to table + // Writes to a table. helloWorld.writeToTable(); Row row = dataClient.readRow(tableId, "rowKey0"); assertNotNull(row); From a0b07a01007e7257078c4701a48396ac30715d46 Mon Sep 17 00:00:00 2001 From: elisheva-qlogic Date: Mon, 14 Jan 2019 13:19:53 -0500 Subject: [PATCH 2/3] removed typsafe name --- .../cloud/examples/bigtable/ITHelloWorld.java | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) 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 0c14cf1056b2..32ba6fc4c521 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 @@ -18,12 +18,12 @@ 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.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.Row; import java.io.IOException; import java.util.Random; @@ -45,7 +45,8 @@ public class ITHelloWorld { private static String tableId; private static BigtableDataClient dataClient; private static BigtableTableAdminClient adminClient; - private static InstanceName instanceName; + private static String projectName; + private static String instanceName; private HelloWorld helloWorld; @BeforeClass @@ -56,13 +57,18 @@ public static void beforeClass() throws IOException { adminClient = null; return; } - instanceName = InstanceName.parse(targetInstance); + projectName = InstanceName.parse(targetInstance).getProject(); + instanceName = InstanceName.parse(targetInstance).getInstance(); BigtableDataSettings settings = - BigtableDataSettings.newBuilder().setInstanceName(instanceName).build(); + BigtableDataSettings.newBuilder() + .setProjectId(projectName) + .setInstanceId(instanceName) + .build(); dataClient = BigtableDataClient.create(settings); BigtableTableAdminSettings adminSettings = BigtableTableAdminSettings.newBuilder() - .setInstanceName(com.google.bigtable.admin.v2.InstanceName.parse(targetInstance)) + .setProjectId(projectName) + .setInstanceId(instanceName) .build(); adminClient = BigtableTableAdminClient.create(adminSettings); } @@ -81,7 +87,7 @@ public void setup() throws IOException { INSTANCE_PROPERTY_NAME + " property is not set, skipping integration tests."); } tableId = generateTableId(); - helloWorld = new HelloWorld(instanceName.getProject(), instanceName.getInstance(), tableId); + helloWorld = new HelloWorld(instanceName, instanceName, tableId); adminClient.createTable(CreateTableRequest.of(tableId).addFamily("cf1")); } @@ -96,8 +102,7 @@ public void after() { public void testCreateAndDeleteTable() throws IOException { // Creates a table. String testTable = generateTableId(); - HelloWorld testHelloWorld = - new HelloWorld(instanceName.getProject(), instanceName.getInstance(), testTable); + HelloWorld testHelloWorld = new HelloWorld(instanceName, instanceName, testTable); testHelloWorld.createTable(); assertTrue(adminClient.exists(testTable)); From 3287ad697ce14d6664c6e2b3b2a0e48fb4f69b69 Mon Sep 17 00:00:00 2001 From: elisheva-qlogic Date: Tue, 15 Jan 2019 12:25:02 -0500 Subject: [PATCH 3/3] separate properties for bigtable.project and bigtable.instance --- .../cloud/examples/bigtable/HelloWorld.java | 8 ++--- .../cloud/examples/bigtable/ITHelloWorld.java | 31 +++++++++---------- 2 files changed, 18 insertions(+), 21 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 b604ed520e23..c24ffccbda4e 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 @@ -22,7 +22,6 @@ 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; @@ -70,9 +69,7 @@ public HelloWorld(String projectId, String instanceId, String tableId) throws IO // [START connecting_to_bigtable] // Creates the settings to configure a bigtable data client. BigtableDataSettings settings = - BigtableDataSettings.newBuilder() - .setInstanceName(InstanceName.of(projectId, instanceId)) - .build(); + BigtableDataSettings.newBuilder().setProjectId(projectId).setInstanceId(instanceId).build(); // Creates a bigtable data client. dataClient = BigtableDataClient.create(settings); @@ -80,7 +77,8 @@ public HelloWorld(String projectId, String instanceId, String tableId) throws IO // Creates the settings to configure a bigtable table admin client. BigtableTableAdminSettings adminSettings = BigtableTableAdminSettings.newBuilder() - .setInstanceName(com.google.bigtable.admin.v2.InstanceName.of(projectId, instanceId)) + .setProjectId(projectId) + .setInstanceId(instanceId) .build(); // Creates a bigtable table admin client. 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 32ba6fc4c521..c45650cab0e1 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 @@ -18,7 +18,6 @@ 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.CreateTableRequest; @@ -40,35 +39,32 @@ /** Integration tests for {@link HelloWorld} */ public class ITHelloWorld { + private static final String PROJECT_PROPERTY_NAME = "bigtable.project"; private static final String INSTANCE_PROPERTY_NAME = "bigtable.instance"; private static final String TABLE_PREFIX = "table"; private static String tableId; private static BigtableDataClient dataClient; private static BigtableTableAdminClient adminClient; - private static String projectName; - private static String instanceName; + private static String projectId; + private static String instanceId; private HelloWorld helloWorld; @BeforeClass public static void beforeClass() throws IOException { - String targetInstance = System.getProperty(INSTANCE_PROPERTY_NAME); - if (targetInstance == null) { + projectId = System.getProperty(PROJECT_PROPERTY_NAME); + instanceId = System.getProperty(INSTANCE_PROPERTY_NAME); + if (projectId == null || instanceId == null) { dataClient = null; adminClient = null; return; } - projectName = InstanceName.parse(targetInstance).getProject(); - instanceName = InstanceName.parse(targetInstance).getInstance(); BigtableDataSettings settings = - BigtableDataSettings.newBuilder() - .setProjectId(projectName) - .setInstanceId(instanceName) - .build(); + BigtableDataSettings.newBuilder().setProjectId(projectId).setInstanceId(instanceId).build(); dataClient = BigtableDataClient.create(settings); BigtableTableAdminSettings adminSettings = BigtableTableAdminSettings.newBuilder() - .setProjectId(projectName) - .setInstanceId(instanceName) + .setProjectId(projectId) + .setInstanceId(instanceId) .build(); adminClient = BigtableTableAdminClient.create(adminSettings); } @@ -84,10 +80,13 @@ public static void afterClass() throws Exception { public void setup() throws IOException { if (adminClient == null || dataClient == null) { throw new AssumptionViolatedException( - INSTANCE_PROPERTY_NAME + " property is not set, skipping integration tests."); + PROJECT_PROPERTY_NAME + + " or " + + INSTANCE_PROPERTY_NAME + + " property is not set, skipping integration tests."); } tableId = generateTableId(); - helloWorld = new HelloWorld(instanceName, instanceName, tableId); + helloWorld = new HelloWorld(projectId, instanceId, tableId); adminClient.createTable(CreateTableRequest.of(tableId).addFamily("cf1")); } @@ -102,7 +101,7 @@ public void after() { public void testCreateAndDeleteTable() throws IOException { // Creates a table. String testTable = generateTableId(); - HelloWorld testHelloWorld = new HelloWorld(instanceName, instanceName, testTable); + HelloWorld testHelloWorld = new HelloWorld(projectId, instanceId, testTable); testHelloWorld.createTable(); assertTrue(adminClient.exists(testTable));