use of com.google.bigtable.v2.ReadRowsResponse in project java-bigtable by googleapis.
the class MetricsTracerTest method testReadRowsFirstRow.
@Test
public void testReadRowsFirstRow() throws InterruptedException {
final long beforeSleep = 50;
final long afterSleep = 50;
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
@SuppressWarnings("unchecked") StreamObserver<ReadRowsResponse> observer = (StreamObserver<ReadRowsResponse>) invocation.getArguments()[1];
Thread.sleep(beforeSleep);
observer.onNext(DEFAULT_READ_ROWS_RESPONSES);
Thread.sleep(afterSleep);
observer.onCompleted();
return null;
}
}).when(mockService).readRows(any(ReadRowsRequest.class), any());
Stopwatch stopwatch = Stopwatch.createStarted();
Lists.newArrayList(stub.readRowsCallable().call(Query.create(TABLE_ID)));
long elapsed = stopwatch.elapsed(TimeUnit.MILLISECONDS);
// Give OpenCensus a chance to update the views asynchronously.
Thread.sleep(100);
long firstRowLatency = StatsTestUtils.getAggregationValueAsLong(localStats, RpcViewConstants.BIGTABLE_READ_ROWS_FIRST_ROW_LATENCY_VIEW, ImmutableMap.<TagKey, TagValue>of(), PROJECT_ID, INSTANCE_ID, APP_PROFILE_ID);
// adding buffer time to the upper range to allow for a race between the emulator and the client
// recording the duration
assertThat(firstRowLatency).isIn(Range.closed(beforeSleep, elapsed - afterSleep / 2));
}
use of com.google.bigtable.v2.ReadRowsResponse in project java-bigtable by googleapis.
the class ReadRowsMergingAcceptanceTest method test.
@Test
public void test() throws Exception {
List<ReadRowsResponse> responses = Lists.newArrayList();
// Convert the chunks into a single ReadRowsResponse
for (CellChunk chunk : testCase.getChunksList()) {
ReadRowsResponse.Builder responseBuilder = ReadRowsResponse.newBuilder();
responseBuilder.addChunks(chunk);
responses.add(responseBuilder.build());
}
// Wrap the responses in a callable
ServerStreamingCallable<ReadRowsRequest, ReadRowsResponse> source = new ServerStreamingStashCallable<>(responses);
RowMergingCallable<Row> mergingCallable = new RowMergingCallable<>(source, new DefaultRowAdapter());
// Invoke the callable to get the merged rows
ServerStream<Row> stream = mergingCallable.call(ReadRowsRequest.getDefaultInstance());
// Read all of the rows and transform them into logical cells
List<ReadRowsTest.Result> actualResults = Lists.newArrayList();
Exception error = null;
try {
for (Row row : stream) {
for (RowCell cell : row.getCells()) {
actualResults.add(ReadRowsTest.Result.newBuilder().setRowKeyBytes(row.getKey()).setFamilyName(cell.getFamily()).setQualifierBytes(cell.getQualifier()).setTimestampMicros(cell.getTimestamp()).setValueBytes(cell.getValue()).setLabel(cell.getLabels().isEmpty() ? "" : cell.getLabels().get(0)).build());
}
}
} catch (Exception e) {
error = e;
}
// Verify the results
if (expectsError(testCase)) {
assertThat(error).isNotNull();
} else {
if (error != null) {
throw error;
}
}
assertThat(getNonExceptionResults(testCase)).isEqualTo(actualResults);
}
use of com.google.bigtable.v2.ReadRowsResponse in project java-bigtable by googleapis.
the class RowMergingCallableTest method scanMarker.
@Test
public void scanMarker() {
FakeStreamingApi.ServerStreamingStashCallable<ReadRowsRequest, ReadRowsResponse> inner = new ServerStreamingStashCallable<>(Lists.newArrayList(// send a scan marker
ReadRowsResponse.newBuilder().setLastScannedRowKey(ByteString.copyFromUtf8("key1")).build()));
RowMergingCallable<Row> rowMergingCallable = new RowMergingCallable<>(inner, new DefaultRowAdapter());
List<Row> results = rowMergingCallable.all().call(ReadRowsRequest.getDefaultInstance());
Truth.assertThat(results).containsExactly(Row.create(ByteString.copyFromUtf8("key1"), Lists.<RowCell>newArrayList()));
}
use of com.google.bigtable.v2.ReadRowsResponse in project java-bigtable by googleapis.
the class RowMergingCallableTest method invalidMarkerInCell.
@Test
public void invalidMarkerInCell() {
FakeStreamingApi.ServerStreamingStashCallable<ReadRowsRequest, ReadRowsResponse> inner = new ServerStreamingStashCallable<>(Lists.newArrayList(ReadRowsResponse.newBuilder().addChunks(CellChunk.newBuilder().setRowKey(ByteString.copyFromUtf8("key1")).setFamilyName(StringValue.newBuilder().setValue("family")).setQualifier(BytesValue.newBuilder().setValue(ByteString.EMPTY)).setTimestampMicros(1_000).setValue(ByteString.copyFromUtf8("a")).setValueSize(2)).build(), // send a scan marker
ReadRowsResponse.newBuilder().setLastScannedRowKey(ByteString.copyFromUtf8("key1")).build(), // finish the cell & row
ReadRowsResponse.newBuilder().addChunks(CellChunk.newBuilder().setValue(ByteString.copyFromUtf8("b")).setValueSize(0).setCommitRow(true)).build()));
RowMergingCallable<Row> rowMergingCallable = new RowMergingCallable<>(inner, new DefaultRowAdapter());
Throwable actualError = null;
try {
rowMergingCallable.all().call(ReadRowsRequest.getDefaultInstance());
} catch (Throwable t) {
actualError = t;
}
Truth.assertThat(actualError).isInstanceOf(IllegalStateException.class);
}
use of com.google.bigtable.v2.ReadRowsResponse in project beam by apache.
the class BigQueryIOStorageReadTest method testReadFromBigQueryIOArrow.
@Test
public void testReadFromBigQueryIOArrow() throws Exception {
fakeDatasetService.createDataset("foo.com:project", "dataset", "", "", null);
TableReference tableRef = BigQueryHelpers.parseTableSpec("foo.com:project:dataset.table");
Table table = new Table().setTableReference(tableRef).setNumBytes(10L).setSchema(TABLE_SCHEMA);
fakeDatasetService.createTable(table);
CreateReadSessionRequest expectedCreateReadSessionRequest = CreateReadSessionRequest.newBuilder().setParent("projects/project-id").setReadSession(ReadSession.newBuilder().setTable("projects/foo.com:project/datasets/dataset/tables/table").setDataFormat(DataFormat.ARROW)).setMaxStreamCount(10).build();
ReadSession readSession = ReadSession.newBuilder().setName("readSessionName").setArrowSchema(ArrowSchema.newBuilder().setSerializedSchema(serializeArrowSchema(ARROW_SCHEMA)).build()).addStreams(ReadStream.newBuilder().setName("streamName")).setDataFormat(DataFormat.ARROW).build();
ReadRowsRequest expectedReadRowsRequest = ReadRowsRequest.newBuilder().setReadStream("streamName").build();
List<String> names = Arrays.asList("A", "B", "C", "D");
List<Long> values = Arrays.asList(1L, 2L, 3L, 4L);
List<ReadRowsResponse> readRowsResponses = Lists.newArrayList(createResponseArrow(ARROW_SCHEMA, names.subList(0, 2), values.subList(0, 2), 0.0, 0.50), createResponseArrow(ARROW_SCHEMA, names.subList(2, 4), values.subList(2, 4), 0.5, 0.75));
StorageClient fakeStorageClient = mock(StorageClient.class, withSettings().serializable());
when(fakeStorageClient.createReadSession(expectedCreateReadSessionRequest)).thenReturn(readSession);
when(fakeStorageClient.readRows(expectedReadRowsRequest, "")).thenReturn(new FakeBigQueryServerStream<>(readRowsResponses));
PCollection<KV<String, Long>> output = p.apply(BigQueryIO.read(new ParseKeyValue()).from("foo.com:project:dataset.table").withMethod(Method.DIRECT_READ).withFormat(DataFormat.ARROW).withTestServices(new FakeBigQueryServices().withDatasetService(fakeDatasetService).withStorageClient(fakeStorageClient)));
PAssert.that(output).containsInAnyOrder(ImmutableList.of(KV.of("A", 1L), KV.of("B", 2L), KV.of("C", 3L), KV.of("D", 4L)));
p.run();
}
Aggregations