Search in sources :

Example 1 with BigQueryReadClient

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

the class BigQueryClientFactoryTest method testGetReadClientWithUserAgent.

@Test
public void testGetReadClientWithUserAgent() {
    BigQueryClientFactory clientFactory = new BigQueryClientFactory(bigQueryCredentialsSupplier, headerProvider, bigQueryConfig);
    when(bigQueryConfig.getBigQueryProxyConfig()).thenReturn(bigQueryProxyConfig);
    BigQueryReadClient readClient = clientFactory.getBigQueryReadClient();
    assertNotNull(readClient);
    BigQueryClientFactory clientFactory2 = new BigQueryClientFactory(bigQueryCredentialsSupplier, headerProvider, bigQueryConfig);
    when(bigQueryConfig.getBigQueryProxyConfig()).thenReturn(bigQueryProxyConfig);
    BigQueryReadClient readClient2 = clientFactory2.getBigQueryReadClient();
    assertNotNull(readClient2);
    assertSame(readClient, readClient2);
    BigQueryClientFactory clientFactory3 = new BigQueryClientFactory(bigQueryCredentialsSupplier, HttpUtil.createHeaderProvider(bigQueryConfig, "test-agent-2"), bigQueryConfig);
    when(bigQueryConfig.getBigQueryProxyConfig()).thenReturn(bigQueryProxyConfig);
    BigQueryReadClient readClient3 = clientFactory3.getBigQueryReadClient();
    assertNotNull(readClient3);
    assertNotSame(readClient, readClient3);
    assertNotSame(readClient2, readClient3);
}
Also used : BigQueryReadClient(com.google.cloud.bigquery.storage.v1.BigQueryReadClient) Test(org.junit.Test)

Example 2 with BigQueryReadClient

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

the class BigQueryClientFactoryTest method testGetReadClientWithServiceAccountCredentials.

@Test
public void testGetReadClientWithServiceAccountCredentials() {
    when(bigQueryCredentialsSupplier.getCredentials()).thenReturn(createServiceAccountCredentials("test-client-id"));
    BigQueryClientFactory clientFactory = new BigQueryClientFactory(bigQueryCredentialsSupplier, headerProvider, bigQueryConfig);
    when(bigQueryConfig.getBigQueryProxyConfig()).thenReturn(bigQueryProxyConfig);
    BigQueryReadClient readClient = clientFactory.getBigQueryReadClient();
    assertNotNull(readClient);
    when(bigQueryCredentialsSupplier.getCredentials()).thenReturn(createServiceAccountCredentials("test-client-id"));
    BigQueryClientFactory clientFactory2 = new BigQueryClientFactory(bigQueryCredentialsSupplier, headerProvider, bigQueryConfig);
    when(bigQueryConfig.getBigQueryProxyConfig()).thenReturn(bigQueryProxyConfig);
    BigQueryReadClient readClient2 = clientFactory2.getBigQueryReadClient();
    assertNotNull(readClient2);
    assertSame(readClient, readClient2);
    when(bigQueryCredentialsSupplier.getCredentials()).thenReturn(createServiceAccountCredentials("test-client-id-2"));
    BigQueryClientFactory clientFactory3 = new BigQueryClientFactory(bigQueryCredentialsSupplier, headerProvider, bigQueryConfig);
    when(bigQueryConfig.getBigQueryProxyConfig()).thenReturn(bigQueryProxyConfig);
    BigQueryReadClient readClient3 = clientFactory3.getBigQueryReadClient();
    assertNotNull(readClient3);
    assertNotSame(readClient, readClient3);
    assertNotSame(readClient2, readClient3);
}
Also used : BigQueryReadClient(com.google.cloud.bigquery.storage.v1.BigQueryReadClient) Test(org.junit.Test)

Example 3 with BigQueryReadClient

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

the class BigQueryClientFactoryTest method testGetReadClientWithBigQueryConfig.

@Test
public void testGetReadClientWithBigQueryConfig() {
    BigQueryClientFactory clientFactory = new BigQueryClientFactory(bigQueryCredentialsSupplier, headerProvider, new TestBigQueryConfig(Optional.of("US:8080")));
    BigQueryReadClient readClient = clientFactory.getBigQueryReadClient();
    assertNotNull(readClient);
    BigQueryClientFactory clientFactory2 = new BigQueryClientFactory(bigQueryCredentialsSupplier, headerProvider, new TestBigQueryConfig(Optional.of("US:8080")));
    BigQueryReadClient readClient2 = clientFactory2.getBigQueryReadClient();
    assertNotNull(readClient2);
    assertSame(readClient, readClient2);
    BigQueryClientFactory clientFactory3 = new BigQueryClientFactory(bigQueryCredentialsSupplier, headerProvider, new TestBigQueryConfig(Optional.of("EU:8080")));
    BigQueryReadClient readClient3 = clientFactory3.getBigQueryReadClient();
    assertNotNull(readClient3);
    assertNotSame(readClient, readClient3);
    assertNotSame(readClient2, readClient3);
}
Also used : BigQueryReadClient(com.google.cloud.bigquery.storage.v1.BigQueryReadClient) Test(org.junit.Test)

