Skip to content

Commit

Permalink
Bigtable: add sync methods (#3856)
Browse files Browse the repository at this point in the history
  • Loading branch information
elisheva-qlogic authored and igorbernstein2 committed Nov 7, 2018
1 parent 82c0d01 commit 68e6d1a
Show file tree
Hide file tree
Showing 2 changed files with 321 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.google.api.core.ApiFuture;
import com.google.api.core.BetaApi;
import com.google.api.core.InternalApi;
import com.google.api.gax.rpc.ApiExceptions;
import com.google.api.gax.rpc.ResponseObserver;
import com.google.api.gax.rpc.ServerStream;
import com.google.api.gax.rpc.ServerStreamingCallable;
Expand Down Expand Up @@ -136,6 +137,66 @@ public static BigtableDataClient create(BigtableDataSettings settings) throws IO
this.stub = stub;
}

/**
* Convenience method for synchronously reading a single row. If the row does not exist, the
* value will be null.
*
* <p>Sample code:
*
* <pre>{code
* InstanceName instanceName = InstanceName.of("[PROJECT]", "[INSTANCE]");
* try (BigtableDataClient bigtableDataClient = BigtableDataClient.create(instanceName)) {
* String tableId = "[TABLE]";
*
* Row row = bigtableDataClient.readRow(tableId, ByteString.copyFromUtf8("key"));
* // Do something with row, for example, display all cells
* if(row != null) {
* System.out.println(row.getKey().toStringUtf8());
* for(RowCell cell : row.getCells()) {
* System.out.println("Family: " + cell.getFamily() + " Qualifier: " + cell.getQualifier().toStringUtf8() + " Value: " + cell.getValue().toStringUtf8());
* }
* }
* } catch(ApiException e) {
* e.printStackTrace();
* }
* }</pre>
*
* @throws com.google.api.gax.rpc.ApiException when a serverside error occurs
*/
public Row readRow(String tableId, ByteString rowKey) {
return ApiExceptions.callAndTranslateApiException(readRowAsync(tableId, rowKey));
}

/**
* Convenience method for synchronously reading a single row. If the row does not exist, the
* value will be null.
*
* <p>Sample code:
*
* <pre>{code
* InstanceName instanceName = InstanceName.of("[PROJECT]", "[INSTANCE]");
* try (BigtableDataClient bigtableDataClient = BigtableDataClient.create(instanceName)) {
* String tableId = "[TABLE]";
*
* Row row = bigtableDataClient.readRow(tableId, "key");
* // Do something with row, for example, display all cells
* if(row != null) {
* System.out.println(row.getKey().toStringUtf8());
* for(RowCell cell : row.getCells()) {
* System.out.println("Family: " + cell.getFamily() + " Qualifier: " + cell.getQualifier().toStringUtf8() + " Value: " + cell.getValue().toStringUtf8());
* }
* }
* } catch(ApiException e) {
* e.printStackTrace();
* }
* }</pre>
*
* @throws com.google.api.gax.rpc.ApiException when a serverside error occurs
*/
public Row readRow(String tableId, String rowKey) {
return ApiExceptions.callAndTranslateApiException(readRowAsync(tableId, rowKey));
}

/**
* Convenience method for asynchronously reading a single row. If the row does not exist, the
* future's value will be null.
Expand All @@ -158,7 +219,9 @@ public static BigtableDataClient create(BigtableDataSettings settings) throws IO
* }
* }
* public void onSuccess(Row result) {
* System.out.println("Got row: " + result);
* if (result != null) {
* System.out.println("Got row: " + result);
* }
* }
* }, MoreExecutors.directExecutor());
* }
Expand Down Expand Up @@ -190,7 +253,9 @@ public ApiFuture<Row> readRowAsync(String tableId, String rowKey) {
* }
* }
* public void onSuccess(Row row) {
* System.out.println("Got row: " + row);
* if (result != null) {
* System.out.println("Got row: " + result);
* }
* }
* }, MoreExecutors.directExecutor());
* }
Expand Down Expand Up @@ -374,6 +439,33 @@ public <RowT> ServerStreamingCallable<Query, RowT> readRowsCallable(RowAdapter<R
return stub.createReadRowsCallable(rowAdapter);
}

/**
* Convenience method to synchronously return a sample of row keys in the table. The returned row
* keys will delimit contiguous sections of the table of approximately equal size, which can be
* used to break up the data for distributed tasks like mapreduces.
*
* <p>Sample code:
*
* <pre>{@code
* InstanceName instanceName = InstanceName.of("[PROJECT]", "[INSTANCE]");
* try (BigtableDataClient bigtableDataClient = BigtableDataClient.create(instanceName)) {
* String tableId = "[TABLE_ID]";
*
* List<KeyOffset> keyOffsets = bigtableDataClient.sampleRowKeys(tableId);
* for(KeyOffset keyOffset : keyOffsets) {
* // Do something with keyOffset
* }
* } catch(ApiException e) {
* e.printStackTrace();
* }
* }</pre>
*
* @throws com.google.api.gax.rpc.ApiException when a serverside error occurs
*/
public List<KeyOffset> sampleRowKeys(String tableId) {
return ApiExceptions.callAndTranslateApiException(sampleRowKeysAsync(tableId));
}

