|
15 | 15 | */ |
16 | 16 | package com.google.cloud.bigtable.data.v2; |
17 | 17 |
|
| 18 | +import static com.google.cloud.bigtable.data.v2.models.Filters.FILTERS; |
| 19 | + |
| 20 | +import com.google.api.core.ApiFunction; |
18 | 21 | import com.google.api.core.ApiFuture; |
| 22 | +import com.google.api.core.ApiFutures; |
19 | 23 | import com.google.api.core.BetaApi; |
20 | 24 | import com.google.api.core.InternalApi; |
21 | 25 | import com.google.api.gax.batching.Batcher; |
|
36 | 40 | import com.google.cloud.bigtable.data.v2.models.RowMutation; |
37 | 41 | import com.google.cloud.bigtable.data.v2.models.RowMutationEntry; |
38 | 42 | import com.google.cloud.bigtable.data.v2.stub.EnhancedBigtableStub; |
| 43 | +import com.google.common.util.concurrent.MoreExecutors; |
39 | 44 | import com.google.protobuf.ByteString; |
40 | 45 | import java.io.IOException; |
41 | 46 | import java.util.List; |
@@ -159,6 +164,135 @@ public static BigtableDataClient create(BigtableDataSettings settings) throws IO |
159 | 164 | this.stub = stub; |
160 | 165 | } |
161 | 166 |
|
| 167 | + /** |
| 168 | + * Confirms synchronously if given row key exists or not. |
| 169 | + * |
| 170 | + * <p>Sample code: |
| 171 | + * |
| 172 | + * <pre>{@code |
| 173 | + * try (BigtableDataClient bigtableDataClient = BigtableDataClient.create("[PROJECT]", "[INSTANCE]")) { |
| 174 | + * String tableId = "[TABLE]"; |
| 175 | + * String key = "key"; |
| 176 | + * |
| 177 | + * boolean isRowPresent = bigtableDataClient.exists(tableId, key); |
| 178 | + * |
| 179 | + * // Do something with result, for example, display a message |
| 180 | + * if(isRowPresent) { |
| 181 | + * System.out.println(key + " is present"); |
| 182 | + * } |
| 183 | + * } catch(ApiException e) { |
| 184 | + * e.printStackTrace(); |
| 185 | + * } |
| 186 | + * }</pre> |
| 187 | + * |
| 188 | + * @throws com.google.api.gax.rpc.ApiException when a serverside error occurs |
| 189 | + */ |
| 190 | + public boolean exists(String tableId, String rowKey) { |
| 191 | + return ApiExceptions.callAndTranslateApiException(existsAsync(tableId, rowKey)); |
| 192 | + } |
| 193 | + |
| 194 | + /** |
| 195 | + * Confirms synchronously if given row key exists or not. |
| 196 | + * |
| 197 | + * <p>Sample code: |
| 198 | + * |
| 199 | + * <pre>{@code |
| 200 | + * try (BigtableDataClient bigtableDataClient = BigtableDataClient.create("[PROJECT]", "[INSTANCE]")) { |
| 201 | + * String tableId = "[TABLE]"; |
| 202 | + * ByteString key = ByteString.copyFromUtf8("key"); |
| 203 | + * |
| 204 | + * boolean isRowPresent = bigtableDataClient.exists(tableId, key); |
| 205 | + * |
| 206 | + * // Do something with result, for example, display a message |
| 207 | + * if(isRowPresent) { |
| 208 | + * System.out.println(key.toStringUtf8() + " is present"); |
| 209 | + * } |
| 210 | + * } catch(ApiException e) { |
| 211 | + * e.printStackTrace(); |
| 212 | + * } |
| 213 | + * }</pre> |
| 214 | + * |
| 215 | + * @throws com.google.api.gax.rpc.ApiException when a serverside error occurs |
| 216 | + */ |
| 217 | + public boolean exists(String tableId, ByteString rowKey) { |
| 218 | + return ApiExceptions.callAndTranslateApiException(existsAsync(tableId, rowKey)); |
| 219 | + } |
| 220 | + |
| 221 | + /** |
| 222 | + * Confirms asynchronously if given row key exists or not. |
| 223 | + * |
| 224 | + * <p>Sample code: |
| 225 | + * |
| 226 | + * <pre>{@code |
| 227 | + * try (BigtableDataClient bigtableDataClient = BigtableDataClient.create("[PROJECT]", "[INSTANCE]")) { |
| 228 | + * String tableId = "[TABLE]"; |
| 229 | + * final String key = "key"; |
| 230 | + * |
| 231 | + * ApiFuture<Boolean> futureResult = bigtableDataClient.existsAsync(tableId, key); |
| 232 | + * |
| 233 | + * ApiFutures.addCallback(futureResult, new ApiFutureCallback<Boolean>() { |
| 234 | + * public void onFailure(Throwable t) { |
| 235 | + * t.printStackTrace(); |
| 236 | + * } |
| 237 | + * public void onSuccess(Boolean isRowPresent) { |
| 238 | + * if(isRowPresent) { |
| 239 | + * System.out.println(key + " is present"); |
| 240 | + * } |
| 241 | + * } |
| 242 | + * }, MoreExecutors.directExecutor()); |
| 243 | + * } |
| 244 | + * }</pre> |
| 245 | + */ |
| 246 | + public ApiFuture<Boolean> existsAsync(String tableId, String rowKey) { |
| 247 | + return existsAsync(tableId, ByteString.copyFromUtf8(rowKey)); |
| 248 | + } |
| 249 | + |
| 250 | + /** |
| 251 | + * Confirms asynchronously if given row key exists or not. |
| 252 | + * |
| 253 | + * <p>Sample code: |
| 254 | + * |
| 255 | + * <pre>{@code |
| 256 | + * try (BigtableDataClient bigtableDataClient = BigtableDataClient.create("[PROJECT]", "[INSTANCE]")) { |
| 257 | + * String tableId = "[TABLE]"; |
| 258 | + * final ByteString key = ByteString.copyFromUtf8("key"); |
| 259 | + * |
| 260 | + * ApiFuture<Boolean> futureResult = bigtableDataClient.existsAsync(tableId, key); |
| 261 | + * |
| 262 | + * ApiFutures.addCallback(futureResult, new ApiFutureCallback<Boolean>() { |
| 263 | + * public void onFailure(Throwable t) { |
| 264 | + * t.printStackTrace(); |
| 265 | + * } |
| 266 | + * public void onSuccess(Boolean isRowPresent) { |
| 267 | + * if(isRowPresent) { |
| 268 | + * System.out.println(key.toStringUtf8() + " is present"); |
| 269 | + * } |
| 270 | + * } |
| 271 | + * }, MoreExecutors.directExecutor()); |
| 272 | + * } |
| 273 | + * }</pre> |
| 274 | + */ |
| 275 | + public ApiFuture<Boolean> existsAsync(String tableId, ByteString rowKey) { |
| 276 | + Query query = |
| 277 | + Query.create(tableId) |
| 278 | + .rowKey(rowKey) |
| 279 | + .filter( |
| 280 | + FILTERS |
| 281 | + .chain() |
| 282 | + .filter(FILTERS.limit().cellsPerRow(1)) |
| 283 | + .filter(FILTERS.value().strip())); |
| 284 | + ApiFuture<Row> resultFuture = stub.readRowCallable().futureCall(query); |
| 285 | + return ApiFutures.transform( |
| 286 | + resultFuture, |
| 287 | + new ApiFunction<Row, Boolean>() { |
| 288 | + @Override |
| 289 | + public Boolean apply(Row row) { |
| 290 | + return row != null; |
| 291 | + } |
| 292 | + }, |
| 293 | + MoreExecutors.directExecutor()); |
| 294 | + } |
| 295 | + |
162 | 296 | /** |
163 | 297 | * Convenience method for synchronously reading a single row. If the row does not exist, the value |
164 | 298 | * will be null. |
|
0 commit comments