Search in sources :

Example 11 with ReadRowsResponse

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));
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Answer(org.mockito.stubbing.Answer) ReadRowsResponse(com.google.bigtable.v2.ReadRowsResponse) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Stopwatch(com.google.common.base.Stopwatch) ReadRowsRequest(com.google.bigtable.v2.ReadRowsRequest) Test(org.junit.Test)

Example 12 with ReadRowsResponse

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);
}
Also used : CellChunk(com.google.bigtable.v2.ReadRowsResponse.CellChunk) ServerStreamingStashCallable(com.google.cloud.bigtable.gaxx.testing.FakeStreamingApi.ServerStreamingStashCallable) RowCell(com.google.cloud.bigtable.data.v2.models.RowCell) ReadRowsRequest(com.google.bigtable.v2.ReadRowsRequest) IOException(java.io.IOException) DefaultRowAdapter(com.google.cloud.bigtable.data.v2.models.DefaultRowAdapter) ReadRowsResponse(com.google.bigtable.v2.ReadRowsResponse) Row(com.google.cloud.bigtable.data.v2.models.Row) ReadRowsTest(com.google.cloud.conformance.bigtable.v2.TestDefinition.ReadRowsTest) Test(org.junit.Test)

Example 13 with ReadRowsResponse

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()));
}
Also used : FakeStreamingApi(com.google.cloud.bigtable.gaxx.testing.FakeStreamingApi) ReadRowsResponse(com.google.bigtable.v2.ReadRowsResponse) ServerStreamingStashCallable(com.google.cloud.bigtable.gaxx.testing.FakeStreamingApi.ServerStreamingStashCallable) RowCell(com.google.cloud.bigtable.data.v2.models.RowCell) ReadRowsRequest(com.google.bigtable.v2.ReadRowsRequest) Row(com.google.cloud.bigtable.data.v2.models.Row) DefaultRowAdapter(com.google.cloud.bigtable.data.v2.models.DefaultRowAdapter) Test(org.junit.Test)

Example 14 with ReadRowsResponse

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);
}
Also used : FakeStreamingApi(com.google.cloud.bigtable.gaxx.testing.FakeStreamingApi) ReadRowsResponse(com.google.bigtable.v2.ReadRowsResponse) ServerStreamingStashCallable(com.google.cloud.bigtable.gaxx.testing.FakeStreamingApi.ServerStreamingStashCallable) ReadRowsRequest(com.google.bigtable.v2.ReadRowsRequest) Row(com.google.cloud.bigtable.data.v2.models.Row) DefaultRowAdapter(com.google.cloud.bigtable.data.v2.models.DefaultRowAdapter) Test(org.junit.Test)

Example 15 with ReadRowsResponse

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();
}
Also used : Table(com.google.api.services.bigquery.model.Table) ReadSession(com.google.cloud.bigquery.storage.v1.ReadSession) ReadRowsRequest(com.google.cloud.bigquery.storage.v1.ReadRowsRequest) StorageClient(org.apache.beam.sdk.io.gcp.bigquery.BigQueryServices.StorageClient) ByteString(com.google.protobuf.ByteString) KV(org.apache.beam.sdk.values.KV) TableReference(com.google.api.services.bigquery.model.TableReference) ReadRowsResponse(com.google.cloud.bigquery.storage.v1.ReadRowsResponse) FakeBigQueryServices(org.apache.beam.sdk.io.gcp.testing.FakeBigQueryServices) CreateReadSessionRequest(com.google.cloud.bigquery.storage.v1.CreateReadSessionRequest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)29 ReadRowsResponse (com.google.cloud.bigquery.storage.v1.ReadRowsResponse)18 StorageClient (org.apache.beam.sdk.io.gcp.bigquery.BigQueryServices.StorageClient)17 FakeBigQueryServices (org.apache.beam.sdk.io.gcp.testing.FakeBigQueryServices)17 TableRow (com.google.api.services.bigquery.model.TableRow)14 ReadRowsResponse (com.google.bigtable.v2.ReadRowsResponse)13 TableRowParser (org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO.TableRowParser)13 ReadRowsRequest (com.google.cloud.bigquery.storage.v1.ReadRowsRequest)11 ReadRowsRequest (com.google.bigtable.v2.ReadRowsRequest)10 ReadSession (com.google.cloud.bigquery.storage.v1.ReadSession)10 ByteString (com.google.protobuf.ByteString)9 StreamObserver (io.grpc.stub.StreamObserver)6 GenericRecord (org.apache.avro.generic.GenericRecord)6 Mockito.doAnswer (org.mockito.Mockito.doAnswer)6 InvocationOnMock (org.mockito.invocation.InvocationOnMock)6 Answer (org.mockito.stubbing.Answer)6 StatusRuntimeException (io.grpc.StatusRuntimeException)5 Table (com.google.api.services.bigquery.model.Table)4 TableReference (com.google.api.services.bigquery.model.TableReference)4 CreateReadSessionRequest (com.google.cloud.bigquery.storage.v1.CreateReadSessionRequest)3