Search in sources :

Example 1 with Query

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

the class CassandraMigrationCodelab method scan.

public void scan() {
    try {
        Query query = Query.create(tableId).range("tablet#a0b81f74#201905", "tablet#a0b81f74#201906");
        ServerStream<Row> rowStream = dataClient.readRows(query);
        for (Row row : rowStream) {
            System.out.println("Row Key: " + row.getKey().toStringUtf8());
            for (RowCell cell : row.getCells()) {
                System.out.printf("Family: %s    Qualifier: %s    Value: %s    Timestamp: %s%n", cell.getFamily(), cell.getQualifier().toStringUtf8(), cell.getValue().toStringUtf8(), cell.getTimestamp());
            }
        }
    } catch (Exception e) {
        System.out.println("Error during scan: \n" + e.toString());
    }
}
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)

Example 2 with Query

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

the class HBaseRequestAdapter method adapt.

/**
 * adapt.
 *
 * @param scan a {@link Scan} object.
 * @return a {@link Query} object.
 */
public Query adapt(Scan scan) {
    ReadHooks readHooks = new DefaultReadHooks();
    Query query = Query.create(getTableId());
    Adapters.SCAN_ADAPTER.adapt(scan, readHooks, query);
    readHooks.applyPreSendHook(query);
    return query;
}
Also used : ReadHooks(com.google.cloud.bigtable.hbase.adapters.read.ReadHooks) DefaultReadHooks(com.google.cloud.bigtable.hbase.adapters.read.DefaultReadHooks) Query(com.google.cloud.bigtable.data.v2.models.Query) DefaultReadHooks(com.google.cloud.bigtable.hbase.adapters.read.DefaultReadHooks)

Example 3 with Query

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

the class RowRangeAdapter method rangeSetToByteStringRange.

/**
 * Convert guava's {@link RangeSet} to Bigtable's {@link ByteStringRange}. Please note that this
 * will convert boundless ranges into unset key cases.
 */
@VisibleForTesting
void rangeSetToByteStringRange(RangeSet<RowKeyWrapper> guavaRangeSet, Query query) {
    for (Range<RowKeyWrapper> guavaRange : guavaRangeSet.asRanges()) {
        // Is it a point?
        if (guavaRange.hasLowerBound() && guavaRange.lowerBoundType() == BoundType.CLOSED && guavaRange.hasUpperBound() && guavaRange.upperBoundType() == BoundType.CLOSED && guavaRange.lowerEndpoint().equals(guavaRange.upperEndpoint())) {
            query.rowKey(guavaRange.lowerEndpoint().getKey());
        } else {
            ByteStringRange byteRange = ByteStringRange.unbounded();
            // Handle start key
            if (guavaRange.hasLowerBound()) {
                switch(guavaRange.lowerBoundType()) {
                    case CLOSED:
                        byteRange.startClosed(guavaRange.lowerEndpoint().getKey());
                        break;
                    case OPEN:
                        byteRange.startOpen(guavaRange.lowerEndpoint().getKey());
                        break;
                    default:
                        throw new IllegalArgumentException("Unexpected lower bound type: " + guavaRange.lowerBoundType());
                }
            }
            // handle end key
            if (guavaRange.hasUpperBound()) {
                switch(guavaRange.upperBoundType()) {
                    case CLOSED:
                        byteRange.endClosed(guavaRange.upperEndpoint().getKey());
                        break;
                    case OPEN:
                        byteRange.endOpen(guavaRange.upperEndpoint().getKey());
                        break;
                    default:
                        throw new IllegalArgumentException("Unexpected upper bound type: " + guavaRange.upperBoundType());
                }
            }
            query.range(byteRange);
        }
    }
}
Also used : ByteStringRange(com.google.cloud.bigtable.data.v2.models.Range.ByteStringRange) RowKeyWrapper(com.google.cloud.bigtable.hbase.util.RowKeyWrapper) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 4 with Query

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

