Search in sources :

Example 16 with ReadRowsRequest

use of com.google.cloud.bigquery.storage.v1beta1.Storage.ReadRowsRequest in project java-bigquerystorage by googleapis.

the class BigQueryStorageClientTest method readRowsTest.

@Test
@SuppressWarnings("all")
public void readRowsTest() throws Exception {
    ReadRowsResponse expectedResponse = ReadRowsResponse.newBuilder().build();
    mockBigQueryStorage.addResponse(expectedResponse);
    StreamPosition readPosition = StreamPosition.newBuilder().build();
    ReadRowsRequest request = ReadRowsRequest.newBuilder().setReadPosition(readPosition).build();
    MockStreamObserver<ReadRowsResponse> responseObserver = new MockStreamObserver<>();
    ServerStreamingCallable<ReadRowsRequest, ReadRowsResponse> callable = client.readRowsCallable();
    callable.serverStreamingCall(request, responseObserver);
    List<ReadRowsResponse> actualResponses = responseObserver.future().get();
    Assert.assertEquals(1, actualResponses.size());
    Assert.assertEquals(expectedResponse, actualResponses.get(0));
    Assert.assertEquals(retryCount, 0);
    Assert.assertEquals(lastRetryStatusCode, Code.OK);
}
Also used : ReadRowsResponse(com.google.cloud.bigquery.storage.v1beta1.Storage.ReadRowsResponse) StreamPosition(com.google.cloud.bigquery.storage.v1beta1.Storage.StreamPosition) ReadRowsRequest(com.google.cloud.bigquery.storage.v1beta1.Storage.ReadRowsRequest) MockStreamObserver(com.google.api.gax.grpc.testing.MockStreamObserver) Test(org.junit.Test)

Example 17 with ReadRowsRequest

use of com.google.cloud.bigquery.storage.v1beta1.Storage.ReadRowsRequest in project java-bigquerystorage by googleapis.

the class BigQueryStorageClientTest method readRowsRetryingEOSExceptionTest.

@Test
@SuppressWarnings("all")
public void readRowsRetryingEOSExceptionTest() throws ExecutionException, InterruptedException {
    ApiException exception = new InternalException(new StatusRuntimeException(Status.INTERNAL.withDescription("Received unexpected EOS on DATA frame from server")), GrpcStatusCode.of(Code.INTERNAL), /* retryable = */
    false);
    mockBigQueryStorage.addException(exception);
    long rowCount = 1340416618L;
    ReadRowsResponse expectedResponse = ReadRowsResponse.newBuilder().setRowCount(rowCount).build();
    mockBigQueryStorage.addResponse(expectedResponse);
    ReadRowsRequest request = ReadRowsRequest.newBuilder().build();
    MockStreamObserver<ReadRowsResponse> responseObserver = new MockStreamObserver<>();
    ServerStreamingCallable<ReadRowsRequest, ReadRowsResponse> callable = client.readRowsCallable();
    callable.serverStreamingCall(request, responseObserver);
    List<ReadRowsResponse> actualResponses = responseObserver.future().get();
    Assert.assertEquals(1, actualResponses.size());
    Assert.assertEquals(retryCount, 1);
    Assert.assertEquals(lastRetryStatusCode, Code.INTERNAL);
}
Also used : ReadRowsResponse(com.google.cloud.bigquery.storage.v1beta1.Storage.ReadRowsResponse) StatusRuntimeException(io.grpc.StatusRuntimeException) ReadRowsRequest(com.google.cloud.bigquery.storage.v1beta1.Storage.ReadRowsRequest) MockStreamObserver(com.google.api.gax.grpc.testing.MockStreamObserver) ApiException(com.google.api.gax.rpc.ApiException) InternalException(com.google.api.gax.rpc.InternalException) Test(org.junit.Test)

Example 18 with ReadRowsRequest

use of com.google.cloud.bigquery.storage.v1beta1.Storage.ReadRowsRequest in project java-bigquerystorage by googleapis.

