Skip to content

Commit 4905230

Browse files
author
Praful Makani
committed
docs(samples): address comments
1 parent 266d948 commit 4905230

File tree

2 files changed

+138
-20
lines changed

2 files changed

+138
-20
lines changed

samples/snippets/src/main/java/com/example/bigquery/QueryExternalBigtablePerm.java

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,35 +56,46 @@ public static void queryExternalBigtablePerm(
5656
// once, and can be reused for multiple requests.
5757
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
5858

59-
BigtableColumn name =
59+
BigtableColumnFamily.Builder statsSummary = BigtableColumnFamily.newBuilder();
60+
61+
// Configuring Columns
62+
BigtableColumn connectedCell =
6063
BigtableColumn.newBuilder()
61-
.setQualifierEncoded(Base64.encodeBase64String("name".getBytes()))
62-
.setFieldName("name")
64+
.setQualifierEncoded(Base64.encodeBase64String("connected_cell".getBytes()))
65+
.setFieldName("connected_cell")
6366
.setType("STRING")
6467
.setEncoding("TEXT")
6568
.build();
66-
BigtableColumn postAbbr =
69+
BigtableColumn connectedWifi =
6770
BigtableColumn.newBuilder()
68-
.setQualifierEncoded(Base64.encodeBase64String("post_abbr".getBytes()))
69-
.setFieldName("post_abbr")
71+
.setQualifierEncoded(Base64.encodeBase64String("connected_wifi".getBytes()))
72+
.setFieldName("connected_wifi")
7073
.setType("STRING")
7174
.setEncoding("TEXT")
7275
.build();
73-
BigtableColumnFamily usStates =
74-
BigtableColumnFamily.newBuilder()
75-
.setColumns(ImmutableList.of(name, postAbbr))
76-
.setFamilyID("us-states")
77-
.setOnlyReadLatest(true)
78-
.setEncoding("TEXT")
76+
BigtableColumn osBuild =
77+
BigtableColumn.newBuilder()
78+
.setQualifierEncoded(Base64.encodeBase64String("os_build".getBytes()))
79+
.setFieldName("os_build")
7980
.setType("STRING")
81+
.setEncoding("TEXT")
8082
.build();
8183

82-
// Configure BigtableOptions is optional.
84+
// Configuring column family and columns
85+
statsSummary
86+
.setColumns(ImmutableList.of(connectedCell, connectedWifi, osBuild))
87+
.setFamilyID("stats_summary")
88+
.setOnlyReadLatest(true)
89+
.setEncoding("TEXT")
90+
.setType("STRING")
91+
.build();
92+
93+
// Configuring BigtableOptions is optional.
8394
BigtableOptions options =
8495
BigtableOptions.newBuilder()
8596
.setIgnoreUnspecifiedColumnFamilies(true)
8697
.setReadRowkeyAsString(true)
87-
.setColumnFamilies(ImmutableList.of(usStates))
98+
.setColumnFamilies(ImmutableList.of(statsSummary.build()))
8899
.build();
89100

90101
TableId tableId = TableId.of(datasetName, tableName);

samples/snippets/src/test/java/com/example/bigquery/QueryExternalBigtablePermIT.java

Lines changed: 113 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,14 @@
1919
import static com.google.common.truth.Truth.assertThat;
2020
import static junit.framework.TestCase.assertNotNull;
2121

22+
import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient;
23+
import com.google.cloud.bigtable.admin.v2.models.CreateTableRequest;
24+
import com.google.cloud.bigtable.data.v2.BigtableDataClient;
25+
import com.google.cloud.bigtable.data.v2.models.BulkMutation;
26+
import com.google.cloud.bigtable.data.v2.models.Mutation;
27+
import com.google.protobuf.ByteString;
2228
import java.io.ByteArrayOutputStream;
29+
import java.io.IOException;
2330
import java.io.PrintStream;
2431
import java.util.UUID;
2532
import java.util.logging.Level;
@@ -32,13 +39,21 @@
3239
public class QueryExternalBigtablePermIT {
3340

3441
private final Logger log = Logger.getLogger(this.getClass().getName());
42+
private static final String ID = UUID.randomUUID().toString().substring(0, 8);
43+
private static final String TABLE_ID = "bigquery-samples-test" + ID;
44+
private static final String COLUMN_FAMILY_NAME = "stats_summary";
45+
private static final long TIMESTAMP = System.currentTimeMillis() * 1000;
46+
private static final String CONNECTED_CELL = "connected_cell";
47+
private static final String CONNECTED_WIFI = "connected_wifi";
48+
private static final String OS_BUILD = "os_build";
3549
private String tableName;
3650
private ByteArrayOutputStream bout;
3751
private PrintStream out;
3852
private PrintStream originalPrintStream;
3953

54+
private static final String INSTANCE = requireEnvVar("BIGTABLE_TESTING_INSTANCE");
55+
private static final String PROJECT = requireEnvVar("SAMPLES_TESTING_PROJECT");
4056
private static final String BIGQUERY_DATASET_NAME = requireEnvVar("BIGQUERY_DATASET_NAME");
41-
private static final String BIGTABLE_URI = requireEnvVar("BIGTABLE_URI");
4257

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

5166
@BeforeClass
5267
public static void checkRequirements() {
68+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
69+
requireEnvVar("BIGTABLE_TESTING_INSTANCE");
5370
requireEnvVar("BIGQUERY_DATASET_NAME");
54-
requireEnvVar("BIGTABLE_URI");
5571
}
5672

5773
@Before
58-
public void setUp() {
74+
public void setUp() throws IOException {
5975
// Create a test table
60-
tableName = "EXTERNAL_TABLE_FROM_BIGTABLE_TEST_" + UUID.randomUUID().toString().substring(0, 8);
76+
tableName = "EXTERNAL_TABLE_FROM_BIGTABLE_TEST_" + ID;
6177
bout = new ByteArrayOutputStream();
6278
out = new PrintStream(bout);
6379
originalPrintStream = System.out;
6480
System.setOut(out);
81+
82+
// create a temporary bigtable table.
83+
try (BigtableTableAdminClient client = BigtableTableAdminClient.create(PROJECT, INSTANCE)) {
84+
CreateTableRequest createTableRequest =
85+
CreateTableRequest.of(TABLE_ID).addFamily(COLUMN_FAMILY_NAME);
86+
client.createTable(createTableRequest);
87+
}
88+
// inserting temporary rows.
89+
try (BigtableDataClient client = BigtableDataClient.create(PROJECT, INSTANCE)) {
90+
BulkMutation bulkMutation =
91+
BulkMutation.create(TABLE_ID)
92+
.add(
93+
"phone#4c410523#20190501",
94+
Mutation.create()
95+
.setCell(
96+
COLUMN_FAMILY_NAME,
97+
ByteString.copyFrom(CONNECTED_CELL.getBytes()),
98+
TIMESTAMP,
99+
1)
100+
.setCell(
101+
COLUMN_FAMILY_NAME,
102+
ByteString.copyFrom(CONNECTED_WIFI.getBytes()),
103+
TIMESTAMP,
104+
1)
105+
.setCell(COLUMN_FAMILY_NAME, OS_BUILD, TIMESTAMP, "PQ2A.190405.003"))
106+
.add(
107+
"phone#4c410523#20190502",
108+
Mutation.create()
109+
.setCell(
110+
COLUMN_FAMILY_NAME,
111+
ByteString.copyFrom(CONNECTED_CELL.getBytes()),
112+
TIMESTAMP,
113+
1)
114+
.setCell(
115+
COLUMN_FAMILY_NAME,
116+
ByteString.copyFrom(CONNECTED_WIFI.getBytes()),
117+
TIMESTAMP,
118+
1)
119+
.setCell(COLUMN_FAMILY_NAME, OS_BUILD, TIMESTAMP, "PQ2A.190405.004"))
120+
.add(
121+
"phone#4c410523#20190505",
122+
Mutation.create()
123+
.setCell(
124+
COLUMN_FAMILY_NAME,
125+
ByteString.copyFrom(CONNECTED_CELL.getBytes()),
126+
TIMESTAMP,
127+
0)
128+
.setCell(
129+
COLUMN_FAMILY_NAME,
130+
ByteString.copyFrom(CONNECTED_WIFI.getBytes()),
131+
TIMESTAMP,
132+
1)
133+
.setCell(COLUMN_FAMILY_NAME, OS_BUILD, TIMESTAMP, "PQ2A.190406.000"))
134+
.add(
135+
"phone#5c10102#20190501",
136+
Mutation.create()
137+
.setCell(
138+
COLUMN_FAMILY_NAME,
139+
ByteString.copyFrom(CONNECTED_CELL.getBytes()),
140+
TIMESTAMP,
141+
1)
142+
.setCell(
143+
COLUMN_FAMILY_NAME,
144+
ByteString.copyFrom(CONNECTED_WIFI.getBytes()),
145+
TIMESTAMP,
146+
1)
147+
.setCell(COLUMN_FAMILY_NAME, OS_BUILD, TIMESTAMP, "PQ2A.190401.002"))
148+
.add(
149+
"phone#5c10102#20190502",
150+
Mutation.create()
151+
.setCell(
152+
COLUMN_FAMILY_NAME,
153+
ByteString.copyFrom(CONNECTED_CELL.getBytes()),
154+
TIMESTAMP,
155+
1)
156+
.setCell(
157+
COLUMN_FAMILY_NAME,
158+
ByteString.copyFrom(CONNECTED_WIFI.getBytes()),
159+
TIMESTAMP,
160+
0)
161+
.setCell(COLUMN_FAMILY_NAME, OS_BUILD, TIMESTAMP, "PQ2A.190406.000"));
162+
163+
client.bulkMutateRows(bulkMutation);
164+
}
65165
}
66166

67167
@After
68-
public void tearDown() {
168+
public void tearDown() throws IOException {
69169
// Clean up
170+
try (BigtableTableAdminClient client = BigtableTableAdminClient.create(PROJECT, INSTANCE)) {
171+
client.deleteTable(TABLE_ID);
172+
}
70173
DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName);
71174
// restores print statements in the original method
72175
System.out.flush();
@@ -77,8 +180,12 @@ public void tearDown() {
77180
@Test
78181
public void testQueryExternalBigtablePerm() {
79182
String query = String.format("SELECT * FROM %s.%s ", BIGQUERY_DATASET_NAME, tableName);
183+
String sourceUri =
184+
String.format(
185+
"https://googleapis.com/bigtable/projects/%s/instances/%s/tables/%s",
186+
PROJECT, INSTANCE, TABLE_ID);
80187
QueryExternalBigtablePerm.queryExternalBigtablePerm(
81-
BIGQUERY_DATASET_NAME, tableName, BIGTABLE_URI, query);
188+
BIGQUERY_DATASET_NAME, tableName, sourceUri, query);
82189
assertThat(bout.toString())
83190
.contains("Query on external permanent table performed successfully.");
84191
}

0 commit comments

Comments
 (0)