Skip to content

Commit

Permalink
samples: Update all examples to use TargetId-based data methods (#2189)
Browse files Browse the repository at this point in the history
* samples: Use TargetId-based data methods

Change-Id: I155f73e142169989b2e74c906456d97107462227

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

---------

Co-authored-by: Lixia Chen <[email protected]>
Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
3 people committed Mar 28, 2024
1 parent f29c5bb commit 2748eeb
Show file tree
Hide file tree
Showing 18 changed files with 73 additions and 45 deletions.
Expand Up @@ -26,6 +26,7 @@
import com.google.cloud.bigtable.data.v2.models.Query;
import com.google.cloud.bigtable.data.v2.models.Row;
import com.google.cloud.bigtable.data.v2.models.RowCell;
import com.google.cloud.bigtable.data.v2.models.TableId;
import java.io.IOException;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
Expand Down Expand Up @@ -360,7 +361,7 @@ private static void readFilter(
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
Query query = Query.create(tableId).filter(filter);
Query query = Query.create(TableId.of(tableId)).filter(filter);
ServerStream<Row> rows = dataClient.readRows(query);
for (Row row : rows) {
printRow(row);
Expand Down
Expand Up @@ -32,6 +32,7 @@
import com.google.cloud.bigtable.data.v2.models.Row;
import com.google.cloud.bigtable.data.v2.models.RowCell;
import com.google.cloud.bigtable.data.v2.models.RowMutation;
import com.google.cloud.bigtable.data.v2.models.TableId;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
Expand Down Expand Up @@ -138,7 +139,7 @@ public void writeToTable() {
for (int i = 0; i < names.length; i++) {
String greeting = "Hello " + names[i] + "!";
RowMutation rowMutation =
RowMutation.create(tableId, ROW_KEY_PREFIX + i)
RowMutation.create(TableId.of(tableId), ROW_KEY_PREFIX + i)
.setCell(COLUMN_FAMILY, COLUMN_QUALIFIER_NAME, names[i])
.setCell(COLUMN_FAMILY, COLUMN_QUALIFIER_GREETING, greeting);
dataClient.mutateRow(rowMutation);
Expand Down Expand Up @@ -175,7 +176,7 @@ public List<RowCell> readSpecificCells() {
// [START bigtable_hw_get_by_key]
try {
System.out.println("\nReading specific cells by family and qualifier");
Row row = dataClient.readRow(tableId, ROW_KEY_PREFIX + 0);
Row row = dataClient.readRow(TableId.of(tableId), ROW_KEY_PREFIX + 0);
System.out.println("Row: " + row.getKey().toStringUtf8());
List<RowCell> cells = row.getCells(COLUMN_FAMILY, COLUMN_QUALIFIER_NAME);
for (RowCell cell : cells) {
Expand All @@ -196,7 +197,7 @@ public List<Row> readTable() {
// [START bigtable_hw_scan_all]
try {
System.out.println("\nReading the entire table");
Query query = Query.create(tableId);
Query query = Query.create(TableId.of(tableId));
ServerStream<Row> rowStream = dataClient.readRows(query);
List<Row> tableRows = new ArrayList<>();
for (Row r : rowStream) {
Expand Down Expand Up @@ -229,15 +230,15 @@ public void filterLimitCellsPerCol(String tableId) {
private void readRowFilter(String tableId, Filter filter) {
String rowKey =
Base64.getEncoder().encodeToString("greeting0".getBytes(StandardCharsets.UTF_8));
Row row = dataClient.readRow(tableId, rowKey, filter);
Row row = dataClient.readRow(TableId.of(tableId), rowKey, filter);
printRow(row);
System.out.println("Row filter completed.");
}
// [END bigtable_hw_get_with_filter]

// [START bigtable_hw_scan_with_filter]
private void readFilter(String tableId, Filter filter) {
Query query = Query.create(tableId).filter(filter);
Query query = Query.create(TableId.of(tableId)).filter(filter);
ServerStream<Row> rows = dataClient.readRows(query);
for (Row row : rows) {
printRow(row);
Expand Down
Expand Up @@ -21,6 +21,7 @@
import com.google.cloud.bigtable.data.v2.models.Query;
import com.google.cloud.bigtable.data.v2.models.Row;
import com.google.cloud.bigtable.data.v2.models.RowMutation;
import com.google.cloud.bigtable.data.v2.models.TableId;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -35,7 +36,7 @@ public static void writeSaltedRow(
BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId);
String saltedRowKey = getSaltedRowKey(rowKey, SALT_RANGE);
RowMutation rowMutation =
RowMutation.create(tableId, saltedRowKey)
RowMutation.create(TableId.of(tableId), saltedRowKey)
.setCell(COLUMN_FAMILY_NAME, "os_build", "PQ2A.190405.003");

dataClient.mutateRow(rowMutation);
Expand All @@ -47,7 +48,7 @@ public static void writeSaltedRow(
public static void readSaltedRow(
String projectId, String instanceId, String tableId, String rowKey) throws IOException {
BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId);
Row row = dataClient.readRow(tableId, getSaltedRowKey(rowKey, SALT_RANGE));
Row row = dataClient.readRow(TableId.of(tableId), getSaltedRowKey(rowKey, SALT_RANGE));
System.out.printf("Successfully read row %s\n", row.getKey().toStringUtf8());
}

Expand All @@ -58,7 +59,7 @@ public static void scanSaltedRows(

List<Query> queries = new ArrayList<>();
for (int i = 0; i < SALT_RANGE; i++) {
queries.add(Query.create(tableId).prefix(i + "-" + prefix));
queries.add(Query.create(TableId.of(tableId)).prefix(i + "-" + prefix));
}

List<ApiFuture<List<Row>>> futures = new ArrayList<>();
Expand Down
Expand Up @@ -23,6 +23,7 @@
import com.google.cloud.bigtable.data.v2.BigtableDataSettings;
import com.google.cloud.bigtable.data.v2.models.Row;
import com.google.cloud.bigtable.data.v2.models.RowCell;
import com.google.cloud.bigtable.data.v2.models.TableId;

public class Quickstart {

Expand All @@ -43,7 +44,7 @@ public static void quickstart(String projectId, String instanceId, String tableI
// the "close" method on the client to safely clean up any remaining background resources.
try (BigtableDataClient dataClient = BigtableDataClient.create(settings)) {
System.out.println("\nReading a single row by row key");
Row row = dataClient.readRow(tableId, "r1");
Row row = dataClient.readRow(TableId.of(tableId), "r1");
System.out.println("Row: " + row.getKey().toStringUtf8());
for (RowCell cell : row.getCells()) {
System.out.printf(
Expand Down
19 changes: 11 additions & 8 deletions samples/snippets/src/main/java/com/example/bigtable/Reads.java
Expand Up @@ -26,6 +26,7 @@
import com.google.cloud.bigtable.data.v2.models.Query;
import com.google.cloud.bigtable.data.v2.models.Row;
import com.google.cloud.bigtable.data.v2.models.RowCell;
import com.google.cloud.bigtable.data.v2.models.TableId;
import java.io.IOException;

public class Reads {
Expand All @@ -48,7 +49,7 @@ public static void readRow(String projectId, String instanceId, String tableId)
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
String rowkey = "phone#4c410523#20190501";

Row row = dataClient.readRow(tableId, rowkey);
Row row = dataClient.readRow(TableId.of(tableId), rowkey);
printRow(row);

} catch (IOException e) {
Expand Down Expand Up @@ -79,7 +80,7 @@ public static void readRowPartial(String projectId, String instanceId, String ta
.filter(FILTERS.family().exactMatch("stats_summary"))
.filter(FILTERS.qualifier().exactMatch("os_build"));

Row row = dataClient.readRow(tableId, rowkey, filter);
Row row = dataClient.readRow(TableId.of(tableId), rowkey, filter);
printRow(row);

} catch (IOException e) {
Expand All @@ -104,7 +105,9 @@ public static void readRows(String projectId, String instanceId, String tableId)
// the "close" method on the client to safely clean up any remaining background resources.
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
Query query =
Query.create(tableId).rowKey("phone#4c410523#20190501").rowKey("phone#4c410523#20190502");
Query.create(TableId.of(tableId))
.rowKey("phone#4c410523#20190501")
.rowKey("phone#4c410523#20190502");
ServerStream<Row> rows = dataClient.readRows(query);
for (Row row : rows) {
printRow(row);
Expand Down Expand Up @@ -133,7 +136,7 @@ public static void readRowRange(String projectId, String instanceId, String tabl
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
Query query = Query.create(tableId).range(start, end);
Query query = Query.create(TableId.of(tableId)).range(start, end);
ServerStream<Row> rows = dataClient.readRows(query);
for (Row row : rows) {
printRow(row);
Expand All @@ -160,7 +163,7 @@ public static void readRowRanges(String projectId, String instanceId, String tab
// the "close" method on the client to safely clean up any remaining background resources.
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
Query query =
Query.create(tableId)
Query.create(TableId.of(tableId))
.range("phone#4c410523#20190501", "phone#4c410523#20190601")
.range("phone#5c10102#20190501", "phone#5c10102#20190601");
ServerStream<Row> rows = dataClient.readRows(query);
Expand Down Expand Up @@ -188,7 +191,7 @@ public static void readPrefix(String projectId, String instanceId, String tableI
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
Query query = Query.create(tableId).prefix("phone");
Query query = Query.create(TableId.of(tableId)).prefix("phone");
ServerStream<Row> rows = dataClient.readRows(query);
for (Row row : rows) {
printRow(row);
Expand All @@ -215,7 +218,7 @@ public static void readRowsReversed(String projectId, String instanceId, String
// the "close" method on the client to safely clean up any remaining background resources.
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
Query query =
Query.create(tableId)
Query.create(TableId.of(tableId))
.reversed(true)
.limit(3)
.prefix("phone#4c410523")
Expand Down Expand Up @@ -247,7 +250,7 @@ public static void readFilter(String projectId, String instanceId, String tableI
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
Query query = Query.create(tableId).filter(filter);
Query query = Query.create(TableId.of(tableId)).filter(filter);
ServerStream<Row> rows = dataClient.readRows(query);
for (Row row : rows) {
printRow(row);
Expand Down
Expand Up @@ -23,6 +23,7 @@
import com.google.api.gax.batching.BatchingException;
import com.google.cloud.bigtable.data.v2.BigtableDataClient;
import com.google.cloud.bigtable.data.v2.models.RowMutationEntry;
import com.google.cloud.bigtable.data.v2.models.TableId;
import com.google.protobuf.ByteString;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -38,7 +39,8 @@ public static void writeBatch(String projectId, String instanceId, String tableI

try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
List<ApiFuture<Void>> batchFutures = new ArrayList<>();
try (Batcher<RowMutationEntry, Void> batcher = dataClient.newBulkMutationBatcher(tableId)) {
try (Batcher<RowMutationEntry, Void> batcher =
dataClient.newBulkMutationBatcher(TableId.of(tableId))) {
long timestamp = System.currentTimeMillis() * 1000;
batchFutures.add(
batcher.add(
Expand Down
Expand Up @@ -24,6 +24,7 @@
import com.google.cloud.bigtable.data.v2.models.ConditionalRowMutation;
import com.google.cloud.bigtable.data.v2.models.Filters.Filter;
import com.google.cloud.bigtable.data.v2.models.Mutation;
import com.google.cloud.bigtable.data.v2.models.TableId;

public class WriteConditionally {
private static final String COLUMN_FAMILY_NAME = "stats_summary";
Expand All @@ -49,7 +50,9 @@ public static void writeConditionally(String projectId, String instanceId, Strin
.filter(FILTERS.value().regex("PQ2A\\..*"));

ConditionalRowMutation conditionalRowMutation =
ConditionalRowMutation.create(tableId, rowkey).condition(filter).then(mutation);
ConditionalRowMutation.create(TableId.of(tableId), rowkey)
.condition(filter)
.then(mutation);

boolean success = dataClient.checkAndMutateRow(conditionalRowMutation);

Expand Down
Expand Up @@ -21,6 +21,7 @@
import com.google.cloud.bigtable.data.v2.BigtableDataClient;
import com.google.cloud.bigtable.data.v2.models.ReadModifyWriteRow;
import com.google.cloud.bigtable.data.v2.models.Row;
import com.google.cloud.bigtable.data.v2.models.TableId;
import java.nio.charset.Charset;

public class WriteIncrement {
Expand All @@ -36,7 +37,7 @@ public static void writeIncrement(String projectId, String instanceId, String ta
// if it is encoded as a 64-bit big-endian signed integer.
String rowkey = "phone#4c410523#20190501";
ReadModifyWriteRow mutation =
ReadModifyWriteRow.create(tableId, rowkey)
ReadModifyWriteRow.create(TableId.of(tableId), rowkey)
.increment(COLUMN_FAMILY_NAME, "connected_cell", -1);
Row success = dataClient.readModifyWriteRow(mutation);

Expand Down
Expand Up @@ -20,6 +20,7 @@

import com.google.cloud.bigtable.data.v2.BigtableDataClient;
import com.google.cloud.bigtable.data.v2.models.RowMutation;
import com.google.cloud.bigtable.data.v2.models.TableId;
import com.google.protobuf.ByteString;

public class WriteSimple {
Expand All @@ -36,7 +37,7 @@ public static void writeSimple(String projectId, String instanceId, String table
String rowkey = "phone#4c410523#20190501";

RowMutation rowMutation =
RowMutation.create(tableId, rowkey)
RowMutation.create(TableId.of(tableId), rowkey)
.setCell(
COLUMN_FAMILY_NAME,
ByteString.copyFrom("connected_cell".getBytes()),
Expand Down
Expand Up @@ -23,14 +23,16 @@
import com.google.cloud.bigtable.data.v2.models.Query;
import com.google.cloud.bigtable.data.v2.models.Row;
import com.google.cloud.bigtable.data.v2.models.RowMutationEntry;
import com.google.cloud.bigtable.data.v2.models.TableId;
import java.io.IOException;

public class BatchDeleteExample {
public void batchDelete(String projectId, String instanceId, String tableId)
throws InterruptedException, IOException {
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
try (Batcher<RowMutationEntry, Void> batcher = dataClient.newBulkMutationBatcher(tableId)) {
ServerStream<Row> rows = dataClient.readRows(Query.create(tableId));
try (Batcher<RowMutationEntry, Void> batcher =
dataClient.newBulkMutationBatcher(TableId.of(tableId))) {
ServerStream<Row> rows = dataClient.readRows(Query.create(TableId.of(tableId)));
for (Row row : rows) {
batcher.add(
RowMutationEntry.create(row.getKey()).deleteCells("cell_plan", "data_plan_05gb"));
Expand Down
Expand Up @@ -21,6 +21,7 @@
import com.google.cloud.bigtable.data.v2.models.ConditionalRowMutation;
import com.google.cloud.bigtable.data.v2.models.Filters;
import com.google.cloud.bigtable.data.v2.models.Mutation;
import com.google.cloud.bigtable.data.v2.models.TableId;
import java.io.IOException;

public class ConditionalDeleteExample {
Expand All @@ -30,7 +31,7 @@ public void conditionalDelete(String projectId, String instanceId, String tableI
Filters.Filter condition = Filters.FILTERS.value().exactMatch("PQ2A.190405.004");
Mutation mutation = Mutation.create().deleteCells("stats_summary", "os_build");
dataClient.checkAndMutateRow(
ConditionalRowMutation.create(tableId, "phone#4c410523#20190502")
ConditionalRowMutation.create(TableId.of(tableId), "phone#4c410523#20190502")
.condition(condition)
.then(mutation));
}
Expand Down
Expand Up @@ -20,14 +20,16 @@
import com.google.cloud.bigtable.data.v2.BigtableDataClient;
import com.google.cloud.bigtable.data.v2.models.Mutation;
import com.google.cloud.bigtable.data.v2.models.RowMutation;
import com.google.cloud.bigtable.data.v2.models.TableId;
import java.io.IOException;

public class DeleteFromColumnExample {
public void deleteFromColumnCells(String projectId, String instanceId, String tableId)
throws IOException {
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
Mutation mutation = Mutation.create().deleteCells("cell_plan", "data_plan_01gb");
dataClient.mutateRow(RowMutation.create(tableId, "phone#4c410523#20190501", mutation));
dataClient.mutateRow(
RowMutation.create(TableId.of(tableId), "phone#4c410523#20190501", mutation));
}
}
}
Expand Down
Expand Up @@ -19,14 +19,16 @@
// [START bigtable_delete_from_column_family]
import com.google.cloud.bigtable.data.v2.BigtableDataClient;
import com.google.cloud.bigtable.data.v2.models.RowMutation;
import com.google.cloud.bigtable.data.v2.models.TableId;
import java.io.IOException;

public class DeleteFromColumnFamilyExample {
public void deleteFromColumnFamily(String projectId, String instanceId, String tableId)
throws IOException {
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
dataClient.mutateRow(
RowMutation.create(tableId, "phone#5c10102#20190501").deleteFamily("stats_summary"));
RowMutation.create(TableId.of(tableId), "phone#5c10102#20190501")
.deleteFamily("stats_summary"));
}
}
}
Expand Down
Expand Up @@ -20,14 +20,16 @@
import com.google.cloud.bigtable.data.v2.BigtableDataClient;
import com.google.cloud.bigtable.data.v2.models.Mutation;
import com.google.cloud.bigtable.data.v2.models.RowMutation;
import com.google.cloud.bigtable.data.v2.models.TableId;
import java.io.IOException;

public class DeleteFromRowExample {
public void deleteFromRow(String projectId, String instanceId, String tableId)
throws IOException {
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
Mutation mutation = Mutation.create().deleteRow();
dataClient.mutateRow(RowMutation.create(tableId, "phone#4c410523#20190501", mutation));
dataClient.mutateRow(
RowMutation.create(TableId.of(tableId), "phone#4c410523#20190501", mutation));
}
}
}
Expand Down
Expand Up @@ -26,6 +26,7 @@
import com.google.cloud.bigtable.admin.v2.models.CreateTableRequest;
import com.google.cloud.bigtable.data.v2.BigtableDataClient;
import com.google.cloud.bigtable.data.v2.BigtableDataSettings;
import com.google.cloud.bigtable.data.v2.models.TableId;
import java.io.IOException;
import java.util.Random;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -99,9 +100,9 @@ public void testCreateAndDeleteTable() throws IOException {
@Test
public void testWriteToTable() {
// Writes to a table.
assertNull(dataClient.readRow(tableId, "rowKey0"));
assertNull(dataClient.readRow(TableId.of(tableId), "rowKey0"));
helloWorld.writeToTable();
assertNotNull(dataClient.readRow(tableId, "rowKey0"));
assertNotNull(dataClient.readRow(TableId.of(tableId), "rowKey0"));
}

@Test
Expand Down

0 comments on commit 2748eeb

Please sign in to comment.