/**
* Convenience method to asynchronously return a sample of row keys in the table. The returned row
* keys will delimit contiguous sections of the table of approximately equal size, which can be
Expand Down Expand Up @@ -447,6 +539,30 @@ public UnaryCallable<String, List<KeyOffset>> sampleRowKeysCallable() {
return stub.sampleRowKeysCallable();
}

/**
* Convenience method to synchronously mutate a single row atomically. Cells already present in
* the row are left unchanged unless explicitly changed by the {@link RowMutation}.
*
* <p>Sample code:
*
* <pre>{@code
* InstanceName instanceName = InstanceName.of("[PROJECT]", "[INSTANCE]");
* try (BigtableDataClient bigtableDataClient = BigtableDataClient.create(instanceName)) {
* RowMutation mutation = RowMutation.create("[TABLE]", "[ROW KEY]")
* .setCell("[FAMILY NAME]", "[QUALIFIER]", "[VALUE]");
*
* bigtableDataClient.mutateRow(mutation);
* } catch(ApiException e) {
* e.printStackTrace();
* }
* }</pre>
*
* @throws com.google.api.gax.rpc.ApiException when a serverside error occurs
*/
public void mutateRow(RowMutation rowMutation) {
ApiExceptions.callAndTranslateApiException(mutateRowAsync(rowMutation));
}

/**
* Convenience method to asynchronously mutate a single row atomically. Cells already present in
* the row are left unchanged unless explicitly changed by the {@link RowMutation}.
Expand Down Expand Up @@ -547,6 +663,35 @@ public BulkMutationBatcher newBulkMutationBatcher() {
* try (BigtableDataClient bigtableDataClient = BigtableDataClient.create(instanceName)) {
* BulkMutation batch = BulkMutation.create("[TABLE]");
* for (String someValue : someCollection) {
* batch.add("[ROW KEY]", Mutation.create().setCell("[FAMILY NAME]", "[QUALIFIER]", "[VALUE]"));
* }
* bigtableDataClient.bulkMutateRows(batch);
* } catch(ApiException e) {
* e.printStackTrace();
* } catch(MutateRowsException e) {
* e.printStackTrace();
* }
* }</pre>
*
* @throws com.google.api.gax.rpc.ApiException when a serverside error occurs
* @throws com.google.cloud.bigtable.data.v2.models.MutateRowsException if any of the entries failed to be applied
*/
public void bulkMutateRows(BulkMutation mutation) {
ApiExceptions.callAndTranslateApiException(bulkMutateRowsAsync(mutation));
}

/**
* Convenience method to mutate multiple rows in a batch. Each individual row is mutated
* atomically as in MutateRow, but the entire batch is not executed atomically. Unlike {@link
* #newBulkMutationBatcher()}, this method expects the mutations to be pre-batched.
*
* <p>Sample code:
*
* <pre>{@code
* InstanceName instanceName = InstanceName.of("[PROJECT]", "[INSTANCE]");
* try (BigtableClient bigtableClient = BigtableClient.create(instanceName)) {
* BulkMutation batch = BulkMutation.create("[TABLE]");
* for (String someValue : someCollection) {
* ApiFuture<Void> entryFuture = batch.add("[ROW KEY]",
* Mutation.create().setCell("[FAMILY NAME]", "[QUALIFIER]", "[VALUE]"));
* }
Expand Down Expand Up @@ -594,6 +739,33 @@ public UnaryCallable<BulkMutation, Void> bulkMutationCallable() {
return stub.bulkMutateRowsCallable();
}

/**
* Convenience method to synchronously mutate a row atomically based on the output of a filter.
*
* <p>Sample code:
*
* <pre>{@code
* InstanceName instanceName = InstanceName.of("[PROJECT]", "[INSTANCE]");
* try (BigtableDataClient bigtableDataClient = BigtableDataClient.create(instanceName)) {
* ConditionalRowMutation mutation = ConditionalRowMutation.create("[TABLE]", "[KEY]")
* .condition(FILTERS.value().regex("old-value"))
* .then(
* Mutation.create()
* .setCell("[FAMILY]", "[QUALIFIER]", "[VALUE]")
* );
*
* Boolean result = bigtableDataClient.checkAndMutateRow(mutation);
* } catch(ApiException e) {
* e.printStackTrace();
* }
* }</pre>
*
* @throws com.google.api.gax.rpc.ApiException when a serverside error occurs
*/
public Boolean checkAndMutateRow(ConditionalRowMutation mutation) {
return ApiExceptions.callAndTranslateApiException(checkAndMutateRowAsync(mutation));
}

/**
* Convenience method to asynchronously mutate a row atomically based on the output of a filter.
*
Expand Down Expand Up @@ -663,6 +835,33 @@ public UnaryCallable<ConditionalRowMutation, Boolean> checkAndMutateRowCallable(
return stub.checkAndMutateRowCallable();
}

/**
* Convenience method that synchronously modifies a row atomically on the server. The method
* reads the latest existing timestamp and value from the specified columns and writes a new
* entry. The new value for the timestamp is the greater of the existing timestamp or the current
* server time. The method returns the new contents of all modified cells.
*
* <p>Sample code:
*
* <pre>{@code
* InstanceName instanceName = InstanceName.of("[PROJECT]", "[INSTANCE]");
* try (BigtableDataClient bigtableDataClient = BigtableDataClient.create(instanceName)) {
* ReadModifyWriteRow mutation = ReadModifyWriteRow.create("[TABLE]", "[KEY]")
* .increment("[FAMILY]", "[QUALIFIER]", 1)
* .append("[FAMILY2]", "[QUALIFIER2]", "suffix");
*
* Row success = bigtableDataClient.readModifyWriteRow(mutation);
* } catch(ApiException e) {
* e.printStackTrace();
* }
* }</pre>
*
* @throws com.google.api.gax.rpc.ApiException when a serverside error occurs
*/
public Row readModifyWriteRow(ReadModifyWriteRow mutation) {
return ApiExceptions.callAndTranslateApiException(readModifyWriteRowAsync(mutation));
}

/**
* Convenience method that asynchronously modifies a row atomically on the server. The method
* reads the latest existing timestamp and value from the specified columns and writes a new
Expand Down
Loading

0 comments on commit 68e6d1a

Please sign in to comment.