From a9001a88f338fc2acf6bc48927765f29819124ee Mon Sep 17 00:00:00 2001 From: Dmitry <58846611+dmitry-fa@users.noreply.github.com> Date: Mon, 21 Dec 2020 23:08:39 +0300 Subject: [PATCH] docs: Expand hello world snippet to show how to access specific cells (#516) * samples: Expand hello world snippet to show how to access specific cells by family & qualifier * samples: Expand hello world snippet to show how to access specific cells by family & qualifier --- .../com/m/examples/bigtable/HelloWorld.java | 53 ++++++++++++++++--- .../m/examples/bigtable/HelloWorldTest.java | 25 ++++++--- 2 files changed, 62 insertions(+), 16 deletions(-) diff --git a/samples/snippets/src/main/java/com/m/examples/bigtable/HelloWorld.java b/samples/snippets/src/main/java/com/m/examples/bigtable/HelloWorld.java index 36e1fc08f..9668cdd80 100644 --- a/samples/snippets/src/main/java/com/m/examples/bigtable/HelloWorld.java +++ b/samples/snippets/src/main/java/com/m/examples/bigtable/HelloWorld.java @@ -12,7 +12,7 @@ * 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.m.examples.bigtable; @@ -29,6 +29,8 @@ 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.ArrayList; +import java.util.List; // [END bigtable_hw_imports_veneer] @@ -48,7 +50,8 @@ public class HelloWorld { private static final String COLUMN_FAMILY = "cf1"; - private static final String COLUMN_QUALIFIER = "greeting"; + private static final String COLUMN_QUALIFIER_GREETING = "greeting"; + private static final String COLUMN_QUALIFIER_NAME = "name"; private static final String ROW_KEY_PREFIX = "rowKey"; private final String tableId; private final BigtableDataClient dataClient; @@ -94,8 +97,13 @@ public void run() throws Exception { createTable(); writeToTable(); readSingleRow(); + readSpecificCells(); readTable(); deleteTable(); + close(); + } + + public void close() { dataClient.close(); adminClient.close(); } @@ -119,13 +127,15 @@ public void writeToTable() { // [START bigtable_hw_write_rows_veneer] try { 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++) { + String[] names = {"World", "Bigtable", "Java"}; + for (int i = 0; i < names.length; i++) { + String greeting = "Hello " + names[i] + "!"; RowMutation rowMutation = RowMutation.create(tableId, ROW_KEY_PREFIX + i) - .setCell(COLUMN_FAMILY, COLUMN_QUALIFIER, greetings[i]); + .setCell(COLUMN_FAMILY, COLUMN_QUALIFIER_NAME, names[i]) + .setCell(COLUMN_FAMILY, COLUMN_QUALIFIER_GREETING, greeting); dataClient.mutateRow(rowMutation); - System.out.println(greetings[i]); + System.out.println(greeting); } } catch (NotFoundException e) { System.err.println("Failed to write to non-existent table: " + e.getMessage()); @@ -134,7 +144,7 @@ public void writeToTable() { } /** Demonstrates how to read a single row from a table. */ - public void readSingleRow() { + public Row readSingleRow() { // [START bigtable_hw_get_by_key_veneer] try { System.out.println("\nReading a single row by row key"); @@ -145,29 +155,56 @@ public void readSingleRow() { "Family: %s Qualifier: %s Value: %s%n", cell.getFamily(), cell.getQualifier().toStringUtf8(), cell.getValue().toStringUtf8()); } + return row; + } catch (NotFoundException e) { + System.err.println("Failed to read from a non-existent table: " + e.getMessage()); + return null; + } + // [END bigtable_hw_get_by_key_veneer] + } + + /** Demonstrates how to access specific cells by family and qualifier. */ + public List readSpecificCells() { + // [START bigtable_hw_get_by_key_veneer] + try { + System.out.println("\nReading specific cells by family and qualifier"); + Row row = dataClient.readRow(tableId, ROW_KEY_PREFIX + 0); + System.out.println("Row: " + row.getKey().toStringUtf8()); + List cells = row.getCells(COLUMN_FAMILY, COLUMN_QUALIFIER_NAME); + for (RowCell cell : cells) { + System.out.printf( + "Family: %s Qualifier: %s Value: %s%n", + cell.getFamily(), cell.getQualifier().toStringUtf8(), cell.getValue().toStringUtf8()); + } + return cells; } catch (NotFoundException e) { System.err.println("Failed to read from a non-existent table: " + e.getMessage()); + return null; } // [END bigtable_hw_get_by_key_veneer] } /** Demonstrates how to read an entire table. */ - public void readTable() { + public List readTable() { // [START bigtable_hw_scan_all_veneer] try { System.out.println("\nReading the entire table"); Query query = Query.create(tableId); ServerStream rowStream = dataClient.readRows(query); + List tableRows = new ArrayList<>(); for (Row r : rowStream) { System.out.println("Row Key: " + r.getKey().toStringUtf8()); + tableRows.add(r); for (RowCell cell : r.getCells()) { System.out.printf( "Family: %s Qualifier: %s Value: %s%n", cell.getFamily(), cell.getQualifier().toStringUtf8(), cell.getValue().toStringUtf8()); } } + return tableRows; } catch (NotFoundException e) { System.err.println("Failed to read a non-existent table: " + e.getMessage()); + return null; } // [END bigtable_hw_scan_all_veneer] } diff --git a/samples/snippets/src/test/java/com/m/examples/bigtable/HelloWorldTest.java b/samples/snippets/src/test/java/com/m/examples/bigtable/HelloWorldTest.java index 985b37bbd..690416182 100644 --- a/samples/snippets/src/test/java/com/m/examples/bigtable/HelloWorldTest.java +++ b/samples/snippets/src/test/java/com/m/examples/bigtable/HelloWorldTest.java @@ -12,11 +12,13 @@ * 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.m.examples.bigtable; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient; @@ -24,7 +26,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.Row; import java.io.IOException; import java.util.Random; import java.util.concurrent.TimeUnit; @@ -32,7 +33,6 @@ import java.util.regex.Pattern; import org.junit.After; import org.junit.AfterClass; -import org.junit.AssumptionViolatedException; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; @@ -86,10 +86,11 @@ public void setup() throws IOException { } @After - public void after() { + public void teardown() { if (adminClient.exists(tableId)) { adminClient.deleteTable(tableId); } + helloWorld.close(); } @Test @@ -103,18 +104,26 @@ public void testCreateAndDeleteTable() throws IOException { // Deletes a table. testHelloWorld.deleteTable(); assertTrue(!adminClient.exists(testTable)); + testHelloWorld.close(); } @Test public void testWriteToTable() { // Writes to a table. + assertNull(dataClient.readRow(tableId, "rowKey0")); helloWorld.writeToTable(); - Row row = dataClient.readRow(tableId, "rowKey0"); - assertNotNull(row); + assertNotNull(dataClient.readRow(tableId, "rowKey0")); } - // TODO: add test for helloWorld.readSingleRow() - // TODO: add test for helloWorld.readTable() + @Test + public void testReads() { + assertEquals(0, helloWorld.readTable().size()); + helloWorld.writeToTable(); + + assertEquals(2, helloWorld.readSingleRow().getCells().size()); + assertEquals(1, helloWorld.readSpecificCells().size()); + assertEquals(3, helloWorld.readTable().size()); + } @Test public void testRunDoesNotFail() throws Exception {