Search in sources :

Example 6 with Stream

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

the class ITBigQueryStorageLongRunningTest method testLongRunningReadSession.

@Test
public void testLongRunningReadSession() throws InterruptedException, ExecutionException {
    // This test reads a larger table with the goal of doing a simple validation of timeout settings
    // for a longer running session.
    TableReference tableReference = TableReference.newBuilder().setProjectId("bigquery-public-data").setDatasetId("samples").setTableId("wikipedia").build();
    ReadSession session = client.createReadSession(/* tableReference = */
    tableReference, /* parent = */
    parentProjectId, /* requestedStreams = */
    5);
    assertEquals(String.format("Did not receive expected number of streams for table reference '%s' CreateReadSession response:%n%s", TextFormat.shortDebugString(tableReference), session.toString()), 5, session.getStreamsCount());
    List<Callable<Long>> tasks = new ArrayList<>(session.getStreamsCount());
    for (final Stream stream : session.getStreamsList()) {
        tasks.add(new Callable<Long>() {

            @Override
            public Long call() throws Exception {
                return readAllRowsFromStream(stream);
            }
        });
    }
    ExecutorService executor = Executors.newFixedThreadPool(tasks.size());
    List<Future<Long>> results = executor.invokeAll(tasks);
    long rowCount = 0;
    for (Future<Long> result : results) {
        rowCount += result.get();
    }
    assertEquals(313_797_035, rowCount);
}
Also used : ReadSession(com.google.cloud.bigquery.storage.v1beta1.Storage.ReadSession) ArrayList(java.util.ArrayList) Callable(java.util.concurrent.Callable) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) TableReference(com.google.cloud.bigquery.storage.v1beta1.TableReferenceProto.TableReference) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) Stream(com.google.cloud.bigquery.storage.v1beta1.Storage.Stream) ServerStream(com.google.api.gax.rpc.ServerStream) Test(org.junit.Test)

Example 7 with Stream

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

the class BigQueryStorageClientTest method finalizeStreamExceptionTest.

@Test
@SuppressWarnings("all")
public void finalizeStreamExceptionTest() throws Exception {
    StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT);
    mockBigQueryStorage.addException(exception);
    try {
        Stream stream = Stream.newBuilder().build();
        client.finalizeStream(stream);
        Assert.fail("No exception raised");
    } catch (InvalidArgumentException e) {
    // Expected exception
    }
}
Also used : InvalidArgumentException(com.google.api.gax.rpc.InvalidArgumentException) StatusRuntimeException(io.grpc.StatusRuntimeException) Stream(com.google.cloud.bigquery.storage.v1beta1.Storage.Stream) Test(org.junit.Test)

Example 8 with Stream

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

the class ReadRowsRetryTest method multipleRetryTestWithNonZeroInitialOffset.

@Test
public void multipleRetryTestWithNonZeroInitialOffset() {
    ReadRowsRequest request = RpcExpectation.createRequest("fake-stream", 17);
    service.expectations.add(RpcExpectation.create().expectRequest("fake-stream", 17).respondWithNumberOfRows(5).respondWithStatus(Code.UNAVAILABLE));
    service.expectations.add(RpcExpectation.create().expectRequest("fake-stream", 22).respondWithNumberOfRows(10).respondWithNumberOfRows(7).respondWithStatus(Code.UNAVAILABLE));
    service.expectations.add(RpcExpectation.create().expectRequest("fake-stream", 39).respondWithNumberOfRows(3));
    Assert.assertEquals(25, getRowCount(request));
}
Also used : ReadRowsRequest(com.google.cloud.bigquery.storage.v1beta1.Storage.ReadRowsRequest) Test(org.junit.Test)

Example 9 with Stream

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

the class ReadRowsRetryTest method multipleRetryTestWithZeroInitialOffset.

