Skip to content

Commit

Permalink
Cloud Bigtable: HelloWorld sample updates (#4339)
Browse files Browse the repository at this point in the history
* comments added in HelloWorld and ITHelloWorld

* removed typsafe name

* separate properties for bigtable.project and bigtable.instance
  • Loading branch information
elisheva-qlogic authored and igorbernstein2 committed Jan 16, 2019
1 parent 209fc2a commit 8ce9aef
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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.
*
* <p>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.
*
* <ul>
* <li>create table
* <li>read single row
* <li>read table
* <li>delete table
* </ul>
*/
public class HelloWorld {

private static final String COLUMN_FAMILY = "cf1";
Expand All @@ -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]
}
Expand All @@ -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 =
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand All @@ -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);
}
Expand All @@ -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"));
}

Expand All @@ -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);
Expand Down

0 comments on commit 8ce9aef

Please sign in to comment.