Skip to content

Commit

Permalink
docs(samples): remove client initialization as the snippets are not u…
Browse files Browse the repository at this point in the history
…sed standalone (#1768)
  • Loading branch information
Sita04 committed May 29, 2023
1 parent 9f249be commit a6ac97c
Showing 1 changed file with 16 additions and 36 deletions.
52 changes: 16 additions & 36 deletions samples/snippets/src/main/java/com/example/bigtable/HelloWorld.java
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -109,7 +105,7 @@ public void run() throws Exception {
readSingleRow();
readSpecificCells();
readTable();
filterLimitCellsPerCol(this.projectId, this.instanceId, tableId);
filterLimitCellsPerCol(tableId);
deleteTable();
close();
}
Expand Down Expand Up @@ -221,48 +217,32 @@ public List<Row> 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<Row> 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<Row> rows = dataClient.readRows(query);
for (Row row : rows) {
printRow(row);
}
System.out.println("Table filter completed.");
}
// [END bigtable_hw_scan_with_filter]

Expand Down

0 comments on commit a6ac97c

Please sign in to comment.