Skip to content

Commit

Permalink
docs(samples): address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Praful Makani committed Oct 7, 2020
1 parent 266d948 commit 4905230
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 20 deletions.
Expand Up @@ -56,35 +56,46 @@ public static void queryExternalBigtablePerm(
// once, and can be reused for multiple requests.
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();

BigtableColumn name =
BigtableColumnFamily.Builder statsSummary = BigtableColumnFamily.newBuilder();

// Configuring Columns
BigtableColumn connectedCell =
BigtableColumn.newBuilder()
.setQualifierEncoded(Base64.encodeBase64String("name".getBytes()))
.setFieldName("name")
.setQualifierEncoded(Base64.encodeBase64String("connected_cell".getBytes()))
.setFieldName("connected_cell")
.setType("STRING")
.setEncoding("TEXT")
.build();
BigtableColumn postAbbr =
BigtableColumn connectedWifi =
BigtableColumn.newBuilder()
.setQualifierEncoded(Base64.encodeBase64String("post_abbr".getBytes()))
.setFieldName("post_abbr")
.setQualifierEncoded(Base64.encodeBase64String("connected_wifi".getBytes()))
.setFieldName("connected_wifi")
.setType("STRING")
.setEncoding("TEXT")
.build();
BigtableColumnFamily usStates =
BigtableColumnFamily.newBuilder()
.setColumns(ImmutableList.of(name, postAbbr))
.setFamilyID("us-states")
.setOnlyReadLatest(true)
.setEncoding("TEXT")
BigtableColumn osBuild =
BigtableColumn.newBuilder()
.setQualifierEncoded(Base64.encodeBase64String("os_build".getBytes()))
.setFieldName("os_build")
.setType("STRING")
.setEncoding("TEXT")
.build();

// Configure BigtableOptions is optional.
// Configuring column family and columns
statsSummary
.setColumns(ImmutableList.of(connectedCell, connectedWifi, osBuild))
.setFamilyID("stats_summary")
.setOnlyReadLatest(true)
.setEncoding("TEXT")
.setType("STRING")
.build();

// Configuring BigtableOptions is optional.
BigtableOptions options =
BigtableOptions.newBuilder()
.setIgnoreUnspecifiedColumnFamilies(true)
.setReadRowkeyAsString(true)
.setColumnFamilies(ImmutableList.of(usStates))
.setColumnFamilies(ImmutableList.of(statsSummary.build()))
.build();

TableId tableId = TableId.of(datasetName, tableName);
Expand Down
Expand Up @@ -19,7 +19,14 @@
import static com.google.common.truth.Truth.assertThat;
import static junit.framework.TestCase.assertNotNull;

import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient;
import com.google.cloud.bigtable.admin.v2.models.CreateTableRequest;
import com.google.cloud.bigtable.data.v2.BigtableDataClient;
import com.google.cloud.bigtable.data.v2.models.BulkMutation;
import com.google.cloud.bigtable.data.v2.models.Mutation;
import com.google.protobuf.ByteString;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.UUID;
import java.util.logging.Level;
Expand All @@ -32,13 +39,21 @@
public class QueryExternalBigtablePermIT {

private final Logger log = Logger.getLogger(this.getClass().getName());
private static final String ID = UUID.randomUUID().toString().substring(0, 8);
private static final String TABLE_ID = "bigquery-samples-test" + ID;
private static final String COLUMN_FAMILY_NAME = "stats_summary";
private static final long TIMESTAMP = System.currentTimeMillis() * 1000;
private static final String CONNECTED_CELL = "connected_cell";
private static final String CONNECTED_WIFI = "connected_wifi";
private static final String OS_BUILD = "os_build";
private String tableName;
private ByteArrayOutputStream bout;
private PrintStream out;
private PrintStream originalPrintStream;

private static final String INSTANCE = requireEnvVar("BIGTABLE_TESTING_INSTANCE");
private static final String PROJECT = requireEnvVar("SAMPLES_TESTING_PROJECT");
private static final String BIGQUERY_DATASET_NAME = requireEnvVar("BIGQUERY_DATASET_NAME");
private static final String BIGTABLE_URI = requireEnvVar("BIGTABLE_URI");

private static String requireEnvVar(String varName) {
String value = System.getenv(varName);
Expand All @@ -50,23 +65,111 @@ private static String requireEnvVar(String varName) {

@BeforeClass
public static void checkRequirements() {
requireEnvVar("GOOGLE_CLOUD_PROJECT");
requireEnvVar("BIGTABLE_TESTING_INSTANCE");
requireEnvVar("BIGQUERY_DATASET_NAME");
requireEnvVar("BIGTABLE_URI");
}

@Before
public void setUp() {
public void setUp() throws IOException {
// Create a test table
tableName = "EXTERNAL_TABLE_FROM_BIGTABLE_TEST_" + UUID.randomUUID().toString().substring(0, 8);
tableName = "EXTERNAL_TABLE_FROM_BIGTABLE_TEST_" + ID;
bout = new ByteArrayOutputStream();
out = new PrintStream(bout);
originalPrintStream = System.out;
System.setOut(out);

// create a temporary bigtable table.
try (BigtableTableAdminClient client = BigtableTableAdminClient.create(PROJECT, INSTANCE)) {
CreateTableRequest createTableRequest =
CreateTableRequest.of(TABLE_ID).addFamily(COLUMN_FAMILY_NAME);
client.createTable(createTableRequest);
}
// inserting temporary rows.
try (BigtableDataClient client = BigtableDataClient.create(PROJECT, INSTANCE)) {
BulkMutation bulkMutation =
BulkMutation.create(TABLE_ID)
.add(
"phone#4c410523#20190501",
Mutation.create()
.setCell(
COLUMN_FAMILY_NAME,
ByteString.copyFrom(CONNECTED_CELL.getBytes()),
TIMESTAMP,
1)
.setCell(
COLUMN_FAMILY_NAME,
ByteString.copyFrom(CONNECTED_WIFI.getBytes()),
TIMESTAMP,
1)
.setCell(COLUMN_FAMILY_NAME, OS_BUILD, TIMESTAMP, "PQ2A.190405.003"))
.add(
"phone#4c410523#20190502",
Mutation.create()
.setCell(
COLUMN_FAMILY_NAME,
ByteString.copyFrom(CONNECTED_CELL.getBytes()),
TIMESTAMP,
1)
.setCell(
COLUMN_FAMILY_NAME,
ByteString.copyFrom(CONNECTED_WIFI.getBytes()),
TIMESTAMP,
1)
.setCell(COLUMN_FAMILY_NAME, OS_BUILD, TIMESTAMP, "PQ2A.190405.004"))
.add(
"phone#4c410523#20190505",
Mutation.create()
.setCell(
COLUMN_FAMILY_NAME,
ByteString.copyFrom(CONNECTED_CELL.getBytes()),
TIMESTAMP,
0)
.setCell(
COLUMN_FAMILY_NAME,
ByteString.copyFrom(CONNECTED_WIFI.getBytes()),
TIMESTAMP,
1)
.setCell(COLUMN_FAMILY_NAME, OS_BUILD, TIMESTAMP, "PQ2A.190406.000"))
.add(
"phone#5c10102#20190501",
Mutation.create()
.setCell(
COLUMN_FAMILY_NAME,
ByteString.copyFrom(CONNECTED_CELL.getBytes()),
TIMESTAMP,
1)
.setCell(
COLUMN_FAMILY_NAME,
ByteString.copyFrom(CONNECTED_WIFI.getBytes()),
TIMESTAMP,
1)
.setCell(COLUMN_FAMILY_NAME, OS_BUILD, TIMESTAMP, "PQ2A.190401.002"))
.add(
"phone#5c10102#20190502",
Mutation.create()
.setCell(
COLUMN_FAMILY_NAME,
ByteString.copyFrom(CONNECTED_CELL.getBytes()),
TIMESTAMP,
1)
.setCell(
COLUMN_FAMILY_NAME,
ByteString.copyFrom(CONNECTED_WIFI.getBytes()),
TIMESTAMP,
0)
.setCell(COLUMN_FAMILY_NAME, OS_BUILD, TIMESTAMP, "PQ2A.190406.000"));

client.bulkMutateRows(bulkMutation);
}
}

@After
public void tearDown() {
public void tearDown() throws IOException {
// Clean up
try (BigtableTableAdminClient client = BigtableTableAdminClient.create(PROJECT, INSTANCE)) {
client.deleteTable(TABLE_ID);
}
DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName);
// restores print statements in the original method
System.out.flush();
Expand All @@ -77,8 +180,12 @@ public void tearDown() {
@Test
public void testQueryExternalBigtablePerm() {
String query = String.format("SELECT * FROM %s.%s ", BIGQUERY_DATASET_NAME, tableName);
String sourceUri =
String.format(
"https://googleapis.com/bigtable/projects/%s/instances/%s/tables/%s",
PROJECT, INSTANCE, TABLE_ID);
QueryExternalBigtablePerm.queryExternalBigtablePerm(
BIGQUERY_DATASET_NAME, tableName, BIGTABLE_URI, query);
BIGQUERY_DATASET_NAME, tableName, sourceUri, query);
assertThat(bout.toString())
.contains("Query on external permanent table performed successfully.");
}
Expand Down

0 comments on commit 4905230

Please sign in to comment.