From 8ce9aef323f4c87af9f1cc013b9e568e32342330 Mon Sep 17 00:00:00 2001
From: elisheva-qlogic <43041115+elisheva-qlogic@users.noreply.github.com>
Date: Wed, 16 Jan 2019 13:31:20 -0500
Subject: [PATCH] Cloud Bigtable: HelloWorld sample updates (#4339)
* comments added in HelloWorld and ITHelloWorld
* removed typsafe name
* separate properties for bigtable.project and bigtable.instance
---
.../cloud/examples/bigtable/HelloWorld.java | 38 ++++++++++++-----
.../cloud/examples/bigtable/ITHelloWorld.java | 41 +++++++++++--------
2 files changed, 50 insertions(+), 29 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..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
@@ -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.
@@ -22,13 +22,25 @@
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.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.
+ *
+ *
+ * - create table
+ *
- read single row
+ *
- read table
+ *
- delete table
+ *
+ */
public class HelloWorld {
private static final String COLUMN_FAMILY = "cf1";
@@ -55,22 +67,21 @@ 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();
+ BigtableDataSettings.newBuilder().setProjectId(projectId).setInstanceId(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))
+ .setProjectId(projectId)
+ .setInstanceId(instanceId)
.build();
- // Create bigtable table admin client
+ // Creates a bigtable table admin client.
adminClient = BigtableTableAdminClient.create(adminSettings);
// [END connecting_to_bigtable]
}
@@ -85,9 +96,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 +110,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 +129,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 +147,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 +168,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..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
@@ -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.
@@ -23,7 +23,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.Row;
import java.io.IOException;
import java.util.Random;
@@ -37,31 +36,35 @@
import org.junit.BeforeClass;
import org.junit.Test;
+/** 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 InstanceName 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;
}
- instanceName = InstanceName.parse(targetInstance);
BigtableDataSettings settings =
- BigtableDataSettings.newBuilder().setInstanceName(instanceName).build();
+ BigtableDataSettings.newBuilder().setProjectId(projectId).setInstanceId(instanceId).build();
dataClient = BigtableDataClient.create(settings);
BigtableTableAdminSettings adminSettings =
BigtableTableAdminSettings.newBuilder()
- .setInstanceName(com.google.bigtable.admin.v2.InstanceName.parse(targetInstance))
+ .setProjectId(projectId)
+ .setInstanceId(instanceId)
.build();
adminClient = BigtableTableAdminClient.create(adminSettings);
}
@@ -77,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.getProject(), instanceName.getInstance(), tableId);
+ helloWorld = new HelloWorld(projectId, instanceId, tableId);
adminClient.createTable(CreateTableRequest.of(tableId).addFamily("cf1"));
}
@@ -93,21 +99,20 @@ public void after() {
@Test
public void testCreateAndDeleteTable() throws IOException {
- // Create table
- String fakeTable = generateTableId();
- HelloWorld testHelloWorld =
- new HelloWorld(instanceName.getProject(), instanceName.getInstance(), fakeTable);
+ // Creates a table.
+ String testTable = generateTableId();
+ HelloWorld testHelloWorld = new HelloWorld(projectId, instanceId, 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);