Search in sources :

Example 1 with ReadSession

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

the class ReadSessionCreator method create.

public Storage.ReadSession create(TableId table, ImmutableList<String> selectedFields, Optional<String> filter, int parallelism) {
    TableInfo tableDetails = bigQueryClient.getTable(table);
    TableInfo actualTable = getActualTable(tableDetails, selectedFields, new String[] {});
    try (BigQueryStorageClient bigQueryStorageClient = bigQueryStorageClientFactory.createBigQueryStorageClient()) {
        ReadOptions.TableReadOptions.Builder readOptions = ReadOptions.TableReadOptions.newBuilder().addAllSelectedFields(selectedFields);
        filter.ifPresent(readOptions::setRowRestriction);
        TableReferenceProto.TableReference tableReference = toTableReference(actualTable.getTableId());
        Storage.ReadSession readSession = bigQueryStorageClient.createReadSession(Storage.CreateReadSessionRequest.newBuilder().setParent("projects/" + bigQueryClient.getProjectId()).setFormat(Storage.DataFormat.AVRO).setRequestedStreams(parallelism).setReadOptions(readOptions).setTableReference(tableReference).setShardingStrategy(Storage.ShardingStrategy.BALANCED).build());
        return readSession;
    }
}
Also used : Storage(com.google.cloud.bigquery.storage.v1beta1.Storage) BigQueryStorageClient(com.google.cloud.bigquery.storage.v1beta1.BigQueryStorageClient) TableReferenceProto(com.google.cloud.bigquery.storage.v1beta1.TableReferenceProto) TableInfo(com.google.cloud.bigquery.TableInfo)

Example 2 with ReadSession

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

Aggregations

Logger (com.facebook.airlift.log.Logger)1 BIGQUERY_FAILED_TO_EXECUTE_QUERY (com.facebook.presto.plugin.bigquery.BigQueryErrorCode.BIGQUERY_FAILED_TO_EXECUTE_QUERY)1 ColumnHandle (com.facebook.presto.spi.ColumnHandle)1 ConnectorSession (com.facebook.presto.spi.ConnectorSession)1 ConnectorSplitSource (com.facebook.presto.spi.ConnectorSplitSource)1 ConnectorTableLayoutHandle (com.facebook.presto.spi.ConnectorTableLayoutHandle)1 FixedSplitSource (com.facebook.presto.spi.FixedSplitSource)1 NodeManager (com.facebook.presto.spi.NodeManager)1 PrestoException (com.facebook.presto.spi.PrestoException)1 ConnectorSplitManager (com.facebook.presto.spi.connector.ConnectorSplitManager)1 ConnectorTransactionHandle (com.facebook.presto.spi.connector.ConnectorTransactionHandle)1 BigQueryException (com.google.cloud.bigquery.BigQueryException)1 TableId (com.google.cloud.bigquery.TableId)1 TableInfo (com.google.cloud.bigquery.TableInfo)1 TableResult (com.google.cloud.bigquery.TableResult)1 BigQueryStorageClient (com.google.cloud.bigquery.storage.v1beta1.BigQueryStorageClient)1 Storage (com.google.cloud.bigquery.storage.v1beta1.Storage)1 ReadSession (com.google.cloud.bigquery.storage.v1beta1.Storage.ReadSession)1 TableReferenceProto (com.google.cloud.bigquery.storage.v1beta1.TableReferenceProto)1 ImmutableList (com.google.common.collect.ImmutableList)1