Search in sources :

Example 26 with ReadRowsResponse

use of com.google.cloud.bigquery.storage.v1beta2.ReadRowsResponse in project beam by apache.

the class BigQueryIOStorageReadTest method testFractionConsumedArrow.

@Test
public void testFractionConsumedArrow() throws Exception {
    ReadSession readSession = ReadSession.newBuilder().setName("readSession").setArrowSchema(ArrowSchema.newBuilder().setSerializedSchema(serializeArrowSchema(ARROW_SCHEMA)).build()).setDataFormat(DataFormat.ARROW).build();
    ReadRowsRequest expectedRequest = ReadRowsRequest.newBuilder().setReadStream("readStream").build();
    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> responses = Lists.newArrayList(createResponseArrow(ARROW_SCHEMA, names.subList(0, 2), values.subList(0, 2), 0.0, 0.25), createResponseArrow(ARROW_SCHEMA, Lists.newArrayList(), Lists.newArrayList(), 0.25, 0.25), createResponseArrow(ARROW_SCHEMA, names.subList(2, 4), values.subList(2, 4), 0.3, 0.5), createResponseArrow(ARROW_SCHEMA, names.subList(4, 7), values.subList(4, 7), 0.7, 1.0));
    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));
    BoundedReader<TableRow> reader = streamSource.createReader(options);
    // Before call to BoundedReader#start, fraction consumed must be zero.
    assertEquals(0.0, reader.getFractionConsumed(), DELTA);
    // Reads A.
    assertTrue(reader.start());
    assertEquals(0.125, reader.getFractionConsumed(), DELTA);
    // Reads B.
    assertTrue(reader.advance());
    assertEquals(0.25, reader.getFractionConsumed(), DELTA);
    // Reads C.
    assertTrue(reader.advance());
    assertEquals(0.4, reader.getFractionConsumed(), DELTA);
    // Reads D.
    assertTrue(reader.advance());
    assertEquals(0.5, reader.getFractionConsumed(), DELTA);
    // Reads E.
    assertTrue(reader.advance());
    assertEquals(0.8, reader.getFractionConsumed(), DELTA);
    // Reads F.
    assertTrue(reader.advance());
    assertEquals(0.9, reader.getFractionConsumed(), DELTA);
    // Reads G.
    assertTrue(reader.advance());
    assertEquals(1.0, reader.getFractionConsumed(), DELTA);
    // Reaches the end.
    assertFalse(reader.advance());
    // We are done with the stream, so we should report 100% consumption.
    assertEquals(Double.valueOf(1.0), reader.getFractionConsumed());
}
Also used : 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) TableRowParser(org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO.TableRowParser) ReadRowsResponse(com.google.cloud.bigquery.storage.v1.ReadRowsResponse) TableRow(com.google.api.services.bigquery.model.TableRow) FakeBigQueryServices(org.apache.beam.sdk.io.gcp.testing.FakeBigQueryServices) Test(org.junit.Test)

Example 27 with ReadRowsResponse

use of com.google.cloud.bigquery.storage.v1beta2.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());
}
Also used : ReadSession(com.google.cloud.bigquery.storage.v1.ReadSession) ArrayList(java.util.ArrayList) ReadRowsRequest(com.google.cloud.bigquery.storage.v1.ReadRowsRequest) StorageClient(org.apache.beam.sdk.io.gcp.bigquery.BigQueryServices.StorageClient) TableRowParser(org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO.TableRowParser) ReadRowsResponse(com.google.cloud.bigquery.storage.v1.ReadRowsResponse) TableRow(com.google.api.services.bigquery.model.TableRow) FakeBigQueryServices(org.apache.beam.sdk.io.gcp.testing.FakeBigQueryServices) GenericRecord(org.apache.avro.generic.GenericRecord) Test(org.junit.Test)

Example 28 with ReadRowsResponse

use of com.google.cloud.bigquery.storage.v1beta2.ReadRowsResponse in project beam by apache.

the class BigQueryServicesImplTest method testReadRowsSetsRequestCountMetric.