@Test
public void multipleRetryTestWithZeroInitialOffset() {
    ReadRowsRequest request = RpcExpectation.createRequest("fake-stream", 0);
    service.expectations.add(RpcExpectation.create().expectRequest("fake-stream", 0).respondWithNumberOfRows(5).respondWithStatus(Code.UNAVAILABLE));
    service.expectations.add(RpcExpectation.create().expectRequest("fake-stream", 5).respondWithNumberOfRows(10).respondWithNumberOfRows(7).respondWithStatus(Code.UNAVAILABLE));
    service.expectations.add(RpcExpectation.create().expectRequest("fake-stream", 22).respondWithNumberOfRows(6));
    Assert.assertEquals(28, getRowCount(request));
}
Also used : ReadRowsRequest(com.google.cloud.bigquery.storage.v1beta1.Storage.ReadRowsRequest) Test(org.junit.Test)

Example 10 with Stream

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

the class BigQueryStorageClient method finalizeStream.

// AUTO-GENERATED DOCUMENTATION AND METHOD
/**
 * Triggers the graceful termination of a single stream in a ReadSession. This API can be used to
 * dynamically adjust the parallelism of a batch processing task downwards without losing data.
 *
 * <p>This API does not delete the stream -- it remains visible in the ReadSession, and any data
 * processed by the stream is not released to other streams. However, no additional data will be
 * assigned to the stream once this call completes. Callers must continue reading data on the
 * stream until the end of the stream is reached so that data which has already been assigned to
 * the stream will be processed.
 *
 * <p>This method will return an error if there are no other live streams in the Session, or if
 * SplitReadStream() has been called on the given Stream.
 *
 * <p>Sample code:
 *
 * <pre><code>
 * try (BigQueryStorageClient bigQueryStorageClient = BigQueryStorageClient.create()) {
 *   Stream stream = Stream.newBuilder().build();
 *   bigQueryStorageClient.finalizeStream(stream);
 * }
 * </code></pre>
 *
 * @param stream Stream to finalize.
 * @throws com.google.api.gax.rpc.ApiException if the remote call fails
 */
public final void finalizeStream(Stream stream) {
    FinalizeStreamRequest request = FinalizeStreamRequest.newBuilder().setStream(stream).build();
    finalizeStream(request);
}
Also used : FinalizeStreamRequest(com.google.cloud.bigquery.storage.v1beta1.Storage.FinalizeStreamRequest)

Aggregations

ReadRowsRequest (com.google.cloud.bigquery.storage.v1beta1.Storage.ReadRowsRequest)15 Test (org.junit.Test)15 ReadSession (com.google.cloud.bigquery.storage.v1beta1.Storage.ReadSession)9 ReadRowsResponse (com.google.cloud.bigquery.storage.v1beta1.Storage.ReadRowsResponse)8 StreamPosition (com.google.cloud.bigquery.storage.v1beta1.Storage.StreamPosition)7 Stream (com.google.cloud.bigquery.storage.v1beta1.Storage.Stream)5 TableReference (com.google.cloud.bigquery.storage.v1beta1.TableReferenceProto.TableReference)5 CreateReadSessionRequest (com.google.cloud.bigquery.storage.v1beta1.Storage.CreateReadSessionRequest)4 TableReadOptions (com.google.cloud.bigquery.storage.v1beta1.ReadOptions.TableReadOptions)3 Logger (com.facebook.airlift.log.Logger)2 BIGQUERY_FAILED_TO_EXECUTE_QUERY (com.facebook.presto.plugin.bigquery.BigQueryErrorCode.BIGQUERY_FAILED_TO_EXECUTE_QUERY)2 ColumnHandle (com.facebook.presto.spi.ColumnHandle)2 ConnectorSession (com.facebook.presto.spi.ConnectorSession)2 ConnectorSplitSource (com.facebook.presto.spi.ConnectorSplitSource)2 ConnectorTableLayoutHandle (com.facebook.presto.spi.ConnectorTableLayoutHandle)2 FixedSplitSource (com.facebook.presto.spi.FixedSplitSource)2 NodeManager (com.facebook.presto.spi.NodeManager)2 PrestoException (com.facebook.presto.spi.PrestoException)2 ConnectorSplitManager (com.facebook.presto.spi.connector.ConnectorSplitManager)2 ConnectorTransactionHandle (com.facebook.presto.spi.connector.ConnectorTransactionHandle)2