use of com.google.bigtable.v2.ReadRowsResponse in project beam by apache.
the class BigQueryIOStorageReadTest method testStreamSourceSplitAtFractionSucceedsArrow.
@Test
public void testStreamSourceSplitAtFractionSucceedsArrow() throws Exception {
List<String> names = Arrays.asList("A", "B", "C", "D", "E", "F", "G");
List<Long> values = Arrays.asList(1L, 2L, 3L, 4L, 5L, 6L, 7L);
List<ReadRowsResponse> parentResponses = Lists.newArrayList(createResponseArrow(ARROW_SCHEMA, names.subList(0, 2), values.subList(0, 2), 0.0, 0.25), createResponseArrow(ARROW_SCHEMA, names.subList(2, 3), values.subList(2, 3), 0.25, 0.5), createResponseArrow(ARROW_SCHEMA, names.subList(3, 5), values.subList(3, 5), 0.5, 0.75));
StorageClient fakeStorageClient = mock(StorageClient.class);
when(fakeStorageClient.readRows(ReadRowsRequest.newBuilder().setReadStream("parentStream").build(), "")).thenReturn(new FakeBigQueryServerStream<>(parentResponses));
// Mocks the split call.
when(fakeStorageClient.splitReadStream(SplitReadStreamRequest.newBuilder().setName("parentStream").setFraction(0.5f).build())).thenReturn(SplitReadStreamResponse.newBuilder().setPrimaryStream(ReadStream.newBuilder().setName("primaryStream")).setRemainderStream(ReadStream.newBuilder().setName("remainderStream")).build());
// Mocks the ReadRows calls expected on the primary and residual streams.
when(fakeStorageClient.readRows(ReadRowsRequest.newBuilder().setReadStream("primaryStream").setOffset(2).build(), "")).thenReturn(new FakeBigQueryServerStream<>(parentResponses.subList(1, 2)));
when(fakeStorageClient.readRows(ReadRowsRequest.newBuilder().setReadStream("remainderStream").build(), "")).thenReturn(new FakeBigQueryServerStream<>(parentResponses.subList(2, parentResponses.size())));
BigQueryStorageStreamSource<TableRow> streamSource = BigQueryStorageStreamSource.create(ReadSession.newBuilder().setName("readSession").setArrowSchema(ArrowSchema.newBuilder().setSerializedSchema(serializeArrowSchema(ARROW_SCHEMA)).build()).setDataFormat(DataFormat.ARROW).build(), ReadStream.newBuilder().setName("parentStream").build(), TABLE_SCHEMA, new TableRowParser(), TableRowJsonCoder.of(), new FakeBigQueryServices().withStorageClient(fakeStorageClient));
// Read a few records from the parent stream and ensure that records are returned in the
// prescribed order.
BoundedReader<TableRow> parent = streamSource.createReader(options);
assertTrue(parent.start());
assertEquals("A", parent.getCurrent().get("name"));
assertTrue(parent.advance());
assertEquals("B", parent.getCurrent().get("name"));
// Now split the stream, and ensure that the "parent" reader has been replaced with the
// primary stream and that the returned source points to the residual stream.
BoundedReader<TableRow> primary = parent;
BoundedSource<TableRow> residualSource = parent.splitAtFraction(0.5);
assertNotNull(residualSource);
BoundedReader<TableRow> residual = residualSource.createReader(options);
assertTrue(primary.advance());
assertEquals("C", primary.getCurrent().get("name"));
assertFalse(primary.advance());
assertTrue(residual.start());
assertEquals("D", residual.getCurrent().get("name"));
assertTrue(residual.advance());
assertEquals("E", residual.getCurrent().get("name"));
assertFalse(residual.advance());
}
use of com.google.bigtable.v2.ReadRowsResponse in project beam by apache.
the class BigQueryIOStorageReadTest method testReadFromStreamSource.
@Test
public void testReadFromStreamSource() throws Exception {
ReadSession readSession = ReadSession.newBuilder().setName("readSession").setAvroSchema(AvroSchema.newBuilder().setSchema(AVRO_SCHEMA_STRING)).build();
ReadRowsRequest expectedRequest = ReadRowsRequest.newBuilder().setReadStream("readStream").build();
List<GenericRecord> records = Lists.newArrayList(createRecord("A", 1, AVRO_SCHEMA), createRecord("B", 2, AVRO_SCHEMA), createRecord("C", 3, AVRO_SCHEMA));
List<ReadRowsResponse> responses = Lists.newArrayList(createResponse(AVRO_SCHEMA, records.subList(0, 2), 0.0, 0.50), createResponse(AVRO_SCHEMA, records.subList(2, 3), 0.5, 0.75));
StorageClient fakeStorageClient = mock(StorageClient.class);
when(fakeStorageClient.readRows(expectedRequest, "")).thenReturn(new FakeBigQueryServerStream<>(responses));
BigQueryStorageStreamSource<TableRow> streamSource = BigQueryStorageStreamSource.create(readSession, ReadStream.newBuilder().setName("readStream").build(), TABLE_SCHEMA, new TableRowParser(), TableRowJsonCoder.of(), new FakeBigQueryServices().withStorageClient(fakeStorageClient));
List<TableRow> rows = new ArrayList<>();
BoundedReader<TableRow> reader = streamSource.createReader(options);
for (boolean hasNext = reader.start(); hasNext; hasNext = reader.advance()) {
rows.add(reader.getCurrent());
}
System.out.println("Rows: " + rows);
assertEquals(3, rows.size());
}
use of com.google.bigtable.v2.ReadRowsResponse in project java-bigtable by googleapis.
the class EmulatorTest method testDataClient.
@Test
public void testDataClient() {
tableAdminStub.createTable(CreateTableRequest.newBuilder().setParent("projects/fake-project/instances/fake-instance").setTableId("fake-table").setTable(Table.newBuilder().putColumnFamilies("cf", ColumnFamily.getDefaultInstance())).build());
dataStub.mutateRow(MutateRowRequest.newBuilder().setTableName("projects/fake-project/instances/fake-instance/tables/fake-table").setRowKey(ByteString.copyFromUtf8("fake-key")).addMutations(Mutation.newBuilder().setSetCell(SetCell.newBuilder().setFamilyName("cf").setColumnQualifier(ByteString.EMPTY).setValue(ByteString.copyFromUtf8("value")))).build());
Iterator<ReadRowsResponse> results = dataStub.readRows(ReadRowsRequest.newBuilder().setTableName("projects/fake-project/instances/fake-instance/tables/fake-table").build());
ReadRowsResponse row = results.next();
assertThat(row.getChunks(0).getValue()).isEqualTo(ByteString.copyFromUtf8("value"));
}
use of com.google.bigtable.v2.ReadRowsResponse in project java-bigtable by googleapis.
the class MetricsTracerTest method testReadRowsAttemptsPerOp.
@Test
public void testReadRowsAttemptsPerOp() throws InterruptedException {
final AtomicInteger callCount = new AtomicInteger(0);
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) {
@SuppressWarnings("unchecked") StreamObserver<ReadRowsResponse> observer = (StreamObserver<ReadRowsResponse>) invocation.getArguments()[1];
// First call will trigger a transient error
if (callCount.getAndIncrement() == 0) {
observer.onError(new StatusRuntimeException(Status.UNAVAILABLE));
return null;
}
// Next attempt will return a row
observer.onNext(DEFAULT_READ_ROWS_RESPONSES);
observer.onCompleted();
return null;
}
}).when(mockService).readRows(any(ReadRowsRequest.class), any());
Lists.newArrayList(stub.readRowsCallable().call(Query.create(TABLE_ID)));
// Give OpenCensus a chance to update the views asynchronously.
Thread.sleep(100);
long opLatency = StatsTestUtils.getAggregationValueAsLong(localStats, RpcViewConstants.BIGTABLE_ATTEMPTS_PER_OP_VIEW, ImmutableMap.of(RpcMeasureConstants.BIGTABLE_OP, TagValue.create("Bigtable.ReadRows"), RpcMeasureConstants.BIGTABLE_STATUS, TagValue.create("OK")), PROJECT_ID, INSTANCE_ID, APP_PROFILE_ID);
assertThat(opLatency).isEqualTo(2);
}
use of com.google.bigtable.v2.ReadRowsResponse in project java-bigtable by googleapis.
the class MetricsTracerTest method testReadRowsLatency.
@Test
public void testReadRowsLatency() throws InterruptedException {
final long sleepTime = 50;
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
@SuppressWarnings("unchecked") StreamObserver<ReadRowsResponse> observer = (StreamObserver<ReadRowsResponse>) invocation.getArguments()[1];
Thread.sleep(sleepTime);
observer.onNext(DEFAULT_READ_ROWS_RESPONSES);
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 opLatency = StatsTestUtils.getAggregationValueAsLong(localStats, RpcViewConstants.BIGTABLE_OP_LATENCY_VIEW, ImmutableMap.of(RpcMeasureConstants.BIGTABLE_OP, TagValue.create("Bigtable.ReadRows"), RpcMeasureConstants.BIGTABLE_STATUS, TagValue.create("OK")), PROJECT_ID, INSTANCE_ID, APP_PROFILE_ID);
assertThat(opLatency).isIn(Range.closed(sleepTime, elapsed));
}
Aggregations