Search in sources :

Example 11 with BigQueryReadClient

use of com.google.cloud.bigquery.storage.v1.BigQueryReadClient in project spark-bigquery-connector by GoogleCloudDataproc.

the class ReadRowsHelper method readRows.

public Iterator<ReadRowsResponse> readRows() {
    BigQueryReadClient client = bigQueryReadClientFactory.getBigQueryReadClient();
    incomingStream = new StreamCombiningIterator(client, requests, options.prebufferResponses, options.getMaxReadRowsRetries());
    return incomingStream;
}
Also used : BigQueryReadClient(com.google.cloud.bigquery.storage.v1.BigQueryReadClient)

Example 12 with BigQueryReadClient

use of com.google.cloud.bigquery.storage.v1.BigQueryReadClient in project trino by trinodb.

the class BigQueryReadClientFactory method create.

BigQueryReadClient create(ConnectorSession session) {
    Optional<Credentials> credentials = credentialsSupplier.getCredentials(session);
    try {
        BigQueryReadSettings.Builder clientSettings = BigQueryReadSettings.newBuilder().setTransportChannelProvider(BigQueryReadSettings.defaultGrpcTransportProviderBuilder().setHeaderProvider(headerProvider).build());
        credentials.ifPresent(value -> clientSettings.setCredentialsProvider(FixedCredentialsProvider.create(value)));
        return BigQueryReadClient.create(clientSettings.build());
    } catch (IOException e) {
        throw new UncheckedIOException("Error creating BigQueryReadClient", e);
    }
}
Also used : BigQueryReadSettings(com.google.cloud.bigquery.storage.v1.BigQueryReadSettings) UncheckedIOException(java.io.UncheckedIOException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) Credentials(com.google.auth.Credentials)

Example 13 with BigQueryReadClient

use of com.google.cloud.bigquery.storage.v1.BigQueryReadClient in project trino by trinodb.

the class TestReadRowsHelper method testRetryOfSingleFailure.

@Test
public void testRetryOfSingleFailure() {
    BigQueryReadClient client = mock(BigQueryReadClient.class);
    MockResponsesBatch batch1 = new MockResponsesBatch();
    batch1.addResponse(ReadRowsResponse.newBuilder().setRowCount(10).build());
    batch1.addException(new StatusRuntimeException(Status.INTERNAL.withDescription("Received unexpected EOS on DATA frame from server.")));
    MockResponsesBatch batch2 = new MockResponsesBatch();
    batch2.addResponse(ReadRowsResponse.newBuilder().setRowCount(11).build());
    List<ReadRowsResponse> responses = ImmutableList.copyOf(new MockReadRowsHelper(client, "test", 3, ImmutableList.of(batch1, batch2)).readRows());
    assertThat(responses.size()).isEqualTo(2);
    assertThat(responses.stream().mapToLong(ReadRowsResponse::getRowCount).sum()).isEqualTo(21);
}
Also used : ReadRowsResponse(com.google.cloud.bigquery.storage.v1.ReadRowsResponse) StatusRuntimeException(io.grpc.StatusRuntimeException) BigQueryReadClient(com.google.cloud.bigquery.storage.v1.BigQueryReadClient) Test(org.testng.annotations.Test)

Example 14 with BigQueryReadClient

use of com.google.cloud.bigquery.storage.v1.BigQueryReadClient in project trino by trinodb.

the class ReadSessionCreator method create.

public ReadSession create(ConnectorSession session, TableId remoteTable, List<String> selectedFields, Optional<String> filter, int parallelism) {
    BigQueryClient client = bigQueryClientFactory.create(session);
    TableInfo tableDetails = client.getTable(remoteTable).orElseThrow(() -> new TableNotFoundException(new SchemaTableName(remoteTable.getDataset(), remoteTable.getTable())));
    TableInfo actualTable = getActualTable(client, tableDetails, selectedFields);
    List<String> filteredSelectedFields = selectedFields.stream().filter(BigQueryUtil::validColumnName).collect(toList());
    try (BigQueryReadClient bigQueryReadClient = bigQueryReadClientFactory.create(session)) {
        ReadSession.TableReadOptions.Builder readOptions = ReadSession.TableReadOptions.newBuilder().addAllSelectedFields(filteredSelectedFields);
        filter.ifPresent(readOptions::setRowRestriction);
        ReadSession readSession = bigQueryReadClient.createReadSession(CreateReadSessionRequest.newBuilder().setParent("projects/" + client.getProjectId()).setReadSession(ReadSession.newBuilder().setDataFormat(DataFormat.AVRO).setTable(toTableResourceName(actualTable.getTableId())).setReadOptions(readOptions)).setMaxStreamCount(parallelism).build());
        return readSession;
    }
}
Also used : TableNotFoundException(io.trino.spi.connector.TableNotFoundException) ReadSession(com.google.cloud.bigquery.storage.v1.ReadSession) TableInfo(com.google.cloud.bigquery.TableInfo) SchemaTableName(io.trino.spi.connector.SchemaTableName) BigQueryReadClient(com.google.cloud.bigquery.storage.v1.BigQueryReadClient)

Aggregations

BigQueryReadClient (com.google.cloud.bigquery.storage.v1.BigQueryReadClient)12 ReadRowsResponse (com.google.cloud.bigquery.storage.v1.ReadRowsResponse)4 ReadSession (com.google.cloud.bigquery.storage.v1.ReadSession)4 Test (org.junit.Test)4 CreateReadSessionRequest (com.google.cloud.bigquery.storage.v1.CreateReadSessionRequest)3 TableReadOptions (com.google.cloud.bigquery.storage.v1.ReadSession.TableReadOptions)3 TableInfo (com.google.cloud.bigquery.TableInfo)2 BigQueryReadSettings (com.google.cloud.bigquery.storage.v1.BigQueryReadSettings)2 ReadRowsRequest (com.google.cloud.bigquery.storage.v1.ReadRowsRequest)2 TableModifiers (com.google.cloud.bigquery.storage.v1.ReadSession.TableModifiers)2 Timestamp (com.google.protobuf.Timestamp)2 IOException (java.io.IOException)2 UncheckedIOException (java.io.UncheckedIOException)2 Test (org.testng.annotations.Test)2 InstantiatingGrpcChannelProvider (com.google.api.gax.grpc.InstantiatingGrpcChannelProvider)1 Credentials (com.google.auth.Credentials)1 StandardTableDefinition (com.google.cloud.bigquery.StandardTableDefinition)1 TableDefinition (com.google.cloud.bigquery.TableDefinition)1 TableId (com.google.cloud.bigquery.TableId)1 UNSUPPORTED (com.google.cloud.bigquery.connector.common.BigQueryErrorCode.UNSUPPORTED)1