public class BigtableDataClient extends Object implements AutoCloseable
This class provides the ability to make remote calls to the backing service. Sample code to get started:
try (BigtableDataClient bigtableDataClient = BigtableDataClient.create("[PROJECT]", "[INSTANCE]")) {
for(Row row : bigtableDataClient.readRows(Query.create("[TABLE]")) {
// Do something with row
}
}
Note: close() needs to be called on the bigtableDataClient object to clean up resources such as threads. In the example above, try-with-resources is used, which automatically calls close().
The surface of this class includes several types of Java methods for each of the API's methods:
All RPC related errors are represented as subclasses of ApiException
. For example, a nonexistent table will trigger a NotFoundException
. Async methods will wrap the error inside the future.
Synchronous methods will re-throw the async error but will try to preserve the caller's
stacktrace by attaching a suppressed exception at the callsite. This allows callers to use
typesafe exceptions, without losing their callsite. Streaming methods (ie. readRows) will
re-throw the async exception (like sync methods) when starting iteration.
See the individual methods for example code.
This class can be customized by passing in a custom instance of BigtableDataSettings to create(). For example:
To customize credentials:
BigtableDataSettings bigtableDataSettings =
BigtableDataSettings.newBuilder()
.setProjectId("[PROJECT]")
.setInstanceId("[INSTANCE]")
.setCredentialsProvider(FixedCredentialsProvider.create(myCredentials))
.build();
try(BigtableDataClient bigtableDataClient = BigtableDataClient.create(bigtableDataSettings)) {
// ..
}
To customize the endpoint:
BigtableDataSettings bigtableDataSettings =
BigtableDataSettings.newBuilder()
.setProjectId("[PROJECT]")
.setInstanceId("[INSTANCE]")
.setEndpoint(myEndpoint).build();
try(BigtableDataClient bigtableDataClient = BigtableDataClient.create(bigtableDataSettings)) {
// ..
}
Modifier and Type | Method and Description |
---|---|
void |
bulkMutateRows(BulkMutation mutation)
Convenience method to mutate multiple rows in a batch.
|
ApiFuture<Void> |
bulkMutateRowsAsync(BulkMutation mutation)
Convenience method to mutate multiple rows in a batch.
|
UnaryCallable<BulkMutation,Void> |
bulkMutationCallable()
Mutates multiple rows in a batch.
|
Boolean |
checkAndMutateRow(ConditionalRowMutation mutation)
Convenience method to synchronously mutate a row atomically based on the output of a filter.
|
ApiFuture<Boolean> |
checkAndMutateRowAsync(ConditionalRowMutation mutation)
Convenience method to asynchronously mutate a row atomically based on the output of a filter.
|
UnaryCallable<ConditionalRowMutation,Boolean> |
checkAndMutateRowCallable()
Mutates a row atomically based on the output of a filter.
|
void |
close()
Close the clients and releases all associated resources.
|
static BigtableDataClient |
create(BigtableDataSettings settings)
Constructs an instance of BigtableDataClient, using the given settings.
|
static BigtableDataClient |
create(String projectId,
String instanceId)
Constructs an instance of BigtableDataClient with default settings.
|
void |
mutateRow(RowMutation rowMutation)
Convenience method to synchronously mutate a single row atomically.
|
ApiFuture<Void> |
mutateRowAsync(RowMutation rowMutation)
Convenience method to asynchronously mutate a single row atomically.
|
UnaryCallable<RowMutation,Void> |
mutateRowCallable()
Mutates a single row atomically.
|
BulkMutationBatcher |
newBulkMutationBatcher()
Mutates multiple rows in a batch.
|
Row |
readModifyWriteRow(ReadModifyWriteRow mutation)
Convenience method that synchronously modifies a row atomically on the server.
|
ApiFuture<Row> |
readModifyWriteRowAsync(ReadModifyWriteRow mutation)
Convenience method that asynchronously modifies a row atomically on the server.
|
UnaryCallable<ReadModifyWriteRow,Row> |
readModifyWriteRowCallable()
Modifies a row atomically on the server.
|
Row |
readRow(String tableId,
ByteString rowKey)
Convenience method for synchronously reading a single row.
|
Row |
readRow(String tableId,
ByteString rowKey,
Filters.Filter filter)
Convenience method for synchronously reading a single row.
|
Row |
readRow(String tableId,
String rowKey)
Convenience method for synchronously reading a single row.
|
Row |
readRow(String tableId,
String rowKey,
Filters.Filter filter)
Convenience method for synchronously reading a single row.
|
ApiFuture<Row> |
readRowAsync(String tableId,
ByteString rowKey)
Convenience method for asynchronously reading a single row.
|
ApiFuture<Row> |
readRowAsync(String tableId,
ByteString rowKey,
Filters.Filter filter)
Convenience method for asynchronously reading a single row.
|
ApiFuture<Row> |
readRowAsync(String tableId,
String rowKey)
Convenience method for asynchronously reading a single row.
|
ApiFuture<Row> |
readRowAsync(String tableId,
String rowKey,
Filters.Filter filter)
Convenience method for asynchronously reading a single row.
|
UnaryCallable<Query,Row> |
readRowCallable()
Reads a single row.
|
<RowT> UnaryCallable<Query,RowT> |
readRowCallable(RowAdapter<RowT> rowAdapter)
Reads a single row.
|
ServerStream<Row> |
readRows(Query query)
Convenience method for synchronously streaming the results of a
Query . |
void |
readRowsAsync(Query query,
ResponseObserver<Row> observer)
Convenience method for asynchronously streaming the results of a
Query . |
ServerStreamingCallable<Query,Row> |
readRowsCallable()
Streams back the results of the query.
|
<RowT> ServerStreamingCallable<Query,RowT> |
readRowsCallable(RowAdapter<RowT> rowAdapter)
Streams back the results of the query.
|
List<KeyOffset> |
sampleRowKeys(String tableId)
Convenience method to synchronously return a sample of row keys in the table.
|
ApiFuture<List<KeyOffset>> |
sampleRowKeysAsync(String tableId)
Convenience method to asynchronously return a sample of row keys in the table.
|
UnaryCallable<String,List<KeyOffset>> |
sampleRowKeysCallable()
Returns a sample of row keys in the table.
|
public static BigtableDataClient create(String projectId, String instanceId) throws IOException
projectId
- The project id of the instance to connect to.instanceId
- The id of the instance to connect to.IOException
- If any.public static BigtableDataClient create(BigtableDataSettings settings) throws IOException
IOException
public Row readRow(String tableId, ByteString rowKey)
Sample code:
{code try (BigtableDataClient bigtableDataClient = BigtableDataClient.create("[PROJECT]", "[INSTANCE]")) { 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.printf("Family: %s Qualifier: %s Value: %s", cell.getFamily(), cell.getQualifier().toStringUtf8(), cell.getValue().toStringUtf8()); } } } catch(ApiException e) { e.printStackTrace(); } }
ApiException
- when a serverside error occurspublic Row readRow(String tableId, String rowKey)
Sample code:
{code try (BigtableDataClient bigtableDataClient = BigtableDataClient.create("[PROJECT]", "[INSTANCE]")) { 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.printf("Family: %s Qualifier: %s Value: %s", cell.getFamily(), cell.getQualifier().toStringUtf8(), cell.getValue().toStringUtf8()); } } } catch(ApiException e) { e.printStackTrace(); } }
ApiException
- when a serverside error occurspublic Row readRow(String tableId, String rowKey, @Nullable Filters.Filter filter)
Sample code:
try (BigtableDataClient bigtableDataClient = BigtableDataClient.create("[PROJECT]", "[INSTANCE]")) {
String tableId = "[TABLE]";
// Build the filter expression
Filter filter = FILTERS.chain()
.filter(FILTERS.qualifier().regex("prefix.*"))
.filter(FILTERS.limit().cellsPerRow(10));
Row row = bigtableDataClient.readRow(tableId, "key", filter);
// 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.printf("Family: %s Qualifier: %s Value: %s", cell.getFamily(),
cell.getQualifier().toStringUtf8(), cell.getValue().toStringUtf8());
}
}
} catch(ApiException e) {
e.printStackTrace();
}
ApiException
- when a serverside error occurspublic Row readRow(String tableId, ByteString rowKey, @Nullable Filters.Filter filter)
Sample code:
try (BigtableDataClient bigtableDataClient = BigtableDataClient.create("[PROJECT]", "[INSTANCE]")) {
String tableId = "[TABLE]";
// Build the filter expression
Filter filter = FILTERS.chain()
.filter(FILTERS.qualifier().regex("prefix.*"))
.filter(FILTERS.limit().cellsPerRow(10));
Row row = bigtableDataClient.readRow(tableId, ByteString.copyFromUtf8("key"), filter);
// 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.printf("Family: %s Qualifier: %s Value: %s", cell.getFamily(),
cell.getQualifier().toStringUtf8(), cell.getValue().toStringUtf8());
}
}
} catch(ApiException e) {
e.printStackTrace();
}
ApiException
- when a serverside error occurspublic ApiFuture<Row> readRowAsync(String tableId, String rowKey)
Sample code:
try (BigtableDataClient bigtableDataClient = BigtableDataClient.create("[PROJECT]", "[INSTANCE]")) {
String tableId = "[TABLE]";
ApiFuture<Row> futureResult = bigtableDataClient.readRowAsync(tableId, "key");
ApiFutures.addCallback(futureResult, new ApiFutureCallback<Row>() {
public void onFailure(Throwable t) {
if (t instanceof NotFoundException) {
System.out.println("Tried to read a non-existent table");
} else {
t.printStackTrace();
}
}
public void onSuccess(Row result) {
if (result != null) {
System.out.println("Got row: " + result);
}
}
}, MoreExecutors.directExecutor());
}
public ApiFuture<Row> readRowAsync(String tableId, ByteString rowKey)
Sample code:
try (BigtableDataClient bigtableDataClient = BigtableDataClient.create("[PROJECT]", "[INSTANCE]")) {
String tableId = "[TABLE]";
ApiFuture<Row> futureResult = bigtableDataClient.readRowAsync(tableId, ByteString.copyFromUtf8("key"));
ApiFutures.addCallback(futureResult, new ApiFutureCallback<Row>() {
public void onFailure(Throwable t) {
if (t instanceof NotFoundException) {
System.out.println("Tried to read a non-existent table");
} else {
t.printStackTrace();
}
}
public void onSuccess(Row row) {
if (result != null) {
System.out.println("Got row: " + result);
}
}
}, MoreExecutors.directExecutor());
}
public ApiFuture<Row> readRowAsync(String tableId, String rowKey, @Nullable Filters.Filter filter)
Sample code:
try (BigtableDataClient bigtableDataClient = BigtableDataClient.create("[PROJECT]", "[INSTANCE]")) {
String tableId = "[TABLE]";
// Build the filter expression
Filters.Filter filter = FILTERS.chain()
.filter(FILTERS.qualifier().regex("prefix.*"))
.filter(FILTERS.limit().cellsPerRow(10));
ApiFuture<Row> futureResult = bigtableDataClient.readRowAsync(tableId, "key", filter);
ApiFutures.addCallback(futureResult, new ApiFutureCallback<Row>() {
public void onFailure(Throwable t) {
if (t instanceof NotFoundException) {
System.out.println("Tried to read a non-existent table");
} else {
t.printStackTrace();
}
}
public void onSuccess(Row row) {
if (result != null) {
System.out.println("Got row: " + result);
}
}
}, MoreExecutors.directExecutor());
}
public ApiFuture<Row> readRowAsync(String tableId, ByteString rowKey, @Nullable Filters.Filter filter)
Sample code:
try (BigtableDataClient bigtableDataClient = BigtableDataClient.create("[PROJECT]", "[INSTANCE]")) {
String tableId = "[TABLE]";
// Build the filter expression
Filters.Filter filter = FILTERS.chain()
.filter(FILTERS.qualifier().regex("prefix.*"))
.filter(FILTERS.limit().cellsPerRow(10));
ApiFuture<Row> futureResult = bigtableDataClient.readRowAsync(tableId, ByteString.copyFromUtf8("key"), filter);
ApiFutures.addCallback(futureResult, new ApiFutureCallback<Row>() {
public void onFailure(Throwable t) {
if (t instanceof NotFoundException) {
System.out.println("Tried to read a non-existent table");
} else {
t.printStackTrace();
}
}
public void onSuccess(Row row) {
if (result != null) {
System.out.println("Got row: " + result);
}
}
}, MoreExecutors.directExecutor());
}
public UnaryCallable<Query,Row> readRowCallable()
Sample code:
try (BigtableDataClient bigtableDataClient = BigtableDataClient.create("[PROJECT]", "[INSTANCE]")) {
String tableId = "[TABLE]";
Query query = Query.create(tableId)
.rowKey("[KEY]")
.filter(FILTERS.qualifier().regex("[COLUMN PREFIX].*"));
// Synchronous invocation
try {
Row row = bigtableDataClient.readRowCallable().call(query);
if (row == null) {
System.out.println("Row not found");
}
} catch (RuntimeException e) {
e.printStackTrace();
}
// Asynchronous invocation
ApiFuture<Row> rowFuture = bigtableDataClient.readRowCallable().futureCall(query);
ApiFutures.addCallback(rowFuture, new ApiFutureCallback<Row>() {
public void onFailure(Throwable t) {
if (t instanceof NotFoundException) {
System.out.println("Tried to read a non-existent table");
} else {
t.printStackTrace();
}
}
public void onSuccess(Row row) {
if (row == null) {
System.out.println("Row not found");
}
}
}, MoreExecutors.directExecutor());
}
public <RowT> UnaryCallable<Query,RowT> readRowCallable(RowAdapter<RowT> rowAdapter)
Sample code:
try (BigtableDataClient bigtableDataClient = BigtableDataClient.create("[PROJECT]", "[INSTANCE]")) {
String tableId = "[TABLE]";
Query query = Query.create(tableId)
.rowKey("[KEY]")
.filter(FILTERS.qualifier().regex("[COLUMN PREFIX].*"));
// Synchronous invocation
CustomRow row = bigtableDataClient.readRowCallable(new CustomRowAdapter()).call(query));
// Do something with row
}
public ServerStream<Row> readRows(Query query)
Query
.
Sample code:
// Import the filter DSL
import static com.google.cloud.bigtable.data.v2.models.Filters.FILTERS;
try (BigtableDataClient bigtableDataClient = BigtableDataClient.create("[PROJECT]", "[INSTANCE]")) {
String tableId = "[TABLE]";
Query query = Query.create(tableId)
.range("[START KEY]", "[END KEY]")
.filter(FILTERS.qualifier().regex("[COLUMN PREFIX].*"));
// Iterator style
try {
for(Row row : bigtableDataClient.readRows(query)) {
// Do something with row
}
} catch (NotFoundException e) {
System.out.println("Tried to read a non-existent table");
} catch (RuntimeException e) {
e.printStackTrace();
}
}
public void readRowsAsync(Query query, ResponseObserver<Row> observer)
Query
.
Sample code:
try (BigtableDataClient bigtableDataClient = BigtableDataClient.create("[PROJECT]", "[INSTANCE]")) {
String tableId = "[TABLE]";
Query query = Query.create(tableId)
.range("[START KEY]", "[END KEY]")
.filter(FILTERS.qualifier().regex("[COLUMN PREFIX].*"));
bigtableDataClient.readRowsAsync(query, new ResponseObserver<Row>() {
public void onStart(StreamController controller) { }
public void onResponse(Row row) {
// Do something with Row
}
public void onError(Throwable t) {
if (t instanceof NotFoundException) {
System.out.println("Tried to read a non-existent table");
} else {
t.printStackTrace();
}
}
public void onComplete() {
// Handle stream completion
}
});
}
public ServerStreamingCallable<Query,Row> readRowsCallable()
Sample code:
try (BigtableDataClient bigtableDataClient = BigtableDataClient.create("[PROJECT]", "[INSTANCE]")) {
String tableId = "[TABLE]";
Query query = Query.create(tableId)
.range("[START KEY]", "[END KEY]")
.filter(FILTERS.qualifier().regex("[COLUMN PREFIX].*"));
// Iterator style
try {
for(Row row : bigtableDataClient.readRowsCallable().call(query)) {
// Do something with row
}
} catch (NotFoundException e) {
System.out.println("Tried to read a non-existent table");
} catch (RuntimeException e) {
e.printStackTrace();
}
// Sync style
try {
List<Row> rows = bigtableDataClient.readRowsCallable().all().call(query);
} catch (NotFoundException e) {
System.out.println("Tried to read a non-existent table");
} catch (RuntimeException e) {
e.printStackTrace();
}
// Point look up
ApiFuture<Row> rowFuture = bigtableDataClient.readRowsCallable().first().futureCall(query);
ApiFutures.addCallback(rowFuture, new ApiFutureCallback<Row>() {
public void onFailure(Throwable t) {
if (t instanceof NotFoundException) {
System.out.println("Tried to read a non-existent table");
} else {
t.printStackTrace();
}
}
public void onSuccess(Row result) {
System.out.println("Got row: " + result);
}
}, MoreExecutors.directExecutor());
// etc
}
public <RowT> ServerStreamingCallable<Query,RowT> readRowsCallable(RowAdapter<RowT> rowAdapter)
Sample code:
try (BigtableDataClient bigtableDataClient = BigtableDataClient.create("[PROJECT]", "[INSTANCE]")) {
String tableId = "[TABLE]";
Query query = Query.create(tableId)
.range("[START KEY]", "[END KEY]")
.filter(FILTERS.qualifier().regex("[COLUMN PREFIX].*"));
// Iterator style
try {
for(CustomRow row : bigtableDataClient.readRowsCallable(new CustomRowAdapter()).call(query)) {
// Do something with row
}
} catch (NotFoundException e) {
System.out.println("Tried to read a non-existent table");
} catch (RuntimeException e) {
e.printStackTrace();
}
}
public List<KeyOffset> sampleRowKeys(String tableId)
Sample code:
try (BigtableDataClient bigtableDataClient = BigtableDataClient.create("[PROJECT]", "[INSTANCE]")) {
String tableId = "[TABLE_ID]";
List<KeyOffset> keyOffsets = bigtableDataClient.sampleRowKeys(tableId);
for(KeyOffset keyOffset : keyOffsets) {
// Do something with keyOffset
}
} catch(ApiException e) {
e.printStackTrace();
}
ApiException
- when a serverside error occurspublic ApiFuture<List<KeyOffset>> sampleRowKeysAsync(String tableId)
Sample code:
try (BigtableClient bigtableDataClient = BigtableClient.create("[PROJECT]", "[INSTANCE]")) {
ApiFuture<List<KeyOffset>> keyOffsetsFuture = bigtableClient.sampleRowKeysAsync("[TABLE]");
ApiFutures.addCallback(keyOffsetsFuture, new ApiFutureCallback<List<KeyOffset>>() {
public void onFailure(Throwable t) {
if (t instanceof NotFoundException) {
System.out.println("Tried to sample keys of a non-existent table");
} else {
t.printStackTrace();
}
}
public void onSuccess(List<KeyOffset> keyOffsets) {
System.out.println("Got key offsets: " + keyOffsets);
}
}, MoreExecutors.directExecutor());
}
public UnaryCallable<String,List<KeyOffset>> sampleRowKeysCallable()
Sample code:
try (BigtableDataClient bigtableDataClient = BigtableDataClient.create("[PROJECT]", "[INSTANCE]")) {
// Synchronous invocation
try {
List<KeyOffset> keyOffsets = bigtableDataClient.sampleRowKeysCallable().call("[TABLE]");
} catch (NotFoundException e) {
System.out.println("Tried to sample keys of a non-existent table");
} catch (RuntimeException e) {
e.printStackTrace();
}
// Asynchronous invocation
ApiFuture<List<KeyOffset>> keyOffsetsFuture = bigtableDataClient.sampleRowKeysCallable().futureCall("[TABLE]");
ApiFutures.addCallback(keyOffsetsFuture, new ApiFutureCallback<List<KeyOffset>>() {
public void onFailure(Throwable t) {
if (t instanceof NotFoundException) {
System.out.println("Tried to sample keys of a non-existent table");
} else {
t.printStackTrace();
}
}
public void onSuccess(List<KeyOffset> keyOffsets) {
System.out.println("Got key offsets: " + keyOffsets);
}
}, MoreExecutors.directExecutor());
}
public void mutateRow(RowMutation rowMutation)
RowMutation
.
Sample code:
try (BigtableDataClient bigtableDataClient = BigtableDataClient.create("[PROJECT]", "[INSTANCE]")) {
RowMutation mutation = RowMutation.create("[TABLE]", "[ROW KEY]")
.setCell("[FAMILY NAME]", "[QUALIFIER]", "[VALUE]");
bigtableDataClient.mutateRow(mutation);
} catch(ApiException e) {
e.printStackTrace();
}
ApiException
- when a serverside error occurspublic ApiFuture<Void> mutateRowAsync(RowMutation rowMutation)
RowMutation
.
Sample code:
try (BigtableDataClient bigtableDataClient = BigtableDataClient.create("[PROJECT]", "[INSTANCE]")) {
RowMutation mutation = RowMutation.create("[TABLE]", "[ROW KEY]")
.setCell("[FAMILY NAME]", "[QUALIFIER]", "[VALUE]");
ApiFuture<Void> future = bigtableDataClient.mutateRowAsync(mutation);
ApiFutures.addCallback(future, new ApiFutureCallback<Void>() {
public void onFailure(Throwable t) {
if (t instanceof NotFoundException) {
System.out.println("Tried to mutate a non-existent table");
} else {
t.printStackTrace();
}
}
public void onSuccess(Void ignored) {
System.out.println("Successfully applied mutation");
}
}, MoreExecutors.directExecutor());
}
public UnaryCallable<RowMutation,Void> mutateRowCallable()
RowMutation
.
Sample code:
try (BigtableDataClient bigtableDataClient = BigtableDataClient.create("[PROJECT]", "[INSTANCE]")) {
RowMutation mutation = RowMutation.create("[TABLE]", "[ROW KEY]")
.setCell("[FAMILY NAME]", "[QUALIFIER]", "[VALUE]");
// Sync style
try {
bigtableDataClient.mutateRowCallable().call(mutation);
} catch (NotFoundException e) {
System.out.println("Tried to mutate a non-existent table");
} catch (RuntimeException e) {
e.printStackTrace();
}
}
@BetaApi(value="This surface is likely to change as the batching surface evolves.") public BulkMutationBatcher newBulkMutationBatcher()
Sample code:
try (BigtableDataClient bigtableDataClient = BigtableDataClient.create("[PROJECT]", "[INSTANCE]")) {
try (BulkMutationBatcher batcher = bigtableDataClient.newBulkMutationBatcher()) {
for (String someValue : someCollection) {
RowMutation mutation = RowMutation.create("[TABLE]", "[ROW KEY]")
.setCell("[FAMILY NAME]", "[QUALIFIER]", "[VALUE]");
ApiFuture<Void> entryFuture = batcher.add(mutation);
}
} catch (BulkMutationFailure failure) {
// Handle error
}
// After `batcher` is closed, all mutations have been applied
}
public void bulkMutateRows(BulkMutation mutation)
newBulkMutationBatcher()
, this method expects the mutations to be pre-batched.
Sample code:
try (BigtableDataClient bigtableDataClient = BigtableDataClient.create("[PROJECT]", "[INSTANCE]")) {
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();
}
ApiException
- when a serverside error occursMutateRowsException
- if any of the entries
failed to be appliedpublic ApiFuture<Void> bulkMutateRowsAsync(BulkMutation mutation)
newBulkMutationBatcher()
, this method expects the mutations to be pre-batched.
Sample code:
try (BigtableClient bigtableClient = BigtableClient.create("[PROJECT]", "[INSTANCE]")) {
BulkMutation batch = BulkMutation.create("[TABLE]");
for (String someValue : someCollection) {
ApiFuture<Void> entryFuture = batch.add("[ROW KEY]",
Mutation.create().setCell("[FAMILY NAME]", "[QUALIFIER]", "[VALUE]"));
}
ApiFuture<Void> resultFuture = bigtableDataClient.bulkMutateRowsAsync(batch);
ApiFutures.addCallback(resultFuture, new ApiFutureCallback<Void>() {
public void onFailure(Throwable t) {
if (t instanceof BulkMutationFailure) {
System.out.println("Some entries failed to apply");
} else {
t.printStackTrace();
}
}
public void onSuccess(Void ignored) {
System.out.println("Successfully applied all mutation");
}
}, MoreExecutors.directExecutor());
}
public UnaryCallable<BulkMutation,Void> bulkMutationCallable()
newBulkMutationBatcher()
, this
method expects the mutations to be pre-batched.
Sample code:
try (BigtableDataClient bigtableDataClient = BigtableDataClient.create("[PROJECT]", "[INSTANCE]")) {
BulkMutation batch = BulkMutation.create("[TABLE]");
for (String someValue : someCollection) {
ApiFuture<Void> entryFuture = batch.add("[ROW KEY]",
Mutation.create().setCell("[FAMILY NAME]", "[QUALIFIER]", "[VALUE]");
}
bigtableDataClient.bulkMutationCallable().call(batch);
}
public Boolean checkAndMutateRow(ConditionalRowMutation mutation)
Sample code:
try (BigtableDataClient bigtableDataClient = BigtableDataClient.create("[PROJECT]", "[INSTANCE]")) {
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();
}
ApiException
- when a serverside error occurspublic ApiFuture<Boolean> checkAndMutateRowAsync(ConditionalRowMutation mutation)
Sample code:
try (BigtableDataClient bigtableDataClient = BigtableDataClient.create("[PROJECT]", "[INSTANCE]")) {
ConditionalRowMutation mutation = ConditionalRowMutation.create("[TABLE]", "[KEY]")
.condition(FILTERS.value().regex("old-value"))
.then(
Mutation.create()
.setCell("[FAMILY]", "[QUALIFIER]", "[VALUE]")
);
ApiFuture<Boolean> future = bigtableDataClient.checkAndMutateRowAsync(mutation);
ApiFutures.addCallback(future, new ApiFutureCallback<Boolean>() {
public void onFailure(Throwable t) {
if (t instanceof NotFoundException) {
System.out.println("Tried to mutate a non-existent table");
} else {
t.printStackTrace();
}
}
public void onSuccess(Boolean wasApplied) {
System.out.println("Row was modified: " + wasApplied);
}
}, MoreExecutors.directExecutor());
}
public UnaryCallable<ConditionalRowMutation,Boolean> checkAndMutateRowCallable()
Sample code:
try (BigtableDataClient bigtableDataClient = BigtableDataClient.create("[PROJECT]", "[INSTANCE]")) {
ConditionalRowMutation mutation = ConditionalRowMutation.create("[TABLE]", "[KEY]")
.condition(FILTERS.value().regex("old-value"))
.then(
Mutation.create()
.setCell("[FAMILY]", "[QUALIFIER]", "[VALUE]")
);
// Sync style
try {
boolean success = bigtableDataClient.checkAndMutateRowCallable().call(mutation);
if (!success) {
System.out.println("Row did not match conditions");
}
} catch (NotFoundException e) {
System.out.println("Tried to mutate a non-existent table");
} catch (RuntimeException e) {
e.printStackTrace();
}
}
public Row readModifyWriteRow(ReadModifyWriteRow mutation)
Sample code:
try (BigtableDataClient bigtableDataClient = BigtableDataClient.create("[PROJECT]", "[INSTANCE]")) {
ReadModifyWriteRow mutation = ReadModifyWriteRow.create("[TABLE]", "[KEY]")
.increment("[FAMILY]", "[QUALIFIER]", 1)
.append("[FAMILY2]", "[QUALIFIER2]", "suffix");
Row success = bigtableDataClient.readModifyWriteRow(mutation);
} catch(ApiException e) {
e.printStackTrace();
}
ApiException
- when a serverside error occurspublic ApiFuture<Row> readModifyWriteRowAsync(ReadModifyWriteRow mutation)
Sample code:
try (BigtableDataClient bigtableDataClient = BigtableDataClient.create("[PROJECT]", "[INSTANCE]")) {
ReadModifyWriteRow mutation = ReadModifyWriteRow.create("[TABLE]", "[KEY]")
.increment("[FAMILY]", "[QUALIFIER]", 1)
.append("[FAMILY2]", "[QUALIFIER2]", "suffix");
ApiFuture<Row> rowFuture = bigtableDataClient.readModifyWriteRowAsync(mutation);
ApiFutures.addCallback(rowFuture, new ApiFutureCallback<Row>() {
public void onFailure(Throwable t) {
if (t instanceof NotFoundException) {
System.out.println("Tried to mutate a non-existent table");
} else {
t.printStackTrace();
}
}
public void onSuccess(Row resultingRow) {
System.out.println("Resulting row: " + resultingRow);
}
}, MoreExecutors.directExecutor());
}
public UnaryCallable<ReadModifyWriteRow,Row> readModifyWriteRowCallable()
Sample code:
try (BigtableDataClient bigtableDataClient = BigtableDataClient.create("[PROJECT]", "[INSTANCE]")) {
ReadModifyWriteRow mutation = ReadModifyWriteRow.create("[TABLE]", "[KEY]")
.increment("[FAMILY]", "[QUALIFIER]", 1)
.append("[FAMILY2]", "[QUALIFIER2]", "suffix");
try {
Row row = bigtableDataClient.readModifyWriteRowCallable().call(mutation);
} catch (NotFoundException e) {
System.out.println("Tried to mutate a non-existent table");
} catch (RuntimeException e) {
e.printStackTrace();
}
}
public void close() throws Exception
close
in interface AutoCloseable
Exception
Copyright © 2019 Google LLC. All rights reserved.