Search in sources :

Example 36 with Query

use of com.google.cloud.bigtable.data.v2.models.Query in project java-bigtable by googleapis.

the class EnhancedBigtableStub method createBulkReadRowsCallable.

/**
 * Creates a callable chain to handle bulk ReadRows RPCs. This is meant to be used in ReadRows
 * batcher. The chain will:
 *
 * <ul>
 *   <li>Convert a {@link Query} into a {@link com.google.bigtable.v2.ReadRowsRequest}.
 *   <li>Upon receiving the response stream, it will merge the {@link
 *       com.google.bigtable.v2.ReadRowsResponse.CellChunk}s in logical rows. The actual row
 *       implementation can be configured in by the {@code rowAdapter} parameter.
 *   <li>Retry/resume on failure.
 *   <li>Filter out marker rows.
 *   <li>Construct a {@link UnaryCallable} that will buffer the entire stream into memory before
 *       completing. If the stream is empty, then the list will be empty.
 *   <li>Add tracing & metrics.
 * </ul>
 */
private <RowT> UnaryCallable<Query, List<RowT>> createBulkReadRowsCallable(RowAdapter<RowT> rowAdapter) {
    ServerStreamingCallable<ReadRowsRequest, RowT> readRowsCallable = createReadRowsBaseCallable(settings.readRowsSettings(), rowAdapter);
    ServerStreamingCallable<Query, RowT> readRowsUserCallable = new ReadRowsUserCallable<>(readRowsCallable, requestContext);
    SpanName span = getSpanName("ReadRows");
    // The TracedBatcherUnaryCallable has to be wrapped by the TracedUnaryCallable, so that
    // TracedUnaryCallable can inject a tracer for the TracedBatcherUnaryCallable to interact with
    UnaryCallable<Query, List<RowT>> tracedBatcher = new TracedBatcherUnaryCallable<>(readRowsUserCallable.all());
    UnaryCallable<Query, List<RowT>> withHeaderTracer = new HeaderTracerUnaryCallable(tracedBatcher);
    UnaryCallable<Query, List<RowT>> traced = new TracedUnaryCallable<>(withHeaderTracer, clientContext.getTracerFactory(), span);
    return traced.withDefaultCallContext(clientContext.getDefaultCallContext());
}
Also used : ReadRowsUserCallable(com.google.cloud.bigtable.data.v2.stub.readrows.ReadRowsUserCallable) SpanName(com.google.api.gax.tracing.SpanName) TracedUnaryCallable(com.google.api.gax.tracing.TracedUnaryCallable) HeaderTracerUnaryCallable(com.google.cloud.bigtable.data.v2.stub.metrics.HeaderTracerUnaryCallable) Query(com.google.cloud.bigtable.data.v2.models.Query) ReadRowsRequest(com.google.bigtable.v2.ReadRowsRequest) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) TracedBatcherUnaryCallable(com.google.cloud.bigtable.data.v2.stub.metrics.TracedBatcherUnaryCallable)

Example 37 with Query

use of com.google.cloud.bigtable.data.v2.models.Query in project java-bigtable by googleapis.

the class BigtableDataClient method existsAsync.

/**
 * Confirms asynchronously if given row key exists or not.
 *
 * <p>Sample code:
 *
 * <pre>{@code
 * try (BigtableDataClient bigtableDataClient = BigtableDataClient.create("[PROJECT]", "[INSTANCE]")) {
 *   String tableId = "[TABLE]";
 *   final ByteString key = ByteString.copyFromUtf8("key");
 *
 *   ApiFuture<Boolean> futureResult = bigtableDataClient.existsAsync(tableId, key);
 *
 *   ApiFutures.addCallback(futureResult, new ApiFutureCallback<Boolean>() {
 *     public void onFailure(Throwable t) {
 *       t.printStackTrace();
 *     }
 *     public void onSuccess(Boolean isRowPresent) {
 *       if(isRowPresent) {
 *         System.out.println(key.toStringUtf8() + " is present");
 *       }
 *     }
 *   }, MoreExecutors.directExecutor());
 * }
 * }</pre>
 */
