From d141c3d597cbd682050b78bb3828fd4d8c96a7c3 Mon Sep 17 00:00:00 2001 From: Rahul Kesharwani <42969463+rahulKQL@users.noreply.github.com> Date: Thu, 6 Feb 2020 03:52:11 +0530 Subject: [PATCH] feat: add row exists api (#190) * feat: add row exists api Adding a new Row exists() API in `BigtableDataClient` * rephrased java doc * addressed feedback comment to add FILTERS.value().strip() --- .../bigtable/data/v2/BigtableDataClient.java | 134 ++++++++++++++++++ .../data/v2/BigtableDataClientTest.java | 51 +++++++ .../cloud/bigtable/data/v2/it/ReadIT.java | 20 +++ 3 files changed, 205 insertions(+) diff --git a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/BigtableDataClient.java b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/BigtableDataClient.java index 3b287594f..40d9f70ca 100644 --- a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/BigtableDataClient.java +++ b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/BigtableDataClient.java @@ -15,7 +15,11 @@ */ package com.google.cloud.bigtable.data.v2; +import static com.google.cloud.bigtable.data.v2.models.Filters.FILTERS; + +import com.google.api.core.ApiFunction; import com.google.api.core.ApiFuture; +import com.google.api.core.ApiFutures; import com.google.api.core.BetaApi; import com.google.api.core.InternalApi; import com.google.api.gax.batching.Batcher; @@ -36,6 +40,7 @@ import com.google.cloud.bigtable.data.v2.models.RowMutation; import com.google.cloud.bigtable.data.v2.models.RowMutationEntry; import com.google.cloud.bigtable.data.v2.stub.EnhancedBigtableStub; +import com.google.common.util.concurrent.MoreExecutors; import com.google.protobuf.ByteString; import java.io.IOException; import java.util.List; @@ -159,6 +164,135 @@ public static BigtableDataClient create(BigtableDataSettings settings) throws IO this.stub = stub; } + /** + * Confirms synchronously if given row key exists or not. + * + *
Sample code: + * + *
{@code + * try (BigtableDataClient bigtableDataClient = BigtableDataClient.create("[PROJECT]", "[INSTANCE]")) { + * String tableId = "[TABLE]"; + * String key = "key"; + * + * boolean isRowPresent = bigtableDataClient.exists(tableId, key); + * + * // Do something with result, for example, display a message + * if(isRowPresent) { + * System.out.println(key + " is present"); + * } + * } catch(ApiException e) { + * e.printStackTrace(); + * } + * }+ * + * @throws com.google.api.gax.rpc.ApiException when a serverside error occurs + */ + public boolean exists(String tableId, String rowKey) { + return ApiExceptions.callAndTranslateApiException(existsAsync(tableId, rowKey)); + } + + /** + * Confirms synchronously if given row key exists or not. + * + *
Sample code: + * + *
{@code + * try (BigtableDataClient bigtableDataClient = BigtableDataClient.create("[PROJECT]", "[INSTANCE]")) { + * String tableId = "[TABLE]"; + * ByteString key = ByteString.copyFromUtf8("key"); + * + * boolean isRowPresent = bigtableDataClient.exists(tableId, key); + * + * // Do something with result, for example, display a message + * if(isRowPresent) { + * System.out.println(key.toStringUtf8() + " is present"); + * } + * } catch(ApiException e) { + * e.printStackTrace(); + * } + * }+ * + * @throws com.google.api.gax.rpc.ApiException when a serverside error occurs + */ + public boolean exists(String tableId, ByteString rowKey) { + return ApiExceptions.callAndTranslateApiException(existsAsync(tableId, rowKey)); + } + + /** + * Confirms asynchronously if given row key exists or not. + * + *
Sample code: + * + *
{@code + * try (BigtableDataClient bigtableDataClient = BigtableDataClient.create("[PROJECT]", "[INSTANCE]")) { + * String tableId = "[TABLE]"; + * final String key = "key"; + * + * ApiFuture+ */ + public ApiFuturefutureResult = bigtableDataClient.existsAsync(tableId, key); + * + * ApiFutures.addCallback(futureResult, new ApiFutureCallback () { + * public void onFailure(Throwable t) { + * t.printStackTrace(); + * } + * public void onSuccess(Boolean isRowPresent) { + * if(isRowPresent) { + * System.out.println(key + " is present"); + * } + * } + * }, MoreExecutors.directExecutor()); + * } + * }
Sample code: + * + *
{@code + * try (BigtableDataClient bigtableDataClient = BigtableDataClient.create("[PROJECT]", "[INSTANCE]")) { + * String tableId = "[TABLE]"; + * final ByteString key = ByteString.copyFromUtf8("key"); + * + * ApiFuture+ */ + public ApiFuturefutureResult = bigtableDataClient.existsAsync(tableId, key); + * + * ApiFutures.addCallback(futureResult, new ApiFutureCallback () { + * public void onFailure(Throwable t) { + * t.printStackTrace(); + * } + * public void onSuccess(Boolean isRowPresent) { + * if(isRowPresent) { + * System.out.println(key.toStringUtf8() + " is present"); + * } + * } + * }, MoreExecutors.directExecutor()); + * } + * }