Search in sources :

Example 21 with Stream

use of com.google.cloud.bigquery.storage.v1beta1.Storage.Stream 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 22 with Stream

use of com.google.cloud.bigquery.storage.v1beta1.Storage.Stream in project presto by prestodb.

the class BigQuerySplitManager method readFromBigQuery.

private ImmutableList<BigQuerySplit> readFromBigQuery(TableId tableId, Optional<List<ColumnHandle>> projectedColumns, int actualParallelism, Optional<String> filter) {
    List<ColumnHandle> columns = projectedColumns.orElse(ImmutableList.of());
    ImmutableList<String> projectedColumnsNames = columns.stream().map(column -> ((BigQueryColumnHandle) column).getName()).collect(toImmutableList());
    ReadSession readSession = new ReadSessionCreator(readSessionCreatorConfig, bigQueryClient, bigQueryStorageClientFactory).create(tableId, projectedColumnsNames, filter, actualParallelism);
    return readSession.getStreamsList().stream().map(stream -> BigQuerySplit.forStream(stream.getName(), readSession.getAvroSchema().getSchema(), columns)).collect(toImmutableList());
}
Also used : Logger(com.facebook.airlift.log.Logger) ConnectorSplitSource(com.facebook.presto.spi.ConnectorSplitSource) ReadSession(com.google.cloud.bigquery.storage.v1beta1.Storage.ReadSession) IntStream.range(java.util.stream.IntStream.range) Inject(com.google.inject.Inject) ConnectorTableLayoutHandle(com.facebook.presto.spi.ConnectorTableLayoutHandle) TableId(com.google.cloud.bigquery.TableId) BigQueryException(com.google.cloud.bigquery.BigQueryException) PrestoException(com.facebook.presto.spi.PrestoException) OptionalInt(java.util.OptionalInt) ConnectorTransactionHandle(com.facebook.presto.spi.connector.ConnectorTransactionHandle) BIGQUERY_FAILED_TO_EXECUTE_QUERY(com.facebook.presto.plugin.bigquery.BigQueryErrorCode.BIGQUERY_FAILED_TO_EXECUTE_QUERY) ImmutableList(com.google.common.collect.ImmutableList) Objects.requireNonNull(java.util.Objects.requireNonNull) TableResult(com.google.cloud.bigquery.TableResult) ConnectorSplitManager(com.facebook.presto.spi.connector.ConnectorSplitManager) NodeManager(com.facebook.presto.spi.NodeManager) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) FixedSplitSource(com.facebook.presto.spi.FixedSplitSource) String.format(java.lang.String.format) ConnectorSession(com.facebook.presto.spi.ConnectorSession) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ColumnHandle(com.facebook.presto.spi.ColumnHandle) Optional(java.util.Optional) ColumnHandle(com.facebook.presto.spi.ColumnHandle) ReadSession(com.google.cloud.bigquery.storage.v1beta1.Storage.ReadSession)

Example 23 with Stream

use of com.google.cloud.bigquery.storage.v1beta1.Storage.Stream 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 24 with Stream

use of com.google.cloud.bigquery.storage.v1beta1.Storage.Stream 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)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