the class BigQueryStorageClientTest method readRowsRetryingHttp2StreamRstTest.

@Test
@SuppressWarnings("all")
public void readRowsRetryingHttp2StreamRstTest() throws ExecutionException, InterruptedException {
    ApiException exception = new InternalException(new StatusRuntimeException(Status.INTERNAL.withDescription("HTTP/2 error code: INTERNAL_ERROR\nReceived Rst Stream")), GrpcStatusCode.of(Code.INTERNAL), /* retryable = */
    false);
    mockBigQueryStorage.addException(exception);
    long rowCount = 1340416618L;
    ReadRowsResponse expectedResponse = ReadRowsResponse.newBuilder().setRowCount(rowCount).build();
    mockBigQueryStorage.addResponse(expectedResponse);
    ReadRowsRequest request = ReadRowsRequest.newBuilder().build();
    MockStreamObserver<ReadRowsResponse> responseObserver = new MockStreamObserver<>();
    ServerStreamingCallable<ReadRowsRequest, ReadRowsResponse> callable = client.readRowsCallable();
    callable.serverStreamingCall(request, responseObserver);
    List<ReadRowsResponse> actualResponses = responseObserver.future().get();
    Assert.assertEquals(1, actualResponses.size());
    Assert.assertEquals(retryCount, 1);
    Assert.assertEquals(lastRetryStatusCode, Code.INTERNAL);
}
Also used : ReadRowsResponse(com.google.cloud.bigquery.storage.v1beta1.Storage.ReadRowsResponse) StatusRuntimeException(io.grpc.StatusRuntimeException) ReadRowsRequest(com.google.cloud.bigquery.storage.v1beta1.Storage.ReadRowsRequest) MockStreamObserver(com.google.api.gax.grpc.testing.MockStreamObserver) ApiException(com.google.api.gax.rpc.ApiException) InternalException(com.google.api.gax.rpc.InternalException) Test(org.junit.Test)

Example 19 with ReadRowsRequest

use of com.google.cloud.bigquery.storage.v1beta1.Storage.ReadRowsRequest in project java-bigquerystorage by googleapis.

the class ITBigQueryStorageLongRunningTest method readAllRowsFromStream.

private long readAllRowsFromStream(Stream stream) {
    StreamPosition readPosition = StreamPosition.newBuilder().setStream(stream).build();
    ReadRowsRequest readRowsRequest = ReadRowsRequest.newBuilder().setReadPosition(readPosition).build();
    long rowCount = 0;
    ServerStream<ReadRowsResponse> serverStream = client.readRowsCallable().call(readRowsRequest);
    for (ReadRowsResponse response : serverStream) {
        rowCount += response.getRowCount();
    }
    LOG.info(String.format("Read total of %d rows from stream '%s'.", rowCount, stream.getName()));
    return rowCount;
}
Also used : ReadRowsResponse(com.google.cloud.bigquery.storage.v1beta1.Storage.ReadRowsResponse) StreamPosition(com.google.cloud.bigquery.storage.v1beta1.Storage.StreamPosition) ReadRowsRequest(com.google.cloud.bigquery.storage.v1beta1.Storage.ReadRowsRequest)

Example 20 with ReadRowsRequest

use of com.google.cloud.bigquery.storage.v1beta1.Storage.ReadRowsRequest in project java-bigquerystorage by googleapis.

the class ITBigQueryStorageTest method testFilter.