the class TestScanAdapter method testColFamilyTimeRange.

@Test
public void testColFamilyTimeRange() throws IOException {
    String colFamily1 = "cf1", colFamily2 = "cf2";
    long rangeStart = 10000L, rangeEnd = 99999L, secRangeStart = 100L, secRangeEnd = 999L;
    Scan scan = new Scan().setColumnFamilyTimeRange(colFamily1.getBytes(), rangeStart, rangeEnd).setColumnFamilyTimeRange(colFamily2.getBytes(), secRangeStart, secRangeEnd);
    scanAdapter.adapt(scan, throwingReadHooks, query);
    Filters.Filter expected = FILTERS.chain().filter(FILTERS.limit().cellsPerColumn(1)).filter(FILTERS.interleave().filter(FILTERS.chain().filter(FILTERS.family().regex(colFamily1)).filter(FILTERS.timestamp().range().of(rangeStart * 1000, rangeEnd * 1000))).filter(FILTERS.chain().filter(FILTERS.family().regex(colFamily2)).filter(FILTERS.timestamp().range().of(secRangeStart * 1000, secRangeEnd * 1000))));
    Assert.assertEquals(expected.toProto(), query.toProto(requestContext).getFilter());
}
Also used : Filters(com.google.cloud.bigtable.data.v2.models.Filters) BigtableExtendedScan(com.google.cloud.bigtable.hbase.BigtableExtendedScan) Scan(org.apache.hadoop.hbase.client.Scan) ByteString(com.google.protobuf.ByteString) Test(org.junit.Test)

Example 5 with Query

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

the class TestDataClientVeneerApi method testReadRowsAsyncWithStreamOb.

@Test
public void testReadRowsAsyncWithStreamOb() {
    final Exception readException = new Exception();
    Query request = Query.create(TABLE_ID).rowKey(ROW_KEY);
    StreamObserver<Result> resultStreamOb = new StreamObserver<Result>() {

        @Override
        public void onNext(Result result) {
            assertResult(EXPECTED_RESULT, result);
        }

        @Override
        public void onError(Throwable throwable) {
            assertEquals(readException, throwable);
        }

        @Override
        public void onCompleted() {
        }
    };
    when(mockDataClient.readRowsCallable(Mockito.<RowResultAdapter>any())).thenReturn(mockStreamingCallable);
    doAnswer(new Answer() {

        int count = 0;

        @Override
        public Object answer(InvocationOnMock invocationOnMock) {
            ResponseObserver<Result> resObserver = invocationOnMock.getArgument(1);
            resObserver.onStart(null);
            resObserver.onResponse(EXPECTED_RESULT);
            if (count == 0) {
                resObserver.onComplete();
            } else {
                resObserver.onError(readException);
            }
            count++;
            return null;
        }
    }).when(mockStreamingCallable).call(Mockito.<Query>any(), Mockito.<ResponseObserver<Result>>any());
    dataClientWrapper.readRowsAsync(request, resultStreamOb);
    dataClientWrapper.readRowsAsync(request, resultStreamOb);
    verify(mockDataClient, times(2)).readRowsCallable(Mockito.<RowResultAdapter>any());
    verify(mockStreamingCallable, times(2)).call(Mockito.<Query>any(), Mockito.<ResponseObserver<Result>>any(), Mockito.any(GrpcCallContext.class));
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) Query(com.google.cloud.bigtable.data.v2.models.Query) IOException(java.io.IOException) Result(org.apache.hadoop.hbase.client.Result) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) GrpcCallContext(com.google.api.gax.grpc.GrpcCallContext) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ResponseObserver(com.google.api.gax.rpc.ResponseObserver) Test(org.junit.Test)

Aggregations

Query (com.google.cloud.bigtable.data.v2.models.Query)33 Row (com.google.cloud.bigtable.data.v2.models.Row)23 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)3 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