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());
}
}
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;
}
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);
}
}
}
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());
}
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));
}
Aggregations