|
23 | 23 | import com.google.api.core.BetaApi;
|
24 | 24 | import com.google.api.core.InternalApi;
|
25 | 25 | import com.google.api.gax.batching.Batcher;
|
| 26 | +import com.google.api.gax.grpc.GrpcCallContext; |
26 | 27 | import com.google.api.gax.rpc.ApiExceptions;
|
27 | 28 | import com.google.api.gax.rpc.ResponseObserver;
|
28 | 29 | import com.google.api.gax.rpc.ServerStream;
|
@@ -1073,7 +1074,40 @@ public void bulkMutateRows(BulkMutation mutation) {
|
1073 | 1074 | */
|
1074 | 1075 | @BetaApi("This surface is likely to change as the batching surface evolves.")
|
1075 | 1076 | public Batcher<RowMutationEntry, Void> newBulkMutationBatcher(@Nonnull String tableId) {
|
1076 |
| - return stub.newMutateRowsBatcher(tableId); |
| 1077 | + return newBulkMutationBatcher(tableId, null); |
| 1078 | + } |
| 1079 | + |
| 1080 | + /** |
| 1081 | + * Mutates multiple rows in a batch. Each individual row is mutated atomically as in MutateRow, |
| 1082 | + * but the entire batch is not executed atomically. The returned Batcher instance is not |
| 1083 | + * threadsafe, it can only be used from single thread. This method allows customization of the |
| 1084 | + * underlying RPCs by passing in a {@link com.google.api.gax.grpc.GrpcCallContext}. The same |
| 1085 | + * context will be reused for all batches. This can be used to customize things like per attempt |
| 1086 | + * timeouts. |
| 1087 | + * |
| 1088 | + * <p>Sample Code: |
| 1089 | + * |
| 1090 | + * <pre>{@code |
| 1091 | + * try (BigtableDataClient bigtableDataClient = BigtableDataClient.create("[PROJECT]", "[INSTANCE]")) { |
| 1092 | + * try (Batcher<RowMutationEntry, Void> batcher = bigtableDataClient.newBulkMutationBatcher("[TABLE]", GrpcCallContext.createDefault().withTimeout(Duration.ofSeconds(10)))) { |
| 1093 | + * for (String someValue : someCollection) { |
| 1094 | + * ApiFuture<Void> entryFuture = |
| 1095 | + * batcher.add( |
| 1096 | + * RowMutationEntry.create("[ROW KEY]") |
| 1097 | + * .setCell("[FAMILY NAME]", "[QUALIFIER]", "[VALUE]")); |
| 1098 | + * } |
| 1099 | + * |
| 1100 | + * // Blocks until mutations are applied on all submitted row entries. |
| 1101 | + * batcher.flush(); |
| 1102 | + * } |
| 1103 | + * // Before `batcher` is closed, all remaining(If any) mutations are applied. |
| 1104 | + * } |
| 1105 | + * }</pre> |
| 1106 | + */ |
| 1107 | + @BetaApi("This surface is likely to change as the batching surface evolves.") |
| 1108 | + public Batcher<RowMutationEntry, Void> newBulkMutationBatcher( |
| 1109 | + @Nonnull String tableId, @Nullable GrpcCallContext ctx) { |
| 1110 | + return stub.newMutateRowsBatcher(tableId, ctx); |
1077 | 1111 | }
|
1078 | 1112 |
|
1079 | 1113 | /**
|
@@ -1159,11 +1193,61 @@ public Batcher<ByteString, Row> newBulkReadRowsBatcher(String tableId) {
|
1159 | 1193 | */
|
1160 | 1194 | public Batcher<ByteString, Row> newBulkReadRowsBatcher(
|
1161 | 1195 | String tableId, @Nullable Filters.Filter filter) {
|
| 1196 | + return newBulkReadRowsBatcher(tableId, filter, null); |
| 1197 | + } |
| 1198 | + |
| 1199 | + /** |
| 1200 | + * Reads rows for given tableId and filter criteria in a batch. If the row does not exist, the |
| 1201 | + * value will be null. The returned Batcher instance is not threadsafe, it can only be used from a |
| 1202 | + * single thread. This method allows customization of the underlying RPCs by passing in a {@link |
| 1203 | + * com.google.api.gax.grpc.GrpcCallContext}. The same context will be reused for all batches. This |
| 1204 | + * can be used to customize things like per attempt timeouts. |
| 1205 | + * |
| 1206 | + * <p>Performance notice: The ReadRows protocol requires that rows are sent in ascending key |
| 1207 | + * order, which means that the keys are processed sequentially on the server-side, so batching |
| 1208 | + * allows improving throughput but not latency. Lower latencies can be achieved by sending smaller |
| 1209 | + * requests concurrently. |
| 1210 | + * |
| 1211 | + * <p>Sample Code: |
| 1212 | + * |
| 1213 | + * <pre>{@code |
| 1214 | + * try (BigtableDataClient bigtableDataClient = BigtableDataClient.create("[PROJECT]", "[INSTANCE]")) { |
| 1215 | + * |
| 1216 | + * // Build the filter expression |
| 1217 | + * Filter filter = FILTERS.chain() |
| 1218 | + * .filter(FILTERS.key().regex("prefix.*")) |
| 1219 | + * .filter(FILTERS.limit().cellsPerRow(10)); |
| 1220 | + * |
| 1221 | + * List<ApiFuture<Row>> rows = new ArrayList<>(); |
| 1222 | + * |
| 1223 | + * try (Batcher<ByteString, Row> batcher = bigtableDataClient.newBulkReadRowsBatcher( |
| 1224 | + * "[TABLE]", filter, GrpcCallContext.createDefault().withTimeout(Duration.ofSeconds(10)))) { |
| 1225 | + * for (String someValue : someCollection) { |
| 1226 | + * ApiFuture<Row> rowFuture = |
| 1227 | + * batcher.add(ByteString.copyFromUtf8("[ROW KEY]")); |
| 1228 | + * rows.add(rowFuture); |
| 1229 | + * } |
| 1230 | + * |
| 1231 | + * // [Optional] Sends collected elements for batching asynchronously. |
| 1232 | + * batcher.sendOutstanding(); |
| 1233 | + * |
| 1234 | + * // [Optional] Invokes sendOutstanding() and awaits until all pending entries are resolved. |
| 1235 | + * batcher.flush(); |
| 1236 | + * } |
| 1237 | + * // batcher.close() invokes `flush()` which will in turn invoke `sendOutstanding()` with await for |
| 1238 | + * pending batches until its resolved. |
| 1239 | + * |
| 1240 | + * List<Row> actualRows = ApiFutures.allAsList(rows).get(); |
| 1241 | + * } |
| 1242 | + * }</pre> |
| 1243 | + */ |
| 1244 | + public Batcher<ByteString, Row> newBulkReadRowsBatcher( |
| 1245 | + String tableId, @Nullable Filters.Filter filter, @Nullable GrpcCallContext ctx) { |
1162 | 1246 | Query query = Query.create(tableId);
|
1163 | 1247 | if (filter != null) {
|
1164 |
| - query = query.filter(filter); |
| 1248 | + query.filter(filter); |
1165 | 1249 | }
|
1166 |
| - return stub.newBulkReadRowsBatcher(query); |
| 1250 | + return stub.newBulkReadRowsBatcher(query, ctx); |
1167 | 1251 | }
|
1168 | 1252 |
|
1169 | 1253 | /**
|
|
0 commit comments