Search in sources :

Example 1 with BigQueryWriteClient

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

the class BigQueryClientFactoryTest method testGetWriteClientWithUserAgent.

@Test
public void testGetWriteClientWithUserAgent() {
    BigQueryClientFactory clientFactory = new BigQueryClientFactory(bigQueryCredentialsSupplier, headerProvider, bigQueryConfig);
    when(bigQueryConfig.getBigQueryProxyConfig()).thenReturn(bigQueryProxyConfig);
    BigQueryWriteClient writeClient = clientFactory.getBigQueryWriteClient();
    assertNotNull(writeClient);
    BigQueryClientFactory clientFactory2 = new BigQueryClientFactory(bigQueryCredentialsSupplier, headerProvider, bigQueryConfig);
    when(bigQueryConfig.getBigQueryProxyConfig()).thenReturn(bigQueryProxyConfig);
    BigQueryWriteClient writeClient2 = clientFactory2.getBigQueryWriteClient();
    assertNotNull(writeClient2);
    assertSame(writeClient, writeClient2);
    BigQueryClientFactory clientFactory3 = new BigQueryClientFactory(bigQueryCredentialsSupplier, HttpUtil.createHeaderProvider(bigQueryConfig, "test-agent-2"), bigQueryConfig);
    when(bigQueryConfig.getBigQueryProxyConfig()).thenReturn(bigQueryProxyConfig);
    BigQueryWriteClient writeClient3 = clientFactory3.getBigQueryWriteClient();
    assertNotNull(writeClient3);
    assertNotSame(writeClient, writeClient3);
    assertNotSame(writeClient2, writeClient3);
}
Also used : BigQueryWriteClient(com.google.cloud.bigquery.storage.v1.BigQueryWriteClient) Test(org.junit.Test)

Example 2 with BigQueryWriteClient

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

the class BigQueryClientFactoryTest method testGetWriteClientWithBigQueryConfig.

@Test
public void testGetWriteClientWithBigQueryConfig() {
    BigQueryClientFactory clientFactory = new BigQueryClientFactory(bigQueryCredentialsSupplier, headerProvider, new TestBigQueryConfig(Optional.of("US:8080")));
    BigQueryWriteClient writeClient = clientFactory.getBigQueryWriteClient();
    assertNotNull(writeClient);
    BigQueryClientFactory clientFactory2 = new BigQueryClientFactory(bigQueryCredentialsSupplier, headerProvider, new TestBigQueryConfig(Optional.of("US:8080")));
    BigQueryWriteClient writeClient2 = clientFactory2.getBigQueryWriteClient();
    assertNotNull(writeClient2);
    assertSame(writeClient, writeClient2);
    BigQueryClientFactory clientFactory3 = new BigQueryClientFactory(bigQueryCredentialsSupplier, headerProvider, new TestBigQueryConfig(Optional.of("EU:8080")));
    BigQueryWriteClient writeClient3 = clientFactory3.getBigQueryWriteClient();
    assertNotNull(writeClient3);
    assertNotSame(writeClient, writeClient3);
    assertNotSame(writeClient2, writeClient3);
}
Also used : BigQueryWriteClient(com.google.cloud.bigquery.storage.v1.BigQueryWriteClient) Test(org.junit.Test)

Example 3 with BigQueryWriteClient

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

the class BigQueryClientFactoryTest method testGetWriteClientWithServiceAccountCredentials.

@Test
public void testGetWriteClientWithServiceAccountCredentials() throws Exception {
    when(bigQueryCredentialsSupplier.getCredentials()).thenReturn(createServiceAccountCredentials("test-client-id"));
    BigQueryClientFactory clientFactory = new BigQueryClientFactory(bigQueryCredentialsSupplier, headerProvider, bigQueryConfig);
    when(bigQueryConfig.getBigQueryProxyConfig()).thenReturn(bigQueryProxyConfig);
    BigQueryWriteClient writeClient = clientFactory.getBigQueryWriteClient();
    assertNotNull(writeClient);
    when(bigQueryCredentialsSupplier.getCredentials()).thenReturn(createServiceAccountCredentials("test-client-id"));
    BigQueryClientFactory clientFactory2 = new BigQueryClientFactory(bigQueryCredentialsSupplier, headerProvider, bigQueryConfig);
    when(bigQueryConfig.getBigQueryProxyConfig()).thenReturn(bigQueryProxyConfig);
    BigQueryWriteClient writeClient2 = clientFactory2.getBigQueryWriteClient();
    assertNotNull(writeClient2);
    assertSame(writeClient, writeClient2);
    when(bigQueryCredentialsSupplier.getCredentials()).thenReturn(createServiceAccountCredentials("test-client-id-2"));
    BigQueryClientFactory clientFactory3 = new BigQueryClientFactory(bigQueryCredentialsSupplier, headerProvider, bigQueryConfig);
    when(bigQueryConfig.getBigQueryProxyConfig()).thenReturn(bigQueryProxyConfig);
    BigQueryWriteClient writeClient3 = clientFactory3.getBigQueryWriteClient();
    assertNotNull(writeClient3);
    assertNotSame(writeClient, writeClient3);
    assertNotSame(writeClient2, writeClient3);
}
Also used : BigQueryWriteClient(com.google.cloud.bigquery.storage.v1.BigQueryWriteClient) Test(org.junit.Test)