@Test
public void testReadRowsSetsRequestCountMetric() throws InterruptedException, IOException {
    BigQueryServices.StorageClient client = mock(BigQueryServicesImpl.StorageClientImpl.class);
    ReadRowsRequest request = null;
    BigQueryServices.BigQueryServerStream<ReadRowsResponse> response = new BigQueryServices.BigQueryServerStream<ReadRowsResponse>() {

        @Override
        public Iterator<ReadRowsResponse> iterator() {
            return null;
        }

        @Override
        public void cancel() {
        }
    };
    // Mock implementation.
    when(client.readRows(request)).thenReturn(response);
    // Real implementation.
    when(client.readRows(any(), any())).thenCallRealMethod();
    client.readRows(request, "myproject:mydataset.mytable");
    verifyReadMetricWasSet("myproject", "mydataset", "mytable", "ok", 1);
}
Also used : ReadRowsResponse(com.google.cloud.bigquery.storage.v1.ReadRowsResponse) ReadRowsRequest(com.google.cloud.bigquery.storage.v1.ReadRowsRequest) Test(org.junit.Test)

Example 29 with ReadRowsResponse

use of com.google.cloud.bigquery.storage.v1beta2.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;
    SettableFuture<Void> gotFirstRow = SettableFuture.create();
    ExecutorService executor = Executors.newCachedThreadPool();
    doAnswer(invocation -> {
        StreamObserver<ReadRowsResponse> observer = invocation.getArgument(1);
        executor.submit(() -> {
            Thread.sleep(beforeSleep);
            observer.onNext(DEFAULT_READ_ROWS_RESPONSES);
            // wait until the first row is consumed before padding the operation span
            gotFirstRow.get();
            Thread.sleep(afterSleep);
            observer.onCompleted();
            return null;
        });
        return null;
    }).when(mockService).readRows(any(ReadRowsRequest.class), any());
    Stopwatch stopwatch = Stopwatch.createStarted();
    // Get the first row and notify the mock that it can start padding the operation span
    Iterator<Row> it = stub.readRowsCallable().call(Query.create(TABLE_ID)).iterator();
    it.next();
    gotFirstRow.set(null);
    // finish the stream
    while (it.hasNext()) {
        it.next();
    }
    long elapsed = stopwatch.elapsed(TimeUnit.MILLISECONDS);
    // Give OpenCensus a chance to update the views asynchronously.
    Thread.sleep(100);
    executor.shutdown();
    long firstRowLatency = StatsTestUtils.getAggregationValueAsLong(localStats, RpcViewConstants.BIGTABLE_READ_ROWS_FIRST_ROW_LATENCY_VIEW, ImmutableMap.<TagKey, TagValue>of(), PROJECT_ID, INSTANCE_ID, APP_PROFILE_ID);
    assertThat(firstRowLatency).isIn(Range.closed(beforeSleep, elapsed - afterSleep));
}
Also used : ReadRowsResponse(com.google.bigtable.v2.ReadRowsResponse) ExecutorService(java.util.concurrent.ExecutorService) Stopwatch(com.google.common.base.Stopwatch) ReadRowsRequest(com.google.bigtable.v2.ReadRowsRequest) Row(com.google.cloud.bigtable.data.v2.models.Row) Test(org.junit.Test)

Example 30 with ReadRowsResponse

use of com.google.cloud.bigquery.storage.v1beta2.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));
}
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)

Aggregations

Test (org.junit.Test)44 ReadRowsResponse (com.google.cloud.bigquery.storage.v1.ReadRowsResponse)40 ReadRowsRequest (com.google.cloud.bigquery.storage.v1.ReadRowsRequest)25 ReadRowsResponse (com.google.bigtable.v2.ReadRowsResponse)19 ReadSession (com.google.cloud.bigquery.storage.v1.ReadSession)17 StorageClient (org.apache.beam.sdk.io.gcp.bigquery.BigQueryServices.StorageClient)17 FakeBigQueryServices (org.apache.beam.sdk.io.gcp.testing.FakeBigQueryServices)17 ReadRowsRequest (com.google.bigtable.v2.ReadRowsRequest)15 TableRow (com.google.api.services.bigquery.model.TableRow)14 TableRowParser (org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO.TableRowParser)13 ReadRowsResponse (com.google.cloud.bigquery.storage.v1beta2.ReadRowsResponse)11 ReadRowsRequest (com.google.cloud.bigquery.storage.v1beta2.ReadRowsRequest)10 ByteString (com.google.protobuf.ByteString)10 CreateReadSessionRequest (com.google.cloud.bigquery.storage.v1.CreateReadSessionRequest)8 GenericRecord (org.apache.avro.generic.GenericRecord)7 StatusRuntimeException (io.grpc.StatusRuntimeException)6 ReadSession (com.google.cloud.bigquery.storage.v1beta2.ReadSession)5 StreamObserver (io.grpc.stub.StreamObserver)5 Mockito.doAnswer (org.mockito.Mockito.doAnswer)5 InvocationOnMock (org.mockito.invocation.InvocationOnMock)5