Search in sources :

Example 1 with Data

use of com.google.api.client.util.Data in project beam by apache.

the class BigqueryClient method insertDataToTable.

/**
 * Inserts rows to a table using a BigQuery streaming write.
 */
public void insertDataToTable(String projectId, String datasetId, String tableName, List<Map<String, Object>> rows) throws IOException, InterruptedException {
    Sleeper sleeper = Sleeper.DEFAULT;
    BackOff backoff = BackOffAdapter.toGcpBackOff(BACKOFF_FACTORY.backoff());
    IOException lastException = null;
    do {
        if (lastException != null) {
            LOG.warn("Retrying insert table ({}) after exception", tableName, lastException);
        }
        try {
            List<Rows> dataRows = rows.stream().map(row -> new Rows().setJson(row)).collect(Collectors.toList());
            TableDataInsertAllResponse response = this.bqClient.tabledata().insertAll(projectId, datasetId, tableName, new TableDataInsertAllRequest().setRows(dataRows)).execute();
            if (response != null && (response.getInsertErrors() == null || response.getInsertErrors().isEmpty())) {
                LOG.info("Successfully inserted data into table : " + tableName);
                return;
            } else {
                if (response == null || response.getInsertErrors() == null) {
                    lastException = new IOException("Expected valid response from insert data job, but received null.");
                } else {
                    lastException = new IOException(String.format("Got insertion error (%s)", response.getInsertErrors().toString()));
                }
            }
        } catch (IOException e) {
            // ignore and retry
            lastException = e;
        }
    } while (BackOffUtils.next(sleeper, backoff));
    throw new RuntimeException(String.format("Unable to get BigQuery response after retrying %d times for table (%s)", MAX_QUERY_RETRIES, tableName), lastException);
}
Also used : DatasetReference(com.google.api.services.bigquery.model.DatasetReference) LoggerFactory(org.slf4j.LoggerFactory) Random(java.util.Random) Data(com.google.api.client.util.Data) QueryRequest(com.google.api.services.bigquery.model.QueryRequest) Transport(org.apache.beam.sdk.extensions.gcp.util.Transport) Map(java.util.Map) TableRow(com.google.api.services.bigquery.model.TableRow) TableSchema(com.google.api.services.bigquery.model.TableSchema) Internal(org.apache.beam.sdk.annotations.Internal) Collection(java.util.Collection) HttpTransport(com.google.api.client.http.HttpTransport) Credentials(com.google.auth.Credentials) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) TableDataInsertAllResponse(com.google.api.services.bigquery.model.TableDataInsertAllResponse) TableDataInsertAllRequest(com.google.api.services.bigquery.model.TableDataInsertAllRequest) HttpCredentialsAdapter(com.google.auth.http.HttpCredentialsAdapter) ImmutableList(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList) BackOff(com.google.api.client.util.BackOff) Dataset(com.google.api.services.bigquery.model.Dataset) TableList(com.google.api.services.bigquery.model.TableList) Duration(org.joda.time.Duration) Rows(com.google.api.services.bigquery.model.TableDataInsertAllRequest.Rows) Tables(com.google.api.services.bigquery.model.TableList.Tables) GetQueryResultsResponse(com.google.api.services.bigquery.model.GetQueryResultsResponse) BackOffAdapter(org.apache.beam.sdk.extensions.gcp.util.BackOffAdapter) Preconditions.checkArgument(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkArgument) Job(com.google.api.services.bigquery.model.Job) Nonnull(javax.annotation.Nonnull) Sleeper(com.google.api.client.util.Sleeper) Nullable(org.checkerframework.checker.nullness.qual.Nullable) QueryResponse(com.google.api.services.bigquery.model.QueryResponse) TableReference(com.google.api.services.bigquery.model.TableReference) TableFieldSchema(com.google.api.services.bigquery.model.TableFieldSchema) FluentBackoff(org.apache.beam.sdk.util.FluentBackoff) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) ClassInfo(com.google.api.client.util.ClassInfo) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) Lists(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Lists) IOException(java.io.IOException) Table(com.google.api.services.bigquery.model.Table) BackOffUtils(com.google.api.client.util.BackOffUtils) Bigquery(com.google.api.services.bigquery.Bigquery) Preconditions.checkState(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkState) JsonFactory(com.google.api.client.json.JsonFactory) JobConfigurationQuery(com.google.api.services.bigquery.model.JobConfigurationQuery) BigqueryScopes(com.google.api.services.bigquery.BigqueryScopes) JobConfiguration(com.google.api.services.bigquery.model.JobConfiguration) TableDataInsertAllRequest(com.google.api.services.bigquery.model.TableDataInsertAllRequest) TableDataInsertAllResponse(com.google.api.services.bigquery.model.TableDataInsertAllResponse) Sleeper(com.google.api.client.util.Sleeper) IOException(java.io.IOException) BackOff(com.google.api.client.util.BackOff) Rows(com.google.api.services.bigquery.model.TableDataInsertAllRequest.Rows)

Aggregations

HttpTransport (com.google.api.client.http.HttpTransport)1 JsonFactory (com.google.api.client.json.JsonFactory)1 BackOff (com.google.api.client.util.BackOff)1 BackOffUtils (com.google.api.client.util.BackOffUtils)1 ClassInfo (com.google.api.client.util.ClassInfo)1 Data (com.google.api.client.util.Data)1 Sleeper (com.google.api.client.util.Sleeper)1 Bigquery (com.google.api.services.bigquery.Bigquery)1 BigqueryScopes (com.google.api.services.bigquery.BigqueryScopes)1 Dataset (com.google.api.services.bigquery.model.Dataset)1 DatasetReference (com.google.api.services.bigquery.model.DatasetReference)1 GetQueryResultsResponse (com.google.api.services.bigquery.model.GetQueryResultsResponse)1 Job (com.google.api.services.bigquery.model.Job)1 JobConfiguration (com.google.api.services.bigquery.model.JobConfiguration)1 JobConfigurationQuery (com.google.api.services.bigquery.model.JobConfigurationQuery)1 QueryRequest (com.google.api.services.bigquery.model.QueryRequest)1 QueryResponse (com.google.api.services.bigquery.model.QueryResponse)1 Table (com.google.api.services.bigquery.model.Table)1 TableDataInsertAllRequest (com.google.api.services.bigquery.model.TableDataInsertAllRequest)1 Rows (com.google.api.services.bigquery.model.TableDataInsertAllRequest.Rows)1