@Test
public void testFilter() throws IOException {
    TableReference tableReference = TableReference.newBuilder().setProjectId("bigquery-public-data").setDatasetId("samples").setTableId("shakespeare").build();
    TableReadOptions options = TableReadOptions.newBuilder().setRowRestriction("word_count > 100").build();
    CreateReadSessionRequest request = CreateReadSessionRequest.newBuilder().setParent(parentProjectId).setRequestedStreams(1).setTableReference(tableReference).setReadOptions(options).setFormat(DataFormat.AVRO).build();
    ReadSession session = client.createReadSession(request);
    assertEquals(String.format("Did not receive expected number of streams for table reference '%s' CreateReadSession response:%n%s", TextFormat.shortDebugString(tableReference), session.toString()), 1, session.getStreamsCount());
    StreamPosition readPosition = StreamPosition.newBuilder().setStream(session.getStreams(0)).build();
    ReadRowsRequest readRowsRequest = ReadRowsRequest.newBuilder().setReadPosition(readPosition).build();
    SimpleRowReader reader = new SimpleRowReader(new Schema.Parser().parse(session.getAvroSchema().getSchema()));
    long rowCount = 0;
    ServerStream<ReadRowsResponse> stream = client.readRowsCallable().call(readRowsRequest);
    for (ReadRowsResponse response : stream) {
        rowCount += response.getRowCount();
        reader.processRows(response.getAvroRows(), new SimpleRowReader.AvroRowConsumer() {

            @Override
            public void accept(GenericData.Record record) {
                Long wordCount = (Long) record.get("word_count");
                assertWithMessage("Row not matching expectations: %s", record.toString()).that(wordCount).isGreaterThan(100L);
            }
        });
    }
    assertEquals(1_333, rowCount);
}
Also used : AvroRowConsumer(com.google.cloud.bigquery.storage.v1beta1.it.SimpleRowReader.AvroRowConsumer) ReadSession(com.google.cloud.bigquery.storage.v1beta1.Storage.ReadSession) StreamPosition(com.google.cloud.bigquery.storage.v1beta1.Storage.StreamPosition) ReadRowsRequest(com.google.cloud.bigquery.storage.v1beta1.Storage.ReadRowsRequest) GenericData(org.apache.avro.generic.GenericData) TableReference(com.google.cloud.bigquery.storage.v1beta1.TableReferenceProto.TableReference) ReadRowsResponse(com.google.cloud.bigquery.storage.v1beta1.Storage.ReadRowsResponse) TableReadOptions(com.google.cloud.bigquery.storage.v1beta1.ReadOptions.TableReadOptions) CreateReadSessionRequest(com.google.cloud.bigquery.storage.v1beta1.Storage.CreateReadSessionRequest) Test(org.junit.Test)

Aggregations

ReadRowsRequest (com.google.cloud.bigquery.storage.v1beta1.Storage.ReadRowsRequest)24 ReadRowsResponse (com.google.cloud.bigquery.storage.v1beta1.Storage.ReadRowsResponse)17 Test (org.junit.Test)17 StreamPosition (com.google.cloud.bigquery.storage.v1beta1.Storage.StreamPosition)9 MockStreamObserver (com.google.api.gax.grpc.testing.MockStreamObserver)6 ReadSession (com.google.cloud.bigquery.storage.v1beta1.Storage.ReadSession)5 StatusRuntimeException (io.grpc.StatusRuntimeException)5 ApiException (com.google.api.gax.rpc.ApiException)4 TableReference (com.google.cloud.bigquery.storage.v1beta1.TableReferenceProto.TableReference)4 InternalException (com.google.api.gax.rpc.InternalException)3 CreateReadSessionRequest (com.google.cloud.bigquery.storage.v1beta1.Storage.CreateReadSessionRequest)3 ExecutionException (java.util.concurrent.ExecutionException)3 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)2 ResourceExhaustedException (com.google.api.gax.rpc.ResourceExhaustedException)2 TableReadOptions (com.google.cloud.bigquery.storage.v1beta1.ReadOptions.TableReadOptions)2 AvroRowConsumer (com.google.cloud.bigquery.storage.v1beta1.it.SimpleRowReader.AvroRowConsumer)2 GenericData (org.apache.avro.generic.GenericData)2 ExponentialRetryAlgorithm (com.google.api.gax.retrying.ExponentialRetryAlgorithm)1 RetrySettings (com.google.api.gax.retrying.RetrySettings)1 ScheduledRetryingExecutor (com.google.api.gax.retrying.ScheduledRetryingExecutor)1