From a6ac97c08a851977a76e82fddb16690cff17a1fe Mon Sep 17 00:00:00 2001 From: Sita Lakshmi Sangameswaran Date: Mon, 29 May 2023 21:56:08 +0530 Subject: [PATCH] docs(samples): remove client initialization as the snippets are not used standalone (#1768) --- .../java/com/example/bigtable/HelloWorld.java | 52 ++++++------------- 1 file changed, 16 insertions(+), 36 deletions(-) diff --git a/samples/snippets/src/main/java/com/example/bigtable/HelloWorld.java b/samples/snippets/src/main/java/com/example/bigtable/HelloWorld.java index f588a8abaa..724985ce22 100644 --- a/samples/snippets/src/main/java/com/example/bigtable/HelloWorld.java +++ b/samples/snippets/src/main/java/com/example/bigtable/HelloWorld.java @@ -59,8 +59,6 @@ public class HelloWorld { private static final String COLUMN_QUALIFIER_GREETING = "greeting"; private static final String COLUMN_QUALIFIER_NAME = "name"; private static final String ROW_KEY_PREFIX = "rowKey"; - private final String projectId; - private final String instanceId; private final String tableId; private final BigtableDataClient dataClient; private final BigtableTableAdminClient adminClient; @@ -80,8 +78,6 @@ public static void main(String[] args) throws Exception { public HelloWorld(String projectId, String instanceId, String tableId) throws IOException { this.tableId = tableId; - this.projectId = projectId; - this.instanceId = instanceId; // [START bigtable_hw_connect] // Creates the settings to configure a bigtable data client. @@ -109,7 +105,7 @@ public void run() throws Exception { readSingleRow(); readSpecificCells(); readTable(); - filterLimitCellsPerCol(this.projectId, this.instanceId, tableId); + filterLimitCellsPerCol(tableId); deleteTable(); close(); } @@ -221,48 +217,32 @@ public List readTable() { } // [START bigtable_hw_create_filter] - public static void filterLimitCellsPerCol(String projectId, String instanceId, String tableId) { + public void filterLimitCellsPerCol(String tableId) { // A filter that matches only the most recent cell within each column Filter filter = FILTERS.limit().cellsPerColumn(1); - readRowFilter(projectId, instanceId, tableId, filter); - readFilter(projectId, instanceId, tableId, filter); + readRowFilter(tableId, filter); + readFilter(tableId, filter); } // [END bigtable_hw_create_filter] // [START bigtable_hw_get_with_filter] - private static void readRowFilter( - String projectId, String instanceId, String tableId, Filter filter) { - // Initialize client that will be used to send requests. This client only needs to be created - // once, and can be reused for multiple requests. - try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) { - String rowKey = - Base64.getEncoder().encodeToString("greeting0".getBytes(StandardCharsets.UTF_8)); - Row row = dataClient.readRow(tableId, rowKey, filter); - printRow(row); - System.out.println("Row filter completed."); - } catch (IOException e) { - System.out.println( - "Unable to initialize service client, as a network error occurred: \n" + e); - } + private void readRowFilter(String tableId, Filter filter) { + String rowKey = + Base64.getEncoder().encodeToString("greeting0".getBytes(StandardCharsets.UTF_8)); + Row row = dataClient.readRow(tableId, rowKey, filter); + printRow(row); + System.out.println("Row filter completed."); } // [END bigtable_hw_get_with_filter] // [START bigtable_hw_scan_with_filter] - private static void readFilter( - String projectId, String instanceId, String tableId, Filter filter) { - // Initialize client that will be used to send requests. This client only needs to be created - // once, and can be reused for multiple requests. - try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) { - Query query = Query.create(tableId).filter(filter); - ServerStream rows = dataClient.readRows(query); - for (Row row : rows) { - printRow(row); - } - System.out.println("Table filter completed."); - } catch (IOException e) { - System.out.println( - "Unable to initialize service client, as a network error occurred: \n" + e); + private void readFilter(String tableId, Filter filter) { + Query query = Query.create(tableId).filter(filter); + ServerStream rows = dataClient.readRows(query); + for (Row row : rows) { + printRow(row); } + System.out.println("Table filter completed."); } // [END bigtable_hw_scan_with_filter]