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:
InstanceName instanceName = InstanceName.of("[PROJECT]", "[INSTANCE]");
try (BigtableDataClient bigtableDataClient = BigtableDataClient.create(instanceName)) {
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:
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()
.setInstanceName(InstanceName.of("[PROJECT]", "[INSTANCE]"))
.setCredentialsProvider(FixedCredentialsProvider.create(myCredentials))
.build();
try(BigtableDataClient bigtableDataClient = BigtableDataClient.create(bigtableDataSettings)) {
// ..
}
To customize the endpoint:
BigtableDataSettings bigtableDataSettings =
BigtableDataSettings.newBuilder()
.setInstanceName(InstanceName.of("[PROJECT]", "[INSTANCE]"))
.setEndpoint(myEndpoint).build();
try(BigtableDataClient bigtableDataClient = BigtableDataClient.create(bigtableDataSettings)) {
// ..
}
Modifier and Type | Method and Description |
---|---|
com.google.api.core.ApiFuture<Void> |
bulkMutateRowsAsync(BulkMutation mutation)
Convenience method to mutate multiple rows in a batch.
|
com.google.api.gax.rpc.UnaryCallable<BulkMutation,Void> |
bulkMutationCallable()
Mutates multiple rows in a batch.
|
com.google.api.core.ApiFuture<Boolean> |
checkAndMutateRowAsync(ConditionalRowMutation mutation)
Convenience method to asynchronously mutate a row atomically based on the output of a filter.
|
com.google.api.gax.rpc.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(InstanceName instanceName)
Constructs an instance of BigtableClient with default settings.
|
com.google.api.core.ApiFuture<Void> |
mutateRowAsync(RowMutation rowMutation)
Convenience method to asynchronously mutate a single row atomically.
|
com.google.api.gax.rpc.UnaryCallable<RowMutation,Void> |
mutateRowCallable()
Mutates a single row atomically.
|
BulkMutationBatcher |
newBulkMutationBatcher()
Mutates multiple rows in a batch.
|
com.google.api.core.ApiFuture<Row> |
readModifyWriteRowAsync(ReadModifyWriteRow mutation)
Convenience method that asynchronously modifies a row atomically on the server.
|
com.google.api.gax.rpc.UnaryCallable<ReadModifyWriteRow,Row> |
readModifyWriteRowCallable()
Modifies a row atomically on the server.
|
com.google.api.core.ApiFuture<Row> |
readRowAsync(String tableId,
com.google.protobuf.ByteString rowKey)
Convenience method for asynchronously reading a single row.
|
com.google.api.core.ApiFuture<Row> |
readRowAsync(String tableId,
String rowKey)
Convenience method for asynchronously reading a single row.
|
com.google.api.gax.rpc.ServerStream<Row> |
readRows(Query query)
Convenience method for synchronous streaming the results of a
Query . |
void |
readRowsAsync(Query query,
com.google.api.gax.rpc.ResponseObserver<Row> observer)
Convenience method for asynchronous streaming the results of a
Query . |
com.google.api.gax.rpc.ServerStreamingCallable<Query,Row> |
readRowsCallable()
Streams back the results of the query.
|
<RowT> com.google.api.gax.rpc.ServerStreamingCallable<Query,RowT> |
readRowsCallable(RowAdapter<RowT> rowAdapter)
Streams back the results of the query.
|
com.google.api.core.ApiFuture<List<KeyOffset>> |
sampleRowKeysAsync(String tableId)
Convenience method to asynchronously return a sample of row keys in the table.
|
com.google.api.gax.rpc.UnaryCallable<String,List<KeyOffset>> |
sampleRowKeysCallable()
Returns a sample of row keys in the table.
|
public static BigtableDataClient create(InstanceName instanceName) throws IOException
instanceName
- The instance to connect to.IOException
- If any.public static BigtableDataClient create(BigtableDataSettings settings) throws IOException
IOException
public com.google.api.core.ApiFuture<Row> readRowAsync(String tableId, String rowKey)
Sample code:
InstanceName instanceName = InstanceName.of("[PROJECT]", "[INSTANCE]");
try (BigtableClient bigtableClient = BigtableClient.create(instanceName)) {
String tableId = "[TABLE]";
ApiFuture<Row> result = bigtableClient.readRow(tableId, "key");
}
public com.google.api.core.ApiFuture<Row> readRowAsync(String tableId, com.google.protobuf.ByteString rowKey)
Sample code:
InstanceName instanceName = InstanceName.of("[PROJECT]", "[INSTANCE]");
try (BigtableClient bigtableClient = BigtableClient.create(instanceName)) {
String tableId = "[TABLE]";
ApiFuture<Row> result = bigtableClient.readRow(tableId, ByteString.copyFromUtf8("key"));
}
public com.google.api.gax.rpc.ServerStream<Row> readRows(Query query)
Query
.
Sample code:
// Import the filter DSL
import static com.google.cloud.bigtable.data.v2.models.Filters.FILTERS;
InstanceName instanceName = InstanceName.of("[PROJECT]", "[INSTANCE]");
try (BigtableClient bigtableClient = BigtableClient.create(instanceName)) {
String tableId = "[TABLE]";
Query query = Query.create(tableId)
.range("[START KEY]", "[END KEY]")
.filter(FILTERS.qualifier().regex("[COLUMN PREFIX].*"));
// Iterator style
for(Row row : bigtableClient.readRows(query)) {
// Do something with row
}
}
For call styles.
,
For query options.
,
For the filter building DSL.
public void readRowsAsync(Query query, com.google.api.gax.rpc.ResponseObserver<Row> observer)
Query
.
Sample code:
InstanceName instanceName = InstanceName.of("[PROJECT]", "[INSTANCE]");
try (BigtableClient bigtableClient = BigtableClient.create(instanceName)) {
String tableId = "[TABLE]";
Query query = Query.create(tableId)
.range("[START KEY]", "[END KEY]")
.filter(FILTERS.qualifier().regex("[COLUMN PREFIX].*"));
client.readRowsAsync(query, new ResponseObserver<Row>() {
public void onStart(StreamController controller) { }
public void onResponse(Row response) {
// Do something with Row
}
public void onError(Throwable t) {
// Handle error before the stream completes
}
public void onComplete() {
// Handle stream completion
}
});
}
public com.google.api.gax.rpc.ServerStreamingCallable<Query,Row> readRowsCallable()
Sample code:
InstanceName instanceName = InstanceName.of("[PROJECT]", "[INSTANCE]");
try (BigtableClient bigtableClient = BigtableClient.create(instanceName)) {
String tableId = "[TABLE]";
Query query = Query.create(tableId)
.range("[START KEY]", "[END KEY]")
.filter(FILTERS.qualifier().regex("[COLUMN PREFIX].*"));
// Iterator style
for(Row row : bigtableClient.readRowsCallable().call(query)) {
// Do something with row
}
// Point look up
ApiFuture<Row> rowFuture = bigtableClient.readRowsCallable().first().futureCall(query);
// etc
}
For call styles.
,
For query options.
,
For the filter building DSL.
public <RowT> com.google.api.gax.rpc.ServerStreamingCallable<Query,RowT> readRowsCallable(RowAdapter<RowT> rowAdapter)
Sample code:
InstanceName instanceName = InstanceName.of("[PROJECT]", "[INSTANCE]");
try (BigtableClient bigtableClient = BigtableClient.create(instanceName)) {
String tableId = "[TABLE]";
Query query = Query.create(tableId)
.range("[START KEY]", "[END KEY]")
.filter(FILTERS.qualifier().regex("[COLUMN PREFIX].*"));
// Iterator style
for(CustomRow row : bigtableClient.readRowsCallable(new CustomRowAdapter()).call(query)) {
// Do something with row
}
}
For call styles.
,
For query options.
,
For the filter building DSL.
public com.google.api.core.ApiFuture<List<KeyOffset>> sampleRowKeysAsync(String tableId)
Sample code:
InstanceName instanceName = InstanceName.of("[PROJECT]", "[INSTANCE]");
try (BigtableClient bigtableClient = BigtableClient.create(instanceName)) {
ApiFuture<List<KeyOffset>> keyOffsets = bigtableClient.sampleRowKeysAsync("[TABLE]");
}
public com.google.api.gax.rpc.UnaryCallable<String,List<KeyOffset>> sampleRowKeysCallable()
Sample code:
InstanceName instanceName = InstanceName.of("[PROJECT]", "[INSTANCE]");
try (BigtableClient bigtableClient = BigtableClient.create(instanceName)) {
// Synchronous invocation
List<KeyOffset> keyOffsets = bigtableClient.sampleRowKeysCallable().call("[TABLE]");
// Asynchronous invocation
ApiFuture<List<KeyOffset>> keyOffsets = bigtableClient.sampleRowKeysCallable().futureCall("[TABLE]");
}
public com.google.api.core.ApiFuture<Void> mutateRowAsync(RowMutation rowMutation)
RowMutation
.
Sample code:
InstanceName instanceName = InstanceName.of("[PROJECT]", "[INSTANCE]");
try (BigtableClient bigtableClient = BigtableClient.create(instanceName)) {
RowMutation mutation = RowMutation.create("[TABLE]", "[ROW KEY]")
.setCell("[FAMILY NAME]", "[QUALIFIER]", "[VALUE]");
ApitFuture<Void> future = bigtableClient.mutateRowAsync(mutation);
}
public com.google.api.gax.rpc.UnaryCallable<RowMutation,Void> mutateRowCallable()
RowMutation
.
Sample code:
InstanceName instanceName = InstanceName.of("[PROJECT]", "[INSTANCE]");
try (BigtableClient bigtableClient = BigtableClient.create(instanceName)) {
RowMutation mutation = RowMutation.create("[TABLE]", "[ROW KEY]")
.setCell("[FAMILY NAME]", "[QUALIFIER]", "[VALUE]");
bigtableClient.mutateRowCallable().call(mutation);
}
@BetaApi(value="This surface is likely to change as the batching surface evolves.") public BulkMutationBatcher newBulkMutationBatcher()
Sample code:
InstanceName instanceName = InstanceName.of("[PROJECT]", "[INSTANCE]");
try (BigtableClient bigtableClient = BigtableClient.create(instanceName)) {
try (BulkMutationBatcher batcher = bigtableClient.newBulkMutationBatcher()) {
for (String someValue : someCollection) {
RowMutation mutation = RowMutation.create("[TABLE]", "[ROW KEY]")
.setCell("[FAMILY NAME]", "[QUALIFIER]", "[VALUE]");
batcher.add(mutation);
}
} catch (BulkMutationFailure failure) {
// Handle error
}
// After `batcher` is closed, all mutations have been applied
}
public com.google.api.core.ApiFuture<Void> bulkMutateRowsAsync(BulkMutation mutation)
newBulkMutationBatcher()
, this method expects the mutations to be pre-batched.
Sample code:
InstanceName instanceName = InstanceName.of("[PROJECT]", "[INSTANCE]");
try (BigtableClient bigtableClient = BigtableClient.create(instanceName)) {
BulkMutation batch = BulkMutation.create("[TABLE]");
for (String someValue : someCollection) {
batch.add("[ROW KEY]", Mutation.create().setCell("[FAMILY NAME]", "[QUALIFIER]", "[VALUE]");
}
ApiFuture<Void> result = bigtableClient.bulkMutateRowsAsync(batch);
}
public com.google.api.gax.rpc.UnaryCallable<BulkMutation,Void> bulkMutationCallable()
newBulkMutationBatcher()
, this
method expects the mutations to be pre-batched.
Sample code:
InstanceName instanceName = InstanceName.of("[PROJECT]", "[INSTANCE]");
try (BigtableClient bigtableClient = BigtableClient.create(instanceName)) {
BulkMutation batch = BulkMutation.create("[TABLE]");
for (String someValue : someCollection) {
batch.add("[ROW KEY]", Mutation.create().setCell("[FAMILY NAME]", "[QUALIFIER]", "[VALUE]");
}
bigtableClient.bulkMutateRowsCallable().call(batch);
}
public com.google.api.core.ApiFuture<Boolean> checkAndMutateRowAsync(ConditionalRowMutation mutation)
Sample code:
InstanceName instanceName = InstanceName.of("[PROJECT]", "[INSTANCE]");
try (BigtableClient bigtableClient = BigtableClient.create(instanceName)) {
ConditionalRowMutation mutation = ConditionalRowMutation.create("[TABLE]", "[KEY]")
.condition(FILTERS.value().regex("old-value"))
.then(
Mutation.create()
.setCell("[FAMILY]", "[QUALIFIER]", "[VALUE]")
);
ApiFuture<Boolean> future = bigtableClient.checkAndMutateRowAsync(mutation);
}
public com.google.api.gax.rpc.UnaryCallable<ConditionalRowMutation,Boolean> checkAndMutateRowCallable()
Sample code:
InstanceName instanceName = InstanceName.of("[PROJECT]", "[INSTANCE]");
try (BigtableClient bigtableClient = BigtableClient.create(instanceName)) {
ConditionalRowMutation mutation = ConditionalRowMutation.create("[TABLE]", "[KEY]")
.condition(FILTERS.value().regex("old-value"))
.then(
Mutation.create()
.setCell("[FAMILY]", "[QUALIFIER]", "[VALUE]")
);
boolean success = bigtableClient.checkAndMutateRowCallable().call(mutation);
}
public com.google.api.core.ApiFuture<Row> readModifyWriteRowAsync(ReadModifyWriteRow mutation)
Sample code:
InstanceName instanceName = InstanceName.of("[PROJECT]", "[INSTANCE]");
try (BigtableClient bigtableClient = BigtableClient.create(instanceName)) {
ReadModifyWriteRow mutation = ReadModifyWriteRow.create("[TABLE]", "[KEY]")
.increment("[FAMILY]", "[QUALIFIER]", 1)
.append("[FAMILY2]", "[QUALIFIER2]", "suffix");
ApiFuture<Row> success = bigtableClient.readModifyWriteRowAsync(mutation);
}
public com.google.api.gax.rpc.UnaryCallable<ReadModifyWriteRow,Row> readModifyWriteRowCallable()
Sample code:
InstanceName instanceName = InstanceName.of("[PROJECT]", "[INSTANCE]");
try (BigtableClient bigtableClient = BigtableClient.create(instanceName)) {
ReadModifyWriteRow mutation = ReadModifyWriteRow.create("[TABLE]", "[KEY]")
.increment("[FAMILY]", "[QUALIFIER]", 1)
.append("[FAMILY2]", "[QUALIFIER2]", "suffix");
Row row = bigtableClient.readModifyWriteRowCallable().call(mutation);
}
public void close() throws Exception
close
in interface AutoCloseable
Exception
Copyright © 2018 Google LLC. All rights reserved.