public ApiFuture<Boolean> existsAsync(String tableId, ByteString rowKey) {
    Query query = Query.create(tableId).rowKey(rowKey).filter(FILTERS.chain().filter(FILTERS.limit().cellsPerRow(1)).filter(FILTERS.value().strip()));
    ApiFuture<Row> resultFuture = stub.readRowCallable().futureCall(query);
    return ApiFutures.transform(resultFuture, new ApiFunction<Row, Boolean>() {

        @Override
        public Boolean apply(Row row) {
            return row != null;
        }
    }, MoreExecutors.directExecutor());
}
Also used : Query(com.google.cloud.bigtable.data.v2.models.Query) Row(com.google.cloud.bigtable.data.v2.models.Row) ReadModifyWriteRow(com.google.cloud.bigtable.data.v2.models.ReadModifyWriteRow)

Example 38 with Query

use of com.google.cloud.bigtable.data.v2.models.Query in project java-bigtable by googleapis.

the class Reads method readPrefix.

public static void readPrefix(String projectId, String instanceId, String tableId) {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
        Query query = Query.create(tableId).prefix("phone");
        ServerStream<Row> rows = dataClient.readRows(query);
        for (Row row : rows) {
            printRow(row);
        }
    } catch (IOException e) {
        System.out.println("Unable to initialize service client, as a network error occurred: \n" + e.toString());
    }
}
Also used : Query(com.google.cloud.bigtable.data.v2.models.Query) Row(com.google.cloud.bigtable.data.v2.models.Row) IOException(java.io.IOException) BigtableDataClient(com.google.cloud.bigtable.data.v2.BigtableDataClient)

Example 39 with Query

use of com.google.cloud.bigtable.data.v2.models.Query in project native-image-support-java by GoogleCloudPlatform.

the class BigTableSampleApplication method readData.

private static void readData(BigtableDataClient client, String tableId) {
    Query query = Query.create(tableId).prefix("");
    ServerStream<Row> rows = client.readRows(query);
    System.out.println("Reading phone data in table: ");
    for (Row row : rows) {
        System.out.println("Key: " + row.getKey().toStringUtf8());
        for (RowCell cell : row.getCells()) {
            System.out.printf("\t%s: %s @%s%n", cell.getQualifier().toStringUtf8(), cell.getValue().toStringUtf8(), cell.getTimestamp());
        }
        System.out.println();
    }
}
Also used : Query(com.google.cloud.bigtable.data.v2.models.Query) RowCell(com.google.cloud.bigtable.data.v2.models.RowCell) Row(com.google.cloud.bigtable.data.v2.models.Row)

Aggregations

Query (com.google.cloud.bigtable.data.v2.models.Query)34 Row (com.google.cloud.bigtable.data.v2.models.Row)24 Test (org.junit.Test)20 ReadRowsRequest (com.google.bigtable.v2.ReadRowsRequest)7 BigtableDataClient (com.google.cloud.bigtable.data.v2.BigtableDataClient)7 IOException (java.io.IOException)7 ReadModifyWriteRow (com.google.cloud.bigtable.data.v2.models.ReadModifyWriteRow)5 ByteString (com.google.protobuf.ByteString)5 Result (org.apache.hadoop.hbase.client.Result)5 GrpcCallContext (com.google.api.gax.grpc.GrpcCallContext)4 Filters (com.google.cloud.bigtable.data.v2.models.Filters)4 RowCell (com.google.cloud.bigtable.data.v2.models.RowCell)4 DefaultReadHooks (com.google.cloud.bigtable.hbase.adapters.read.DefaultReadHooks)3 ReadHooks (com.google.cloud.bigtable.hbase.adapters.read.ReadHooks)3 SpanName (com.google.api.gax.tracing.SpanName)2 BulkMutation (com.google.cloud.bigtable.data.v2.models.BulkMutation)2 DefaultRowAdapter (com.google.cloud.bigtable.data.v2.models.DefaultRowAdapter)2 ReadRowsUserCallable (com.google.cloud.bigtable.data.v2.stub.readrows.ReadRowsUserCallable)2 ServerStreamingStashCallable (com.google.cloud.bigtable.gaxx.testing.FakeStreamingApi.ServerStreamingStashCallable)2 Scan (org.apache.hadoop.hbase.client.Scan)2