Example 4 with BigQueryReadClient

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

the class ReadSessionCreator method create.

/**
 * Creates a new ReadSession for parallel reads.
 *
 * <p>Some attributes are governed by the {@link ReadSessionCreatorConfig} that this object was
 * constructed with.
 *
 * @param table The table to create the session for.
 * @param selectedFields
 * @param filter
 * @return
 */
public ReadSessionResponse create(TableId table, ImmutableList<String> selectedFields, Optional<String> filter) {
    TableInfo tableDetails = bigQueryClient.getTable(table);
    TableInfo actualTable = getActualTable(tableDetails, selectedFields, filter);
    StandardTableDefinition tableDefinition = actualTable.getDefinition();
    BigQueryReadClient bigQueryReadClient = bigQueryReadClientFactory.getBigQueryReadClient();
    String tablePath = toTablePath(actualTable.getTableId());
    CreateReadSessionRequest request = config.getRequestEncodedBase().map(value -> {
        try {
            return com.google.cloud.bigquery.storage.v1.CreateReadSessionRequest.parseFrom(java.util.Base64.getDecoder().decode(value));
        } catch (com.google.protobuf.InvalidProtocolBufferException e) {
            throw new RuntimeException("Couldn't decode:" + value, e);
        }
    }).orElse(CreateReadSessionRequest.newBuilder().build());
    ReadSession.Builder requestedSession = request.getReadSession().toBuilder();
    config.getTraceId().ifPresent(traceId -> requestedSession.setTraceId(traceId));
    TableReadOptions.Builder readOptions = requestedSession.getReadOptionsBuilder();
    if (!isInputTableAView(tableDetails)) {
        filter.ifPresent(readOptions::setRowRestriction);
    }
    readOptions.addAllSelectedFields(selectedFields);
    readOptions.setArrowSerializationOptions(ArrowSerializationOptions.newBuilder().setBufferCompression(config.getArrowCompressionCodec()).build());
    ReadSession readSession = bigQueryReadClient.createReadSession(request.newBuilder().setParent("projects/" + bigQueryClient.getProjectId()).setReadSession(requestedSession.setDataFormat(config.getReadDataFormat()).setReadOptions(readOptions).setTable(tablePath).build()).setMaxStreamCount(getMaxNumPartitionsRequested(config.getMaxParallelism(), tableDefinition)).build());
    return new ReadSessionResponse(readSession, actualTable);
}
Also used : TableDefinition(com.google.cloud.bigquery.TableDefinition) Logger(org.slf4j.Logger) StandardTableDefinition(com.google.cloud.bigquery.StandardTableDefinition) ReadSession(com.google.cloud.bigquery.storage.v1.ReadSession) TableId(com.google.cloud.bigquery.TableId) LoggerFactory(org.slf4j.LoggerFactory) BigQueryReadClient(com.google.cloud.bigquery.storage.v1.BigQueryReadClient) OptionalInt(java.util.OptionalInt) String.format(java.lang.String.format) UNSUPPORTED(com.google.cloud.bigquery.connector.common.BigQueryErrorCode.UNSUPPORTED) Stream(java.util.stream.Stream) ImmutableList(com.google.common.collect.ImmutableList) TableReadOptions(com.google.cloud.bigquery.storage.v1.ReadSession.TableReadOptions) ArrowSerializationOptions(com.google.cloud.bigquery.storage.v1.ArrowSerializationOptions) Optional(java.util.Optional) TableInfo(com.google.cloud.bigquery.TableInfo) CreateReadSessionRequest(com.google.cloud.bigquery.storage.v1.CreateReadSessionRequest) ReadSession(com.google.cloud.bigquery.storage.v1.ReadSession) BigQueryReadClient(com.google.cloud.bigquery.storage.v1.BigQueryReadClient) TableInfo(com.google.cloud.bigquery.TableInfo) TableReadOptions(com.google.cloud.bigquery.storage.v1.ReadSession.TableReadOptions) StandardTableDefinition(com.google.cloud.bigquery.StandardTableDefinition) CreateReadSessionRequest(com.google.cloud.bigquery.storage.v1.CreateReadSessionRequest)

Example 5 with BigQueryReadClient

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

the class TestReadRowsHelper method testNoFailures.

@Test
public void testNoFailures() {
    BigQueryReadClient client = mock(BigQueryReadClient.class);
    MockResponsesBatch batch1 = new MockResponsesBatch();
    batch1.addResponse(ReadRowsResponse.newBuilder().setRowCount(10).build());
    batch1.addResponse(ReadRowsResponse.newBuilder().setRowCount(11).build());
    // so we can run multiple tests
    List<ReadRowsResponse> responses = ImmutableList.copyOf(new MockReadRowsHelper(client, "test", 3, ImmutableList.of(batch1)).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) BigQueryReadClient(com.google.cloud.bigquery.storage.v1.BigQueryReadClient) Test(org.testng.annotations.Test)

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