Skip to content

Commit 68e6d1a

Browse files
elisheva-qlogicigorbernstein2
authored andcommitted
Bigtable: add sync methods (#3856)
1 parent 82c0d01 commit 68e6d1a

File tree

2 files changed

+321
-5
lines changed

2 files changed

+321
-5
lines changed

google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/BigtableDataClient.java

Lines changed: 201 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.google.api.core.ApiFuture;
1919
import com.google.api.core.BetaApi;
2020
import com.google.api.core.InternalApi;
21+
import com.google.api.gax.rpc.ApiExceptions;
2122
import com.google.api.gax.rpc.ResponseObserver;
2223
import com.google.api.gax.rpc.ServerStream;
2324
import com.google.api.gax.rpc.ServerStreamingCallable;
@@ -136,6 +137,66 @@ public static BigtableDataClient create(BigtableDataSettings settings) throws IO
136137
this.stub = stub;
137138
}
138139

140+
/**
141+
* Convenience method for synchronously reading a single row. If the row does not exist, the
142+
* value will be null.
143+
*
144+
* <p>Sample code:
145+
*
146+
* <pre>{code
147+
* InstanceName instanceName = InstanceName.of("[PROJECT]", "[INSTANCE]");
148+
* try (BigtableDataClient bigtableDataClient = BigtableDataClient.create(instanceName)) {
149+
* String tableId = "[TABLE]";
150+
*
151+
* Row row = bigtableDataClient.readRow(tableId, ByteString.copyFromUtf8("key"));
152+
* // Do something with row, for example, display all cells
153+
* if(row != null) {
154+
* System.out.println(row.getKey().toStringUtf8());
155+
* for(RowCell cell : row.getCells()) {
156+
* System.out.println("Family: " + cell.getFamily() + " Qualifier: " + cell.getQualifier().toStringUtf8() + " Value: " + cell.getValue().toStringUtf8());
157+
* }
158+
* }
159+
* } catch(ApiException e) {
160+
* e.printStackTrace();
161+
* }
162+
* }</pre>
163+
*
164+
* @throws com.google.api.gax.rpc.ApiException when a serverside error occurs
165+
*/
166+
public Row readRow(String tableId, ByteString rowKey) {
167+
return ApiExceptions.callAndTranslateApiException(readRowAsync(tableId, rowKey));
168+
}
169+
170+
/**
171+
* Convenience method for synchronously reading a single row. If the row does not exist, the
172+
* value will be null.
173+
*
174+
* <p>Sample code:
175+
*
176+
* <pre>{code
177+
* InstanceName instanceName = InstanceName.of("[PROJECT]", "[INSTANCE]");
178+
* try (BigtableDataClient bigtableDataClient = BigtableDataClient.create(instanceName)) {
179+
* String tableId = "[TABLE]";
180+
*
181+
* Row row = bigtableDataClient.readRow(tableId, "key");
182+
* // Do something with row, for example, display all cells
183+
* if(row != null) {
184+
* System.out.println(row.getKey().toStringUtf8());
185+
* for(RowCell cell : row.getCells()) {
186+
* System.out.println("Family: " + cell.getFamily() + " Qualifier: " + cell.getQualifier().toStringUtf8() + " Value: " + cell.getValue().toStringUtf8());
187+
* }
188+
* }
189+
* } catch(ApiException e) {
190+
* e.printStackTrace();
191+
* }
192+
* }</pre>
193+
*
194+
* @throws com.google.api.gax.rpc.ApiException when a serverside error occurs
195+
*/
196+
public Row readRow(String tableId, String rowKey) {
197+
return ApiExceptions.callAndTranslateApiException(readRowAsync(tableId, rowKey));
198+
}
199+
139200
/**
140201
* Convenience method for asynchronously reading a single row. If the row does not exist, the
141202
* future's value will be null.
@@ -158,7 +219,9 @@ public static BigtableDataClient create(BigtableDataSettings settings) throws IO
158219
* }
159220
* }
160221
* public void onSuccess(Row result) {
161-
* System.out.println("Got row: " + result);
222+
* if (result != null) {
223+
* System.out.println("Got row: " + result);
224+
* }
162225
* }
163226
* }, MoreExecutors.directExecutor());
164227
* }
@@ -190,7 +253,9 @@ public ApiFuture<Row> readRowAsync(String tableId, String rowKey) {
190253
* }
191254
* }
192255
* public void onSuccess(Row row) {
193-
* System.out.println("Got row: " + row);
256+
* if (result != null) {
257+
* System.out.println("Got row: " + result);
258+
* }
194259
* }
195260
* }, MoreExecutors.directExecutor());
196261
* }
@@ -374,6 +439,33 @@ public <RowT> ServerStreamingCallable<Query, RowT> readRowsCallable(RowAdapter<R
374439
return stub.createReadRowsCallable(rowAdapter);
375440
}
376441

442+
/**
443+
* Convenience method to synchronously return a sample of row keys in the table. The returned row
444+
* keys will delimit contiguous sections of the table of approximately equal size, which can be
445+
* used to break up the data for distributed tasks like mapreduces.
446+
*
447+
* <p>Sample code:
448+
*
449+
* <pre>{@code
450+
* InstanceName instanceName = InstanceName.of("[PROJECT]", "[INSTANCE]");
451+
* try (BigtableDataClient bigtableDataClient = BigtableDataClient.create(instanceName)) {
452+
* String tableId = "[TABLE_ID]";
453+
*
454+
* List<KeyOffset> keyOffsets = bigtableDataClient.sampleRowKeys(tableId);
455+
* for(KeyOffset keyOffset : keyOffsets) {
456+
* // Do something with keyOffset
457+
* }
458+
* } catch(ApiException e) {
459+
* e.printStackTrace();
460+
* }
461+
* }</pre>
462+
*
463+
* @throws com.google.api.gax.rpc.ApiException when a serverside error occurs
464+
*/
465+
public List<KeyOffset> sampleRowKeys(String tableId) {
466+
return ApiExceptions.callAndTranslateApiException(sampleRowKeysAsync(tableId));
467+
}
468+
377469
/**
378470
* Convenience method to asynchronously return a sample of row keys in the table. The returned row
379471
* keys will delimit contiguous sections of the table of approximately equal size, which can be
@@ -447,6 +539,30 @@ public UnaryCallable<String, List<KeyOffset>> sampleRowKeysCallable() {
447539
return stub.sampleRowKeysCallable();
448540
}
449541

542+
/**
543+
* Convenience method to synchronously mutate a single row atomically. Cells already present in
544+
* the row are left unchanged unless explicitly changed by the {@link RowMutation}.
545+
*
546+
* <p>Sample code:
547+
*
548+
* <pre>{@code
549+
* InstanceName instanceName = InstanceName.of("[PROJECT]", "[INSTANCE]");
550+
* try (BigtableDataClient bigtableDataClient = BigtableDataClient.create(instanceName)) {
551+
* RowMutation mutation = RowMutation.create("[TABLE]", "[ROW KEY]")
552+
* .setCell("[FAMILY NAME]", "[QUALIFIER]", "[VALUE]");
553+
*
554+
* bigtableDataClient.mutateRow(mutation);
555+
* } catch(ApiException e) {
556+
* e.printStackTrace();
557+
* }
558+
* }</pre>
559+
*
560+
* @throws com.google.api.gax.rpc.ApiException when a serverside error occurs
561+
*/
562+
public void mutateRow(RowMutation rowMutation) {
563+
ApiExceptions.callAndTranslateApiException(mutateRowAsync(rowMutation));
564+
}
565+
450566
/**
451567
* Convenience method to asynchronously mutate a single row atomically. Cells already present in
452568
* the row are left unchanged unless explicitly changed by the {@link RowMutation}.
@@ -547,6 +663,35 @@ public BulkMutationBatcher newBulkMutationBatcher() {
547663
* try (BigtableDataClient bigtableDataClient = BigtableDataClient.create(instanceName)) {
548664
* BulkMutation batch = BulkMutation.create("[TABLE]");
549665
* for (String someValue : someCollection) {
666+
* batch.add("[ROW KEY]", Mutation.create().setCell("[FAMILY NAME]", "[QUALIFIER]", "[VALUE]"));
667+
* }
668+
* bigtableDataClient.bulkMutateRows(batch);
669+
* } catch(ApiException e) {
670+
* e.printStackTrace();
671+
* } catch(MutateRowsException e) {
672+
* e.printStackTrace();
673+
* }
674+
* }</pre>
675+
*
676+
* @throws com.google.api.gax.rpc.ApiException when a serverside error occurs
677+
* @throws com.google.cloud.bigtable.data.v2.models.MutateRowsException if any of the entries failed to be applied
678+
*/
679+
public void bulkMutateRows(BulkMutation mutation) {
680+
ApiExceptions.callAndTranslateApiException(bulkMutateRowsAsync(mutation));
681+
}
682+
683+
/**
684+
* Convenience method to mutate multiple rows in a batch. Each individual row is mutated
685+
* atomically as in MutateRow, but the entire batch is not executed atomically. Unlike {@link
686+
* #newBulkMutationBatcher()}, this method expects the mutations to be pre-batched.
687+
*
688+
* <p>Sample code:
689+
*
690+
* <pre>{@code
691+
* InstanceName instanceName = InstanceName.of("[PROJECT]", "[INSTANCE]");
692+
* try (BigtableClient bigtableClient = BigtableClient.create(instanceName)) {
693+
* BulkMutation batch = BulkMutation.create("[TABLE]");
694+
* for (String someValue : someCollection) {
550695
* ApiFuture<Void> entryFuture = batch.add("[ROW KEY]",
551696
* Mutation.create().setCell("[FAMILY NAME]", "[QUALIFIER]", "[VALUE]"));
552697
* }
@@ -594,6 +739,33 @@ public UnaryCallable<BulkMutation, Void> bulkMutationCallable() {
594739
return stub.bulkMutateRowsCallable();
595740
}
596741

742+
/**
743+
* Convenience method to synchronously mutate a row atomically based on the output of a filter.
744+
*
745+
* <p>Sample code:
746+
*
747+
* <pre>{@code
748+
* InstanceName instanceName = InstanceName.of("[PROJECT]", "[INSTANCE]");
749+
* try (BigtableDataClient bigtableDataClient = BigtableDataClient.create(instanceName)) {
750+
* ConditionalRowMutation mutation = ConditionalRowMutation.create("[TABLE]", "[KEY]")
751+
* .condition(FILTERS.value().regex("old-value"))
752+
* .then(
753+
* Mutation.create()
754+
* .setCell("[FAMILY]", "[QUALIFIER]", "[VALUE]")
755+
* );
756+
*
757+
* Boolean result = bigtableDataClient.checkAndMutateRow(mutation);
758+
* } catch(ApiException e) {
759+
* e.printStackTrace();
760+
* }
761+
* }</pre>
762+
*
763+
* @throws com.google.api.gax.rpc.ApiException when a serverside error occurs
764+
*/
765+
public Boolean checkAndMutateRow(ConditionalRowMutation mutation) {
766+
return ApiExceptions.callAndTranslateApiException(checkAndMutateRowAsync(mutation));
767+
}
768+
597769
/**
598770
* Convenience method to asynchronously mutate a row atomically based on the output of a filter.
599771
*
@@ -663,6 +835,33 @@ public UnaryCallable<ConditionalRowMutation, Boolean> checkAndMutateRowCallable(
663835
return stub.checkAndMutateRowCallable();
664836
}
665837

838+
/**
839+
* Convenience method that synchronously modifies a row atomically on the server. The method
840+
* reads the latest existing timestamp and value from the specified columns and writes a new
841+
* entry. The new value for the timestamp is the greater of the existing timestamp or the current
842+
* server time. The method returns the new contents of all modified cells.
843+
*
844+
* <p>Sample code:
845+
*
846+
* <pre>{@code
847+
* InstanceName instanceName = InstanceName.of("[PROJECT]", "[INSTANCE]");
848+
* try (BigtableDataClient bigtableDataClient = BigtableDataClient.create(instanceName)) {
849+
* ReadModifyWriteRow mutation = ReadModifyWriteRow.create("[TABLE]", "[KEY]")
850+
* .increment("[FAMILY]", "[QUALIFIER]", 1)
851+
* .append("[FAMILY2]", "[QUALIFIER2]", "suffix");
852+
*
853+
* Row success = bigtableDataClient.readModifyWriteRow(mutation);
854+
* } catch(ApiException e) {
855+
* e.printStackTrace();
856+
* }
857+
* }</pre>
858+
*
859+
* @throws com.google.api.gax.rpc.ApiException when a serverside error occurs
860+
*/
861+
public Row readModifyWriteRow(ReadModifyWriteRow mutation) {
862+
return ApiExceptions.callAndTranslateApiException(readModifyWriteRowAsync(mutation));
863+
}
864+
666865
/**
667866
* Convenience method that asynchronously modifies a row atomically on the server. The method
668867
* reads the latest existing timestamp and value from the specified columns and writes a new

0 commit comments

Comments
 (0)