Search in sources :

Example 21 with ReadRowsRequest

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

the class ITBigQueryStorageTest method testSimpleReadAndResume.

@Test
public void testSimpleReadAndResume() {
    TableReference tableReference = TableReference.newBuilder().setProjectId("bigquery-public-data").setDatasetId("samples").setTableId("shakespeare").build();
    ReadSession session = client.createReadSession(/* tableReference = */
    tableReference, /* parent = */
    parentProjectId, /* requestedStreams = */
    1);
    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());
    // We have to read some number of rows in order to be able to resume. More details:
    // https://cloud.google.com/bigquery/docs/reference/storage/rpc/google.cloud.bigquery.storage.v1beta1#google.cloud.bigquery.storage.v1beta1.ReadRowsRequest
    long rowCount = ReadStreamToOffset(session.getStreams(0), /* rowOffset = */
    34_846);
    StreamPosition readPosition = StreamPosition.newBuilder().setStream(session.getStreams(0)).setOffset(rowCount).build();
    ReadRowsRequest readRowsRequest = ReadRowsRequest.newBuilder().setReadPosition(readPosition).build();
    ServerStream<ReadRowsResponse> stream = client.readRowsCallable().call(readRowsRequest);
    for (ReadRowsResponse response : stream) {
        rowCount += response.getRowCount();
    }
    // Verifies that the number of rows skipped and read equals to the total number of rows in the
    // table.
    assertEquals(164_656, rowCount);
}
Also used : TableReference(com.google.cloud.bigquery.storage.v1beta1.TableReferenceProto.TableReference) ReadRowsResponse(com.google.cloud.bigquery.storage.v1beta1.Storage.ReadRowsResponse) 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) Test(org.junit.Test)

Example 22 with ReadRowsRequest

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

the class ITBigQueryStorageTest method ReadStreamToOffset.

/**
 * Reads to the specified row offset within the stream. If the stream does not have the desired
 * rows to read, it will read all of them.
 *
 * @param stream
 * @param rowOffset
 * @return the number of requested rows to skip or the total rows read if stream had less rows.
 */
private long ReadStreamToOffset(Stream stream, long rowOffset) {
    StreamPosition readPosition = StreamPosition.newBuilder().setStream(stream).build();
    ReadRowsRequest readRowsRequest = ReadRowsRequest.newBuilder().setReadPosition(readPosition).build();
    long rowCount = 0;
    ServerStream<ReadRowsResponse> serverStream = client.readRowsCallable().call(readRowsRequest);
    Iterator<ReadRowsResponse> responseIterator = serverStream.iterator();
    while (responseIterator.hasNext()) {
        ReadRowsResponse response = responseIterator.next();
        rowCount += response.getRowCount();
        if (rowCount >= rowOffset) {
            return rowOffset;
        }
    }
    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 23 with ReadRowsRequest

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

the class ResourceHeaderTest method readRowsTest.

@Test
public void readRowsTest() {
    try {
        ReadRowsRequest request = ReadRowsRequest.newBuilder().setReadPosition(StreamPosition.newBuilder().setStream(TEST_STREAM).setOffset(125)).build();
        client.readRowsCallable().call(request);
    } catch (UnimplementedException e) {
    // Ignore the error: none of the methods are actually implemented.
    }
    verifyHeaderSent(READ_POSITION_PATTERN);
}
Also used : ReadRowsRequest(com.google.cloud.bigquery.storage.v1beta1.Storage.ReadRowsRequest) UnimplementedException(com.google.api.gax.rpc.UnimplementedException) Test(org.junit.Test)

Example 24 with ReadRowsRequest

use of com.google.cloud.bigquery.storage.v1beta1.Storage.ReadRowsRequest in project hadoop-connectors by GoogleCloudDataproc.

the class DirectBigQueryRecordReaderTest method initialize.

private void initialize() throws Exception {
    ReadRowsRequest request = ReadRowsRequest.newBuilder().setReadPosition(StreamPosition.newBuilder().setStream(STREAM)).build();
    reader.initialize(split, taskContext);
    verify(bqClient).readRowsCallable();
    verify(readRows).call(eq(request));
}
Also used : ReadRowsRequest(com.google.cloud.bigquery.storage.v1beta1.Storage.ReadRowsRequest)

Example 25 with ReadRowsRequest

use of com.google.cloud.bigquery.storage.v1beta1.Storage.ReadRowsRequest in project hadoop-connectors by GoogleCloudDataproc.

the class DirectBigQueryRecordReader method initialize.

@Override
public void initialize(InputSplit genericSplit, TaskAttemptContext context) throws IOException {
    DirectBigQueryInputSplit split = (DirectBigQueryInputSplit) genericSplit;
    schema = parser.parse(checkNotNull(split.getSchema(), "schema"));
    stream = Stream.newBuilder().setName(checkNotNull(split.getName(), "name")).build();
    ReadRowsRequest request = ReadRowsRequest.newBuilder().setReadPosition(StreamPosition.newBuilder().setStream(stream).build()).build();
    client = getClient(context.getConfiguration());
    responseIterator = client.readRowsCallable().call(request).iterator();
    recordIterator = Collections.emptyIterator();
    limit = split.getLimit();
    idx = 0;
    finalized = false;
}
Also used : DirectBigQueryInputSplit(com.google.cloud.hadoop.io.bigquery.DirectBigQueryInputFormat.DirectBigQueryInputSplit) ReadRowsRequest(com.google.cloud.bigquery.storage.v1beta1.Storage.ReadRowsRequest)

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