Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 Down Expand Up @@ -29,6 +29,19 @@
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 Down Expand Up @@ -85,6 +98,7 @@ 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
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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);
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 @@ -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;
Expand All @@ -37,14 +37,16 @@
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";
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 projectName;
private static String instanceName;
private HelloWorld helloWorld;

@BeforeClass
Expand All @@ -55,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);
}
Expand All @@ -80,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"));
}

Expand All @@ -94,15 +101,14 @@ public void after() {
@Test
public void testCreateAndDeleteTable() throws IOException {
// Create table
String fakeTable = generateTableId();
HelloWorld testHelloWorld =
new HelloWorld(instanceName.getProject(), instanceName.getInstance(), fakeTable);
String testTable = generateTableId();
HelloWorld testHelloWorld = new HelloWorld(instanceName, instanceName, testTable);
testHelloWorld.createTable();
assertTrue(adminClient.exists(fakeTable));
assertTrue(adminClient.exists(testTable));

// Delete table
testHelloWorld.deleteTable();
assertTrue(!adminClient.exists(fakeTable));
assertTrue(!adminClient.exists(testTable));
}

@Test
Expand Down