Search in sources :

Example 1 with BigQueryStorageClient

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

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

the class BigQueryStorageClientFactory method createBigQueryStorageClient.

BigQueryStorageClient createBigQueryStorageClient() {
    try {
        BigQueryStorageSettings.Builder clientSettings = BigQueryStorageSettings.newBuilder().setTransportChannelProvider(BigQueryStorageSettings.defaultGrpcTransportProviderBuilder().setHeaderProvider(headerProvider).build());
        credentials.ifPresent(credentials -> clientSettings.setCredentialsProvider(FixedCredentialsProvider.create(credentials)));
        return BigQueryStorageClient.create(clientSettings.build());
    } catch (IOException e) {
        throw new UncheckedIOException("Error creating BigQueryStorageClient", e);
    }
}
Also used : BigQueryStorageSettings(com.google.cloud.bigquery.storage.v1beta1.BigQueryStorageSettings) UncheckedIOException(java.io.UncheckedIOException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException)

Example 3 with BigQueryStorageClient

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

the class ReadRowsHelper method readRows.

// TODO: iterator based implementation, instead of fetching all result at once
public Iterator<ReadRowsResponse> readRows() {
    List<ReadRowsResponse> readRowResponses = new ArrayList<>();
    long readRowsCount = 0;
    int retries = 0;
    Iterator<ReadRowsResponse> serverResponses = fetchResponses(request);
    while (serverResponses.hasNext()) {
        try {
            ReadRowsResponse response = serverResponses.next();
            readRowsCount += response.getRowCount();
            readRowResponses.add(response);
        } catch (RuntimeException e) {
            // if relevant, retry the read, from the last read position
            if (BigQueryUtil.isRetryable(e) && retries < maxReadRowsRetries) {
                request.getReadPositionBuilder().setOffset(readRowsCount);
                serverResponses = fetchResponses(request);
                retries++;
            } else {
                // to safely close the client
                try (BigQueryStorageClient ignored = client) {
                    throw e;
                }
            }
        }
    }
    return readRowResponses.iterator();
}
Also used : BigQueryStorageClient(com.google.cloud.bigquery.storage.v1beta1.BigQueryStorageClient) ReadRowsResponse(com.google.cloud.bigquery.storage.v1beta1.Storage.ReadRowsResponse) ArrayList(java.util.ArrayList)

Aggregations

BigQueryStorageClient (com.google.cloud.bigquery.storage.v1beta1.BigQueryStorageClient)2 TableInfo (com.google.cloud.bigquery.TableInfo)1 BigQueryStorageSettings (com.google.cloud.bigquery.storage.v1beta1.BigQueryStorageSettings)1 Storage (com.google.cloud.bigquery.storage.v1beta1.Storage)1 ReadRowsResponse (com.google.cloud.bigquery.storage.v1beta1.Storage.ReadRowsResponse)1 TableReferenceProto (com.google.cloud.bigquery.storage.v1beta1.TableReferenceProto)1 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 ArrayList (java.util.ArrayList)1