|
20 | 20 | import com.google.api.core.ApiFuture; |
21 | 21 | import com.google.api.core.ApiFutures; |
22 | 22 | import com.google.api.gax.rpc.ApiExceptions; |
| 23 | +import com.google.api.gax.rpc.NotFoundException; |
23 | 24 | import com.google.bigtable.admin.v2.DeleteTableRequest; |
24 | 25 | import com.google.bigtable.admin.v2.DropRowRangeRequest; |
25 | 26 | import com.google.bigtable.admin.v2.GetTableRequest; |
@@ -323,6 +324,65 @@ public ApiFuture<Void> deleteTableAsync(String tableId) { |
323 | 324 | return transformToVoid(this.stub.deleteTableCallable().futureCall(request)); |
324 | 325 | } |
325 | 326 |
|
| 327 | + /** |
| 328 | + * Checks if the table specified by the tableId exists |
| 329 | + * |
| 330 | + * <p>Sample code: |
| 331 | + * |
| 332 | + * <pre>{@code |
| 333 | + * if(client.exists("my-table")) { |
| 334 | + * System.out.println("Table exists"); |
| 335 | + * } |
| 336 | + * }</pre> |
| 337 | + */ |
| 338 | + public boolean exists(String tableId) { |
| 339 | + return ApiExceptions.callAndTranslateApiException(existsAsync(tableId)); |
| 340 | + } |
| 341 | + |
| 342 | + /** |
| 343 | + * Asynchronously checks if the table specified by the tableId exists |
| 344 | + * |
| 345 | + * <p>Sample code: |
| 346 | + * |
| 347 | + * <pre>{@code |
| 348 | + * ApiFuture<Boolean> found = client.existsAsync("my-table"); |
| 349 | + * |
| 350 | + * ApiFutures.addCallback( |
| 351 | + * found, |
| 352 | + * new ApiFutureCallback<Boolean>() { |
| 353 | + * public void onSuccess(Boolean found) { |
| 354 | + * if (found) { |
| 355 | + * System.out.println("Table exists"); |
| 356 | + * } else { |
| 357 | + * System.out.println("Table not found"); |
| 358 | + * } |
| 359 | + * } |
| 360 | + * |
| 361 | + * public void onFailure(Throwable t) { |
| 362 | + * t.printStackTrace(); |
| 363 | + * } |
| 364 | + * }, |
| 365 | + * MoreExecutors.directExecutor() |
| 366 | + * ); |
| 367 | + * }</pre> |
| 368 | + */ |
| 369 | + public ApiFuture<Boolean> existsAsync(String tableId) { |
| 370 | + |
| 371 | + ApiFuture<Table> protoFuture = getTableAsync(tableId, com.google.bigtable.admin.v2.Table.View.NAME_ONLY); |
| 372 | + |
| 373 | + ApiFuture<Boolean> existsFuture = ApiFutures.transform(protoFuture, new ApiFunction<Table, Boolean>() { |
| 374 | + @Override public Boolean apply(Table ignored) { |
| 375 | + return true; |
| 376 | + } |
| 377 | + }, MoreExecutors.directExecutor()); |
| 378 | + |
| 379 | + return ApiFutures.catching(existsFuture, NotFoundException.class, new ApiFunction<NotFoundException, Boolean>() { |
| 380 | + @Override public Boolean apply(NotFoundException ignored) { |
| 381 | + return false; |
| 382 | + } |
| 383 | + }, MoreExecutors.directExecutor()); |
| 384 | + } |
| 385 | + |
326 | 386 | /** |
327 | 387 | * Gets the table metadata by tableId. |
328 | 388 | * |
@@ -373,8 +433,13 @@ public Table getTable(String tableId) { |
373 | 433 | */ |
374 | 434 | @SuppressWarnings("WeakerAccess") |
375 | 435 | public ApiFuture<Table> getTableAsync(String tableId) { |
| 436 | + return getTableAsync(tableId, com.google.bigtable.admin.v2.Table.View.SCHEMA_VIEW); |
| 437 | + } |
| 438 | + |
| 439 | + private ApiFuture<Table> getTableAsync(String tableId, com.google.bigtable.admin.v2.Table.View view) { |
376 | 440 | GetTableRequest request = GetTableRequest.newBuilder() |
377 | 441 | .setName(getTableName(tableId)) |
| 442 | + .setView(view) |
378 | 443 | .build(); |
379 | 444 |
|
380 | 445 | return transformToTableResponse( |
|
0 commit comments