Example 4 with BigQueryWriteClient

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

the class BigQueryClientFactory method getBigQueryWriteClient.

public BigQueryWriteClient getBigQueryWriteClient() {
    synchronized (writeClientMap) {
        if (!writeClientMap.containsKey(this)) {
            BigQueryWriteClient bigQueryWriteClient = createBigQueryWriteClient();
            Runtime.getRuntime().addShutdownHook(new Thread(() -> shutdownBigQueryWriteClient(bigQueryWriteClient)));
            writeClientMap.put(this, bigQueryWriteClient);
        }
    }
    return writeClientMap.get(this);
}
Also used : BigQueryWriteClient(com.google.cloud.bigquery.storage.v1.BigQueryWriteClient)

Example 5 with BigQueryWriteClient

use of com.google.cloud.bigquery.storage.v1.BigQueryWriteClient in project java-bigquerystorage by googleapis.

the class ParallelWriteCommittedStream method createStream.

private WriteStream createStream(String projectId, String datasetName, String tableName, BigQueryWriteClient client) {
    LOG.info("Creating a new stream");
    // Initialize a write stream for the specified table.
    // For more information on WriteStream.Type, see:
    // https://googleapis.dev/java/google-cloud-bigquerystorage/latest/com/google/cloud/bigquery/storage/v1/WriteStream.Type.html
    WriteStream stream = WriteStream.newBuilder().setType(WriteStream.Type.COMMITTED).build();
    TableName parentTable = TableName.of(projectId, datasetName, tableName);
    CreateWriteStreamRequest createWriteStreamRequest = CreateWriteStreamRequest.newBuilder().setParent(parentTable.toString()).setWriteStream(stream).build();
    return client.createWriteStream(createWriteStreamRequest);
}
Also used : TableName(com.google.cloud.bigquery.storage.v1.TableName) CreateWriteStreamRequest(com.google.cloud.bigquery.storage.v1.CreateWriteStreamRequest) WriteStream(com.google.cloud.bigquery.storage.v1.WriteStream)

Aggregations

BigQueryWriteClient (com.google.cloud.bigquery.storage.v1.BigQueryWriteClient)8 WriteStream (com.google.cloud.bigquery.storage.v1.WriteStream)5 AppendRowsResponse (com.google.cloud.bigquery.storage.v1.AppendRowsResponse)4 CreateWriteStreamRequest (com.google.cloud.bigquery.storage.v1.CreateWriteStreamRequest)4 TableName (com.google.cloud.bigquery.storage.v1.TableName)4 Test (org.junit.Test)4 JsonStreamWriter (com.google.cloud.bigquery.storage.v1.JsonStreamWriter)3 ExecutionException (java.util.concurrent.ExecutionException)3 JSONArray (org.json.JSONArray)3 JSONObject (org.json.JSONObject)3 FinalizeWriteStreamRequest (com.google.cloud.bigquery.storage.v1.FinalizeWriteStreamRequest)2 BQTableSchemaToProtoDescriptor (com.google.cloud.bigquery.storage.v1.BQTableSchemaToProtoDescriptor)1 BatchCommitWriteStreamsRequest (com.google.cloud.bigquery.storage.v1.BatchCommitWriteStreamsRequest)1 BatchCommitWriteStreamsResponse (com.google.cloud.bigquery.storage.v1.BatchCommitWriteStreamsResponse)1 FinalizeWriteStreamResponse (com.google.cloud.bigquery.storage.v1.FinalizeWriteStreamResponse)1 FlushRowsRequest (com.google.cloud.bigquery.storage.v1.FlushRowsRequest)1 FlushRowsResponse (com.google.cloud.bigquery.storage.v1.FlushRowsResponse)1 ProtoSchema (com.google.cloud.bigquery.storage.v1.ProtoSchema)1 StorageError (com.google.cloud.bigquery.storage.v1.StorageError)1 StreamWriter (com.google.cloud.bigquery.storage.v1.StreamWriter)1