Skip to content

Commit 154cbd3

Browse files
Read and Filter Snippets (GoogleCloudPlatform#1747)
* WIP for Bigtable Read and Filter snippets * Remaining Filter snippets WIP * All tests working, improved interleave label and condition, removed sink * Changing time to java.time Adding comments about filtering Handling exceptions Reformatting Reads print output other cleanups * Updating function declarations * Cleanup requireEnv function.
1 parent c5a6f1f commit 154cbd3

File tree

9 files changed

+1835
-12
lines changed

9 files changed

+1835
-12
lines changed

bigtable/snippets/pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@
3737
<version>4.13-beta-3</version>
3838
<scope>test</scope>
3939
</dependency>
40+
<dependency>
41+
<groupId>com.google.truth</groupId>
42+
<artifactId>truth</artifactId>
43+
<version>1.0</version>
44+
<scope>test</scope>
45+
</dependency>
4046
<!-- https://mvnrepository.com/artifact/com.google.cloud/google-cloud-bigtable -->
4147
<dependency>
4248
<groupId>com.google.cloud</groupId>

bigtable/snippets/src/main/java/com/example/bigtable/Filters.java

+459
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,272 @@
1+
/*
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.bigtable;
18+
19+
// [START bigtable_reads_row]
20+
// [START bigtable_reads_row_partial]
21+
// [START bigtable_reads_rows]
22+
// [START bigtable_reads_row_range]
23+
// [START bigtable_reads_row_ranges]
24+
// [START bigtable_reads_prefix]
25+
// [START bigtable_reads_filter]
26+
27+
import static com.google.cloud.bigtable.data.v2.models.Filters.FILTERS;
28+
29+
import com.google.api.gax.rpc.ServerStream;
30+
import com.google.cloud.bigtable.data.v2.BigtableDataClient;
31+
import com.google.cloud.bigtable.data.v2.models.Filters;
32+
import com.google.cloud.bigtable.data.v2.models.Query;
33+
import com.google.cloud.bigtable.data.v2.models.Row;
34+
import com.google.cloud.bigtable.data.v2.models.RowCell;
35+
import com.google.cloud.bigtable.data.v2.models.RowMutation;
36+
import com.google.protobuf.ByteString;
37+
import java.io.IOException;
38+
39+
public class Reads {
40+
// [END bigtable_reads_row]
41+
// [END bigtable_reads_row_partial]
42+
// [END bigtable_reads_rows]
43+
// [END bigtable_reads_row_range]
44+
// [END bigtable_reads_row_ranges]
45+
// [END bigtable_reads_prefix]
46+
// [END bigtable_reads_filter]
47+
48+
// [START bigtable_reads_row]
49+
public static void readRow() {
50+
// TODO(developer): Replace these variables before running the sample.
51+
String projectId = "my-project-id";
52+
String instanceId = "my-instance-id";
53+
String tableId = "mobile-time-series";
54+
readRow(projectId, instanceId, tableId);
55+
}
56+
57+
public static void readRow(String projectId, String instanceId, String tableId) {
58+
// Initialize client that will be used to send requests. This client only needs to be created
59+
// once, and can be reused for multiple requests. After completing all of your requests, call
60+
// the "close" method on the client to safely clean up any remaining background resources.
61+
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
62+
String rowkey = "phone#4c410523#20190501";
63+
64+
Row row = dataClient.readRow(tableId, rowkey);
65+
printRow(row);
66+
67+
} catch (IOException e) {
68+
System.out.println(
69+
"Unable to intailize service client, as a network error occured: \n" + e.toString());
70+
}
71+
}
72+
// [END bigtable_reads_row]
73+
74+
// [START bigtable_reads_row_partial]
75+
public static void readRowPartial() {
76+
// TODO(developer): Replace these variables before running the sample.
77+
String projectId = "my-project-id";
78+
String instanceId = "my-instance-id";
79+
String tableId = "mobile-time-series";
80+
readRowPartial(projectId, instanceId, tableId);
81+
}
82+
83+
public static void readRowPartial(String projectId, String instanceId, String tableId) {
84+
// Initialize client that will be used to send requests. This client only needs to be created
85+
// once, and can be reused for multiple requests. After completing all of your requests, call
86+
// the "close" method on the client to safely clean up any remaining background resources.
87+
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
88+
String rowkey = "phone#4c410523#20190501";
89+
Filters.Filter filter =
90+
FILTERS
91+
.chain()
92+
.filter(FILTERS.family().exactMatch("stats_summary"))
93+
.filter(FILTERS.qualifier().exactMatch("os_build"));
94+
95+
Row row = dataClient.readRow(tableId, rowkey, filter);
96+
printRow(row);
97+
98+
} catch (IOException e) {
99+
System.out.println(
100+
"Unable to intailize service client, as a network error occured: \n" + e.toString());
101+
}
102+
}
103+
// [END bigtable_reads_row_partial]
104+
105+
// [START bigtable_reads_rows]
106+
public static void readRows() {
107+
// TODO(developer): Replace these variables before running the sample.
108+
String projectId = "my-project-id";
109+
String instanceId = "my-instance-id";
110+
String tableId = "mobile-time-series";
111+
readRows(projectId, instanceId, tableId);
112+
}
113+
114+
public static void readRows(String projectId, String instanceId, String tableId) {
115+
// Initialize client that will be used to send requests. This client only needs to be created
116+
// once, and can be reused for multiple requests. After completing all of your requests, call
117+
// the "close" method on the client to safely clean up any remaining background resources.
118+
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
119+
Query query =
120+
Query.create(tableId).rowKey("phone#4c410523#20190501").rowKey("phone#4c410523#20190502");
121+
ServerStream<Row> rows = dataClient.readRows(query);
122+
for (Row row : rows) {
123+
printRow(row);
124+
}
125+
} catch (IOException e) {
126+
System.out.println(
127+
"Unable to intailize service client, as a network error occured: \n" + e.toString());
128+
}
129+
}
130+
// [END bigtable_reads_rows]
131+
132+
// [START bigtable_reads_row_range]
133+
public static void readRowRange() {
134+
// TODO(developer): Replace these variables before running the sample.
135+
String projectId = "my-project-id";
136+
String instanceId = "my-instance-id";
137+
String tableId = "mobile-time-series";
138+
readRowRange(projectId, instanceId, tableId);
139+
}
140+
141+
public static void readRowRange(String projectId, String instanceId, String tableId) {
142+
String start = "phone#4c410523#20190501";
143+
String end = "phone#4c410523#201906201";
144+
145+
// Initialize client that will be used to send requests. This client only needs to be created
146+
// once, and can be reused for multiple requests. After completing all of your requests, call
147+
// the "close" method on the client to safely clean up any remaining background resources.
148+
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
149+
Query query = Query.create(tableId).range(start, end);
150+
ServerStream<Row> rows = dataClient.readRows(query);
151+
for (Row row : rows) {
152+
printRow(row);
153+
}
154+
} catch (IOException e) {
155+
System.out.println(
156+
"Unable to intailize service client, as a network error occured: \n" + e.toString());
157+
}
158+
}
159+
// [END bigtable_reads_row_range]
160+
161+
// [START bigtable_reads_row_ranges]
162+
public static void readRowRanges() {
163+
// TODO(developer): Replace these variables before running the sample.
164+
String projectId = "my-project-id";
165+
String instanceId = "my-instance-id";
166+
String tableId = "mobile-time-series";
167+
readRowRanges(projectId, instanceId, tableId);
168+
}
169+
170+
public static void readRowRanges(String projectId, String instanceId, String tableId) {
171+
// Initialize client that will be used to send requests. This client only needs to be created
172+
// once, and can be reused for multiple requests. After completing all of your requests, call
173+
// the "close" method on the client to safely clean up any remaining background resources.
174+
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
175+
Query query =
176+
Query.create(tableId)
177+
.range("phone#4c410523#20190501", "phone#4c410523#20190601")
178+
.range("phone#5c10102#20190501", "phone#5c10102#20190601");
179+
ServerStream<Row> rows = dataClient.readRows(query);
180+
for (Row row : rows) {
181+
printRow(row);
182+
}
183+
} catch (IOException e) {
184+
System.out.println(
185+
"Unable to intailize service client, as a network error occured: \n" + e.toString());
186+
}
187+
}
188+
// [END bigtable_reads_row_ranges]
189+
190+
// [START bigtable_reads_prefix]
191+
public static void readPrefix() {
192+
// TODO(developer): Replace these variables before running the sample.
193+
String projectId = "my-project-id";
194+
String instanceId = "my-instance-id";
195+
String tableId = "mobile-time-series";
196+
readPrefix(projectId, instanceId, tableId);
197+
}
198+
199+
public static void readPrefix(String projectId, String instanceId, String tableId) {
200+
// Initialize client that will be used to send requests. This client only needs to be created
201+
// once, and can be reused for multiple requests. After completing all of your requests, call
202+
// the "close" method on the client to safely clean up any remaining background resources.
203+
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
204+
Query query = Query.create(tableId).prefix("phone");
205+
ServerStream<Row> rows = dataClient.readRows(query);
206+
for (Row row : rows) {
207+
printRow(row);
208+
}
209+
} catch (IOException e) {
210+
System.out.println(
211+
"Unable to intailize service client, as a network error occured: \n" + e.toString());
212+
}
213+
}
214+
// [END bigtable_reads_prefix]
215+
216+
// [START bigtable_reads_filter]
217+
public static void readFilter() {
218+
// TODO(developer): Replace these variables before running the sample.
219+
String projectId = "my-project-id";
220+
String instanceId = "my-instance-id";
221+
String tableId = "mobile-time-series";
222+
readFilter(projectId, instanceId, tableId);
223+
}
224+
225+
public static void readFilter(String projectId, String instanceId, String tableId) {
226+
Filters.Filter filter = FILTERS.value().regex("PQ2A.*");
227+
228+
// Initialize client that will be used to send requests. This client only needs to be created
229+
// once, and can be reused for multiple requests. After completing all of your requests, call
230+
// the "close" method on the client to safely clean up any remaining background resources.
231+
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
232+
Query query = Query.create(tableId).filter(filter);
233+
ServerStream<Row> rows = dataClient.readRows(query);
234+
for (Row row : rows) {
235+
printRow(row);
236+
}
237+
} catch (IOException e) {
238+
System.out.println(
239+
"Unable to intailize service client, as a network error occured: \n" + e.toString());
240+
}
241+
}
242+
// [END bigtable_reads_filter]
243+
244+
// [START bigtable_reads_row]
245+
// [START bigtable_reads_row_partial]
246+
// [START bigtable_reads_rows]
247+
// [START bigtable_reads_row_range]
248+
// [START bigtable_reads_row_ranges]
249+
// [START bigtable_reads_prefix]
250+
// [START bigtable_reads_filter]
251+
private static void printRow(Row row) {
252+
System.out.printf("Reading data for %s%n", row.getKey().toStringUtf8());
253+
String colFamily = "";
254+
for (RowCell cell : row.getCells()) {
255+
if (!cell.getFamily().equals(colFamily)) {
256+
colFamily = cell.getFamily();
257+
System.out.printf("Column Family %s%n", colFamily);
258+
}
259+
System.out.printf(
260+
"\t%s: %s @%s%n",
261+
cell.getQualifier().toStringUtf8(), cell.getValue().toStringUtf8(), cell.getTimestamp());
262+
}
263+
System.out.println();
264+
}
265+
}
266+
// [END bigtable_reads_row]
267+
// [END bigtable_reads_row_partial]
268+
// [END bigtable_reads_rows]
269+
// [END bigtable_reads_row_range]
270+
// [END bigtable_reads_row_ranges]
271+
// [END bigtable_reads_prefix]
272+
// [END bigtable_reads_filter]

bigtable/snippets/src/main/java/com/example/bigtable/WriteConditionally.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static void writeConditionally(String projectId, String instanceId, Strin
3636
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
3737
long timestamp = System.currentTimeMillis() * 1000;
3838

39-
String rowKey = "phone#4c410523#20190501";
39+
String rowkey = "phone#4c410523#20190501";
4040

4141
Mutation mutation =
4242
Mutation.create().setCell(COLUMN_FAMILY_NAME, "os_name", timestamp, "android");
@@ -49,7 +49,7 @@ public static void writeConditionally(String projectId, String instanceId, Strin
4949
.filter(FILTERS.value().regex("PQ2A\\..*"));
5050

5151
ConditionalRowMutation conditionalRowMutation =
52-
ConditionalRowMutation.create(tableId, rowKey).condition(filter).then(mutation);
52+
ConditionalRowMutation.create(tableId, rowkey).condition(filter).then(mutation);
5353

5454
boolean success = dataClient.checkAndMutateRow(conditionalRowMutation);
5555

bigtable/snippets/src/main/java/com/example/bigtable/WriteIncrement.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ public static void writeIncrement(String projectId, String instanceId, String ta
3434
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
3535
// Get an existing row that has a cell with an incrementable value. A value can be incremented
3636
// if it is encoded as a 64-bit big-endian signed integer.
37-
String rowKey = "phone#4c410523#20190501";
37+
String rowkey = "phone#4c410523#20190501";
3838
ReadModifyWriteRow mutation =
39-
ReadModifyWriteRow.create(tableId, rowKey)
39+
ReadModifyWriteRow.create(tableId, rowkey)
4040
.increment(COLUMN_FAMILY_NAME, "connected_cell", -1);
4141
Row success = dataClient.readModifyWriteRow(mutation);
4242

bigtable/snippets/src/main/java/com/example/bigtable/WriteSimple.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ public static void writeSimple(String projectId, String instanceId, String table
3333
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
3434
long timestamp = System.currentTimeMillis() * 1000;
3535

36-
String rowKey = "phone#4c410523#20190501";
36+
String rowkey = "phone#4c410523#20190501";
3737
ByteString one = ByteString.copyFrom(new byte[] {0, 0, 0, 0, 0, 0, 0, 1});
3838

3939
RowMutation rowMutation =
40-
RowMutation.create(tableId, rowKey)
40+
RowMutation.create(tableId, rowkey)
4141
.setCell(
4242
COLUMN_FAMILY_NAME,
4343
ByteString.copyFrom("connected_cell".getBytes()),
@@ -51,7 +51,7 @@ public static void writeSimple(String projectId, String instanceId, String table
5151
.setCell(COLUMN_FAMILY_NAME, "os_build", timestamp, "PQ2A.190405.003");
5252

5353
dataClient.mutateRow(rowMutation);
54-
System.out.printf("Successfully wrote row %s", rowKey);
54+
System.out.printf("Successfully wrote row %s", rowkey);
5555

5656
} catch (Exception e) {
5757
System.out.println("Error during WriteSimple: \n" + e.toString());

0 commit comments

Comments
 (0)