1717package com .example .bigtable ;
1818
1919// [START bigtable_hw_imports]
20+
21+ import static com .google .cloud .bigtable .data .v2 .models .Filters .FILTERS ;
22+
2023import com .google .api .gax .rpc .NotFoundException ;
2124import com .google .api .gax .rpc .ServerStream ;
2225import com .google .cloud .bigtable .admin .v2 .BigtableTableAdminClient ;
2326import com .google .cloud .bigtable .admin .v2 .BigtableTableAdminSettings ;
2427import com .google .cloud .bigtable .admin .v2 .models .CreateTableRequest ;
2528import com .google .cloud .bigtable .data .v2 .BigtableDataClient ;
2629import com .google .cloud .bigtable .data .v2 .BigtableDataSettings ;
30+ import com .google .cloud .bigtable .data .v2 .models .Filters .Filter ;
2731import com .google .cloud .bigtable .data .v2 .models .Query ;
2832import com .google .cloud .bigtable .data .v2 .models .Row ;
2933import com .google .cloud .bigtable .data .v2 .models .RowCell ;
3034import com .google .cloud .bigtable .data .v2 .models .RowMutation ;
3135import java .io .IOException ;
36+ import java .nio .charset .StandardCharsets ;
3237import java .util .ArrayList ;
38+ import java .util .Base64 ;
3339import java .util .List ;
3440
3541// [END bigtable_hw_imports]
@@ -53,6 +59,8 @@ public class HelloWorld {
5359 private static final String COLUMN_QUALIFIER_GREETING = "greeting" ;
5460 private static final String COLUMN_QUALIFIER_NAME = "name" ;
5561 private static final String ROW_KEY_PREFIX = "rowKey" ;
62+ private final String projectId ;
63+ private final String instanceId ;
5664 private final String tableId ;
5765 private final BigtableDataClient dataClient ;
5866 private final BigtableTableAdminClient adminClient ;
@@ -72,6 +80,8 @@ public static void main(String[] args) throws Exception {
7280
7381 public HelloWorld (String projectId , String instanceId , String tableId ) throws IOException {
7482 this .tableId = tableId ;
83+ this .projectId = projectId ;
84+ this .instanceId = instanceId ;
7585
7686 // [START bigtable_hw_connect]
7787 // Creates the settings to configure a bigtable data client.
@@ -99,6 +109,7 @@ public void run() throws Exception {
99109 readSingleRow ();
100110 readSpecificCells ();
101111 readTable ();
112+ filterLimitCellsPerCol (this .projectId , this .instanceId , tableId );
102113 deleteTable ();
103114 close ();
104115 }
@@ -209,6 +220,52 @@ public List<Row> readTable() {
209220 // [END bigtable_hw_scan_all]
210221 }
211222
223+ // [START bigtable_hw_create_filter]
224+ public static void filterLimitCellsPerCol (String projectId , String instanceId , String tableId ) {
225+ // A filter that matches only the most recent cell within each column
226+ Filter filter = FILTERS .limit ().cellsPerColumn (1 );
227+ readRowFilter (projectId , instanceId , tableId , filter );
228+ readFilter (projectId , instanceId , tableId , filter );
229+ }
230+ // [END bigtable_hw_create_filter]
231+
232+ // [START bigtable_hw_get_with_filter]
233+ private static void readRowFilter (
234+ String projectId , String instanceId , String tableId , Filter filter ) {
235+ // Initialize client that will be used to send requests. This client only needs to be created
236+ // once, and can be reused for multiple requests.
237+ try (BigtableDataClient dataClient = BigtableDataClient .create (projectId , instanceId )) {
238+ String rowKey =
239+ Base64 .getEncoder ().encodeToString ("greeting0" .getBytes (StandardCharsets .UTF_8 ));
240+ Row row = dataClient .readRow (tableId , rowKey , filter );
241+ printRow (row );
242+ System .out .println ("Row filter completed." );
243+ } catch (IOException e ) {
244+ System .out .println (
245+ "Unable to initialize service client, as a network error occurred: \n " + e );
246+ }
247+ }
248+ // [END bigtable_hw_get_with_filter]
249+
250+ // [START bigtable_hw_scan_with_filter]
251+ private static void readFilter (
252+ String projectId , String instanceId , String tableId , Filter filter ) {
253+ // Initialize client that will be used to send requests. This client only needs to be created
254+ // once, and can be reused for multiple requests.
255+ try (BigtableDataClient dataClient = BigtableDataClient .create (projectId , instanceId )) {
256+ Query query = Query .create (tableId ).filter (filter );
257+ ServerStream <Row > rows = dataClient .readRows (query );
258+ for (Row row : rows ) {
259+ printRow (row );
260+ }
261+ System .out .println ("Table filter completed." );
262+ } catch (IOException e ) {
263+ System .out .println (
264+ "Unable to initialize service client, as a network error occurred: \n " + e );
265+ }
266+ }
267+ // [END bigtable_hw_scan_with_filter]
268+
212269 /** Demonstrates how to delete a table. */
213270 public void deleteTable () {
214271 // [START bigtable_hw_delete_table]
@@ -221,4 +278,29 @@ public void deleteTable() {
221278 }
222279 // [END bigtable_hw_delete_table]
223280 }
281+
282+ // [START bigtable_print_row]
283+ private static void printRow (Row row ) {
284+ if (row == null ) {
285+ return ;
286+ }
287+ System .out .printf ("Reading data for %s%n" , row .getKey ().toStringUtf8 ());
288+ String colFamily = "" ;
289+ for (RowCell cell : row .getCells ()) {
290+ if (!cell .getFamily ().equals (colFamily )) {
291+ colFamily = cell .getFamily ();
292+ System .out .printf ("Column Family %s%n" , colFamily );
293+ }
294+ String labels =
295+ cell .getLabels ().size () == 0 ? "" : " [" + String .join ("," , cell .getLabels ()) + "]" ;
296+ System .out .printf (
297+ "\t %s: %s @%s%s%n" ,
298+ cell .getQualifier ().toStringUtf8 (),
299+ cell .getValue ().toStringUtf8 (),
300+ cell .getTimestamp (),
301+ labels );
302+ }
303+ System .out .println ();
304+ }
305+ // [END bigtable_print_row]
224306}
0 commit comments