Skip to content

Commit 2748eeb

Browse files
trollyxiaLixia Chengcf-owl-bot[bot]
authored
samples: Update all examples to use TargetId-based data methods (#2189)
* 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>
1 parent f29c5bb commit 2748eeb

18 files changed

+73
-45
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.google.cloud.bigtable.data.v2.models.Query;
2727
import com.google.cloud.bigtable.data.v2.models.Row;
2828
import com.google.cloud.bigtable.data.v2.models.RowCell;
29+
import com.google.cloud.bigtable.data.v2.models.TableId;
2930
import java.io.IOException;
3031
import java.time.Instant;
3132
import java.time.temporal.ChronoUnit;
@@ -360,7 +361,7 @@ private static void readFilter(
360361
// once, and can be reused for multiple requests. After completing all of your requests, call
361362
// the "close" method on the client to safely clean up any remaining background resources.
362363
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
363-
Query query = Query.create(tableId).filter(filter);
364+
Query query = Query.create(TableId.of(tableId)).filter(filter);
364365
ServerStream<Row> rows = dataClient.readRows(query);
365366
for (Row row : rows) {
366367
printRow(row);

samples/snippets/src/main/java/com/example/bigtable/HelloWorld.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.google.cloud.bigtable.data.v2.models.Row;
3333
import com.google.cloud.bigtable.data.v2.models.RowCell;
3434
import com.google.cloud.bigtable.data.v2.models.RowMutation;
35+
import com.google.cloud.bigtable.data.v2.models.TableId;
3536
import java.io.IOException;
3637
import java.nio.charset.StandardCharsets;
3738
import java.util.ArrayList;
@@ -138,7 +139,7 @@ public void writeToTable() {
138139
for (int i = 0; i < names.length; i++) {
139140
String greeting = "Hello " + names[i] + "!";
140141
RowMutation rowMutation =
141-
RowMutation.create(tableId, ROW_KEY_PREFIX + i)
142+
RowMutation.create(TableId.of(tableId), ROW_KEY_PREFIX + i)
142143
.setCell(COLUMN_FAMILY, COLUMN_QUALIFIER_NAME, names[i])
143144
.setCell(COLUMN_FAMILY, COLUMN_QUALIFIER_GREETING, greeting);
144145
dataClient.mutateRow(rowMutation);
@@ -175,7 +176,7 @@ public List<RowCell> readSpecificCells() {
175176
// [START bigtable_hw_get_by_key]
176177
try {
177178
System.out.println("\nReading specific cells by family and qualifier");
178-
Row row = dataClient.readRow(tableId, ROW_KEY_PREFIX + 0);
179+
Row row = dataClient.readRow(TableId.of(tableId), ROW_KEY_PREFIX + 0);
179180
System.out.println("Row: " + row.getKey().toStringUtf8());
180181
List<RowCell> cells = row.getCells(COLUMN_FAMILY, COLUMN_QUALIFIER_NAME);
181182
for (RowCell cell : cells) {
@@ -196,7 +197,7 @@ public List<Row> readTable() {
196197
// [START bigtable_hw_scan_all]
197198
try {
198199
System.out.println("\nReading the entire table");
199-
Query query = Query.create(tableId);
200+
Query query = Query.create(TableId.of(tableId));
200201
ServerStream<Row> rowStream = dataClient.readRows(query);
201202
List<Row> tableRows = new ArrayList<>();
202203
for (Row r : rowStream) {
@@ -229,15 +230,15 @@ public void filterLimitCellsPerCol(String tableId) {
229230
private void readRowFilter(String tableId, Filter filter) {
230231
String rowKey =
231232
Base64.getEncoder().encodeToString("greeting0".getBytes(StandardCharsets.UTF_8));
232-
Row row = dataClient.readRow(tableId, rowKey, filter);
233+
Row row = dataClient.readRow(TableId.of(tableId), rowKey, filter);
233234
printRow(row);
234235
System.out.println("Row filter completed.");
235236
}
236237
// [END bigtable_hw_get_with_filter]
237238

238239
// [START bigtable_hw_scan_with_filter]
239240
private void readFilter(String tableId, Filter filter) {
240-
Query query = Query.create(tableId).filter(filter);
241+
Query query = Query.create(TableId.of(tableId)).filter(filter);
241242
ServerStream<Row> rows = dataClient.readRows(query);
242243
for (Row row : rows) {
243244
printRow(row);

samples/snippets/src/main/java/com/example/bigtable/KeySalting.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.google.cloud.bigtable.data.v2.models.Query;
2222
import com.google.cloud.bigtable.data.v2.models.Row;
2323
import com.google.cloud.bigtable.data.v2.models.RowMutation;
24+
import com.google.cloud.bigtable.data.v2.models.TableId;
2425
import java.io.IOException;
2526
import java.util.ArrayList;
2627
import java.util.List;
@@ -35,7 +36,7 @@ public static void writeSaltedRow(
3536
BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId);
3637
String saltedRowKey = getSaltedRowKey(rowKey, SALT_RANGE);
3738
RowMutation rowMutation =
38-
RowMutation.create(tableId, saltedRowKey)
39+
RowMutation.create(TableId.of(tableId), saltedRowKey)
3940
.setCell(COLUMN_FAMILY_NAME, "os_build", "PQ2A.190405.003");
4041

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

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

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

6465
List<ApiFuture<List<Row>>> futures = new ArrayList<>();

samples/snippets/src/main/java/com/example/bigtable/Quickstart.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.google.cloud.bigtable.data.v2.BigtableDataSettings;
2424
import com.google.cloud.bigtable.data.v2.models.Row;
2525
import com.google.cloud.bigtable.data.v2.models.RowCell;
26+
import com.google.cloud.bigtable.data.v2.models.TableId;
2627

2728
public class Quickstart {
2829

@@ -43,7 +44,7 @@ public static void quickstart(String projectId, String instanceId, String tableI
4344
// the "close" method on the client to safely clean up any remaining background resources.
4445
try (BigtableDataClient dataClient = BigtableDataClient.create(settings)) {
4546
System.out.println("\nReading a single row by row key");
46-
Row row = dataClient.readRow(tableId, "r1");
47+
Row row = dataClient.readRow(TableId.of(tableId), "r1");
4748
System.out.println("Row: " + row.getKey().toStringUtf8());
4849
for (RowCell cell : row.getCells()) {
4950
System.out.printf(

samples/snippets/src/main/java/com/example/bigtable/Reads.java

+11-8
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.google.cloud.bigtable.data.v2.models.Query;
2727
import com.google.cloud.bigtable.data.v2.models.Row;
2828
import com.google.cloud.bigtable.data.v2.models.RowCell;
29+
import com.google.cloud.bigtable.data.v2.models.TableId;
2930
import java.io.IOException;
3031

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

51-
Row row = dataClient.readRow(tableId, rowkey);
52+
Row row = dataClient.readRow(TableId.of(tableId), rowkey);
5253
printRow(row);
5354

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

82-
Row row = dataClient.readRow(tableId, rowkey, filter);
83+
Row row = dataClient.readRow(TableId.of(tableId), rowkey, filter);
8384
printRow(row);
8485

8586
} catch (IOException e) {
@@ -104,7 +105,9 @@ public static void readRows(String projectId, String instanceId, String tableId)
104105
// the "close" method on the client to safely clean up any remaining background resources.
105106
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
106107
Query query =
107-
Query.create(tableId).rowKey("phone#4c410523#20190501").rowKey("phone#4c410523#20190502");
108+
Query.create(TableId.of(tableId))
109+
.rowKey("phone#4c410523#20190501")
110+
.rowKey("phone#4c410523#20190502");
108111
ServerStream<Row> rows = dataClient.readRows(query);
109112
for (Row row : rows) {
110113
printRow(row);
@@ -133,7 +136,7 @@ public static void readRowRange(String projectId, String instanceId, String tabl
133136
// once, and can be reused for multiple requests. After completing all of your requests, call
134137
// the "close" method on the client to safely clean up any remaining background resources.
135138
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
136-
Query query = Query.create(tableId).range(start, end);
139+
Query query = Query.create(TableId.of(tableId)).range(start, end);
137140
ServerStream<Row> rows = dataClient.readRows(query);
138141
for (Row row : rows) {
139142
printRow(row);
@@ -160,7 +163,7 @@ public static void readRowRanges(String projectId, String instanceId, String tab
160163
// the "close" method on the client to safely clean up any remaining background resources.
161164
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
162165
Query query =
163-
Query.create(tableId)
166+
Query.create(TableId.of(tableId))
164167
.range("phone#4c410523#20190501", "phone#4c410523#20190601")
165168
.range("phone#5c10102#20190501", "phone#5c10102#20190601");
166169
ServerStream<Row> rows = dataClient.readRows(query);
@@ -188,7 +191,7 @@ public static void readPrefix(String projectId, String instanceId, String tableI
188191
// once, and can be reused for multiple requests. After completing all of your requests, call
189192
// the "close" method on the client to safely clean up any remaining background resources.
190193
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
191-
Query query = Query.create(tableId).prefix("phone");
194+
Query query = Query.create(TableId.of(tableId)).prefix("phone");
192195
ServerStream<Row> rows = dataClient.readRows(query);
193196
for (Row row : rows) {
194197
printRow(row);
@@ -215,7 +218,7 @@ public static void readRowsReversed(String projectId, String instanceId, String
215218
// the "close" method on the client to safely clean up any remaining background resources.
216219
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
217220
Query query =
218-
Query.create(tableId)
221+
Query.create(TableId.of(tableId))
219222
.reversed(true)
220223
.limit(3)
221224
.prefix("phone#4c410523")
@@ -247,7 +250,7 @@ public static void readFilter(String projectId, String instanceId, String tableI
247250
// once, and can be reused for multiple requests. After completing all of your requests, call
248251
// the "close" method on the client to safely clean up any remaining background resources.
249252
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
250-
Query query = Query.create(tableId).filter(filter);
253+
Query query = Query.create(TableId.of(tableId)).filter(filter);
251254
ServerStream<Row> rows = dataClient.readRows(query);
252255
for (Row row : rows) {
253256
printRow(row);

samples/snippets/src/main/java/com/example/bigtable/WriteBatch.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.google.api.gax.batching.BatchingException;
2424
import com.google.cloud.bigtable.data.v2.BigtableDataClient;
2525
import com.google.cloud.bigtable.data.v2.models.RowMutationEntry;
26+
import com.google.cloud.bigtable.data.v2.models.TableId;
2627
import com.google.protobuf.ByteString;
2728
import java.util.ArrayList;
2829
import java.util.List;
@@ -38,7 +39,8 @@ public static void writeBatch(String projectId, String instanceId, String tableI
3839

3940
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
4041
List<ApiFuture<Void>> batchFutures = new ArrayList<>();
41-
try (Batcher<RowMutationEntry, Void> batcher = dataClient.newBulkMutationBatcher(tableId)) {
42+
try (Batcher<RowMutationEntry, Void> batcher =
43+
dataClient.newBulkMutationBatcher(TableId.of(tableId))) {
4244
long timestamp = System.currentTimeMillis() * 1000;
4345
batchFutures.add(
4446
batcher.add(

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.google.cloud.bigtable.data.v2.models.ConditionalRowMutation;
2525
import com.google.cloud.bigtable.data.v2.models.Filters.Filter;
2626
import com.google.cloud.bigtable.data.v2.models.Mutation;
27+
import com.google.cloud.bigtable.data.v2.models.TableId;
2728

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

5152
ConditionalRowMutation conditionalRowMutation =
52-
ConditionalRowMutation.create(tableId, rowkey).condition(filter).then(mutation);
53+
ConditionalRowMutation.create(TableId.of(tableId), rowkey)
54+
.condition(filter)
55+
.then(mutation);
5356

5457
boolean success = dataClient.checkAndMutateRow(conditionalRowMutation);
5558

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.google.cloud.bigtable.data.v2.BigtableDataClient;
2222
import com.google.cloud.bigtable.data.v2.models.ReadModifyWriteRow;
2323
import com.google.cloud.bigtable.data.v2.models.Row;
24+
import com.google.cloud.bigtable.data.v2.models.TableId;
2425
import java.nio.charset.Charset;
2526

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

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import com.google.cloud.bigtable.data.v2.BigtableDataClient;
2222
import com.google.cloud.bigtable.data.v2.models.RowMutation;
23+
import com.google.cloud.bigtable.data.v2.models.TableId;
2324
import com.google.protobuf.ByteString;
2425

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

3839
RowMutation rowMutation =
39-
RowMutation.create(tableId, rowkey)
40+
RowMutation.create(TableId.of(tableId), rowkey)
4041
.setCell(
4142
COLUMN_FAMILY_NAME,
4243
ByteString.copyFrom("connected_cell".getBytes()),

samples/snippets/src/main/java/com/example/bigtable/deletes/BatchDeleteExample.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,16 @@
2323
import com.google.cloud.bigtable.data.v2.models.Query;
2424
import com.google.cloud.bigtable.data.v2.models.Row;
2525
import com.google.cloud.bigtable.data.v2.models.RowMutationEntry;
26+
import com.google.cloud.bigtable.data.v2.models.TableId;
2627
import java.io.IOException;
2728

2829
public class BatchDeleteExample {
2930
public void batchDelete(String projectId, String instanceId, String tableId)
3031
throws InterruptedException, IOException {
3132
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
32-
try (Batcher<RowMutationEntry, Void> batcher = dataClient.newBulkMutationBatcher(tableId)) {
33-
ServerStream<Row> rows = dataClient.readRows(Query.create(tableId));
33+
try (Batcher<RowMutationEntry, Void> batcher =
34+
dataClient.newBulkMutationBatcher(TableId.of(tableId))) {
35+
ServerStream<Row> rows = dataClient.readRows(Query.create(TableId.of(tableId)));
3436
for (Row row : rows) {
3537
batcher.add(
3638
RowMutationEntry.create(row.getKey()).deleteCells("cell_plan", "data_plan_05gb"));

samples/snippets/src/main/java/com/example/bigtable/deletes/ConditionalDeleteExample.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.google.cloud.bigtable.data.v2.models.ConditionalRowMutation;
2222
import com.google.cloud.bigtable.data.v2.models.Filters;
2323
import com.google.cloud.bigtable.data.v2.models.Mutation;
24+
import com.google.cloud.bigtable.data.v2.models.TableId;
2425
import java.io.IOException;
2526

2627
public class ConditionalDeleteExample {
@@ -30,7 +31,7 @@ public void conditionalDelete(String projectId, String instanceId, String tableI
3031
Filters.Filter condition = Filters.FILTERS.value().exactMatch("PQ2A.190405.004");
3132
Mutation mutation = Mutation.create().deleteCells("stats_summary", "os_build");
3233
dataClient.checkAndMutateRow(
33-
ConditionalRowMutation.create(tableId, "phone#4c410523#20190502")
34+
ConditionalRowMutation.create(TableId.of(tableId), "phone#4c410523#20190502")
3435
.condition(condition)
3536
.then(mutation));
3637
}

samples/snippets/src/main/java/com/example/bigtable/deletes/DeleteFromColumnExample.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,16 @@
2020
import com.google.cloud.bigtable.data.v2.BigtableDataClient;
2121
import com.google.cloud.bigtable.data.v2.models.Mutation;
2222
import com.google.cloud.bigtable.data.v2.models.RowMutation;
23+
import com.google.cloud.bigtable.data.v2.models.TableId;
2324
import java.io.IOException;
2425

2526
public class DeleteFromColumnExample {
2627
public void deleteFromColumnCells(String projectId, String instanceId, String tableId)
2728
throws IOException {
2829
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
2930
Mutation mutation = Mutation.create().deleteCells("cell_plan", "data_plan_01gb");
30-
dataClient.mutateRow(RowMutation.create(tableId, "phone#4c410523#20190501", mutation));
31+
dataClient.mutateRow(
32+
RowMutation.create(TableId.of(tableId), "phone#4c410523#20190501", mutation));
3133
}
3234
}
3335
}

samples/snippets/src/main/java/com/example/bigtable/deletes/DeleteFromColumnFamilyExample.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,16 @@
1919
// [START bigtable_delete_from_column_family]
2020
import com.google.cloud.bigtable.data.v2.BigtableDataClient;
2121
import com.google.cloud.bigtable.data.v2.models.RowMutation;
22+
import com.google.cloud.bigtable.data.v2.models.TableId;
2223
import java.io.IOException;
2324

2425
public class DeleteFromColumnFamilyExample {
2526
public void deleteFromColumnFamily(String projectId, String instanceId, String tableId)
2627
throws IOException {
2728
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
2829
dataClient.mutateRow(
29-
RowMutation.create(tableId, "phone#5c10102#20190501").deleteFamily("stats_summary"));
30+
RowMutation.create(TableId.of(tableId), "phone#5c10102#20190501")
31+
.deleteFamily("stats_summary"));
3032
}
3133
}
3234
}

samples/snippets/src/main/java/com/example/bigtable/deletes/DeleteFromRowExample.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,16 @@
2020
import com.google.cloud.bigtable.data.v2.BigtableDataClient;
2121
import com.google.cloud.bigtable.data.v2.models.Mutation;
2222
import com.google.cloud.bigtable.data.v2.models.RowMutation;
23+
import com.google.cloud.bigtable.data.v2.models.TableId;
2324
import java.io.IOException;
2425

2526
public class DeleteFromRowExample {
2627
public void deleteFromRow(String projectId, String instanceId, String tableId)
2728
throws IOException {
2829
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
2930
Mutation mutation = Mutation.create().deleteRow();
30-
dataClient.mutateRow(RowMutation.create(tableId, "phone#4c410523#20190501", mutation));
31+
dataClient.mutateRow(
32+
RowMutation.create(TableId.of(tableId), "phone#4c410523#20190501", mutation));
3133
}
3234
}
3335
}

samples/snippets/src/test/java/com/example/bigtable/HelloWorldTest.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.google.cloud.bigtable.admin.v2.models.CreateTableRequest;
2727
import com.google.cloud.bigtable.data.v2.BigtableDataClient;
2828
import com.google.cloud.bigtable.data.v2.BigtableDataSettings;
29+
import com.google.cloud.bigtable.data.v2.models.TableId;
2930
import java.io.IOException;
3031
import java.util.Random;
3132
import java.util.concurrent.TimeUnit;
@@ -99,9 +100,9 @@ public void testCreateAndDeleteTable() throws IOException {
99100
@Test
100101
public void testWriteToTable() {
101102
// Writes to a table.
102-
assertNull(dataClient.readRow(tableId, "rowKey0"));
103+
assertNull(dataClient.readRow(TableId.of(tableId), "rowKey0"));
103104
helloWorld.writeToTable();
104-
assertNotNull(dataClient.readRow(tableId, "rowKey0"));
105+
assertNotNull(dataClient.readRow(TableId.of(tableId), "rowKey0"));
105106
}
106107

107108
@Test

0 commit comments

Comments
 (0)