Search in sources :

Example 1 with BigQueryException

use of com.google.cloud.bigquery.BigQueryException in project google-cloud-java by GoogleCloudPlatform.

the class ITBigQueryTest method testUpdateNonExistingTable.

@Test
public void testUpdateNonExistingTable() {
    TableInfo tableInfo = TableInfo.of(TableId.of(DATASET, "test_update_non_existing_table"), StandardTableDefinition.of(SIMPLE_SCHEMA));
    try {
        bigquery.update(tableInfo);
        fail("BigQueryException was expected");
    } catch (BigQueryException e) {
        BigQueryError error = e.getError();
        assertNotNull(error);
        assertEquals("notFound", error.getReason());
        assertNotNull(error.getMessage());
    }
}
Also used : BigQueryError(com.google.cloud.bigquery.BigQueryError) TableInfo(com.google.cloud.bigquery.TableInfo) BigQueryException(com.google.cloud.bigquery.BigQueryException) Test(org.junit.Test)

Example 2 with BigQueryException

use of com.google.cloud.bigquery.BigQueryException in project google-cloud-java by GoogleCloudPlatform.

the class HttpBigQueryRpc method write.

@Override
public Job write(String uploadId, byte[] toWrite, int toWriteOffset, long destOffset, int length, boolean last) {
    try {
        GenericUrl url = new GenericUrl(uploadId);
        HttpRequest httpRequest = bigquery.getRequestFactory().buildPutRequest(url, new ByteArrayContent(null, toWrite, toWriteOffset, length));
        httpRequest.setParser(bigquery.getObjectParser());
        long limit = destOffset + length;
        StringBuilder range = new StringBuilder("bytes ");
        range.append(destOffset).append('-').append(limit - 1).append('/');
        if (last) {
            range.append(limit);
        } else {
            range.append('*');
        }
        httpRequest.getHeaders().setContentRange(range.toString());
        int code;
        String message;
        IOException exception = null;
        HttpResponse response = null;
        try {
            response = httpRequest.execute();
            code = response.getStatusCode();
            message = response.getStatusMessage();
        } catch (HttpResponseException ex) {
            exception = ex;
            code = ex.getStatusCode();
            message = ex.getStatusMessage();
        }
        if (!last && code != HTTP_RESUME_INCOMPLETE || last && !(code == HTTP_OK || code == HTTP_CREATED)) {
            if (exception != null) {
                throw exception;
            }
            throw new BigQueryException(code, message);
        }
        return last && response != null ? response.parseAs(Job.class) : null;
    } catch (IOException ex) {
        throw translate(ex);
    }
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) HttpResponse(com.google.api.client.http.HttpResponse) HttpResponseException(com.google.api.client.http.HttpResponseException) GenericUrl(com.google.api.client.http.GenericUrl) IOException(java.io.IOException) BigQueryException(com.google.cloud.bigquery.BigQueryException) ByteArrayContent(com.google.api.client.http.ByteArrayContent) Job(com.google.api.services.bigquery.model.Job)

Example 3 with BigQueryException

use of com.google.cloud.bigquery.BigQueryException in project google-cloud-java by GoogleCloudPlatform.

the class BigQuerySnippets method createJob.

/**
   * Example of creating a query job.
   */
// [TARGET create(JobInfo, JobOption...)]
// [VARIABLE "SELECT field FROM my_dataset_name.my_table_name"]
public Job createJob(String query) {
    // [START createJob]
    Job job = null;
    JobConfiguration jobConfiguration = QueryJobConfiguration.of(query);
    JobInfo jobInfo = JobInfo.of(jobConfiguration);
    try {
        job = bigquery.create(jobInfo);
    } catch (BigQueryException e) {
    // the job was not created
    }
    // [END createJob]
    return job;
}
Also used : JobInfo(com.google.cloud.bigquery.JobInfo) BigQueryException(com.google.cloud.bigquery.BigQueryException) Job(com.google.cloud.bigquery.Job) JobConfiguration(com.google.cloud.bigquery.JobConfiguration) QueryJobConfiguration(com.google.cloud.bigquery.QueryJobConfiguration)

Example 4 with BigQueryException

use of com.google.cloud.bigquery.BigQueryException in project google-cloud-java by GoogleCloudPlatform.

the class BigQuerySnippets method createDataset.

/**
   * Example of creating a dataset.
   */
// [TARGET create(DatasetInfo, DatasetOption...)]
// [VARIABLE "my_dataset_name"]
public Dataset createDataset(String datasetName) {
    // [START createDataset]
    Dataset dataset = null;
    DatasetInfo datasetInfo = DatasetInfo.newBuilder(datasetName).build();
    try {
        // the dataset was created
        dataset = bigquery.create(datasetInfo);
    } catch (BigQueryException e) {
    // the dataset was not created
    }
    // [END createDataset]
    return dataset;
}
Also used : DatasetInfo(com.google.cloud.bigquery.DatasetInfo) Dataset(com.google.cloud.bigquery.Dataset) BigQueryException(com.google.cloud.bigquery.BigQueryException)

Example 5 with BigQueryException

use of com.google.cloud.bigquery.BigQueryException in project workbench by all-of-us.

the class CohortReviewController method createParticipantCohortStatusesList.

/**
 * Helper method that builds a list of {@link ParticipantCohortStatus} from BigQuery results.
 *
 * @param cohortReviewId
 * @param result
 * @param rm
 * @return
 */
private List<ParticipantCohortStatus> createParticipantCohortStatusesList(Long cohortReviewId, QueryResult result, Map<String, Integer> rm) {
    List<ParticipantCohortStatus> participantCohortStatuses = new ArrayList<>();
    for (List<FieldValue> row : result.iterateAll()) {
        String birthDateTimeString = bigQueryService.getString(row, rm.get("birth_datetime"));
        if (birthDateTimeString == null) {
            throw new BigQueryException(500, "birth_datetime is null at position: " + rm.get("birth_datetime"));
        }
        java.util.Date birthDate = Date.from(Instant.ofEpochMilli(Double.valueOf(birthDateTimeString).longValue() * 1000));
        participantCohortStatuses.add(new ParticipantCohortStatus().participantKey(new ParticipantCohortStatusKey(cohortReviewId, bigQueryService.getLong(row, rm.get("person_id")))).status(CohortStatus.NOT_REVIEWED).birthDate(new java.sql.Date(birthDate.getTime())).genderConceptId(bigQueryService.getLong(row, rm.get("gender_concept_id"))).raceConceptId(bigQueryService.getLong(row, rm.get("race_concept_id"))).ethnicityConceptId(bigQueryService.getLong(row, rm.get("ethnicity_concept_id"))));
    }
    return participantCohortStatuses;
}
Also used : ParticipantCohortStatus(org.pmiops.workbench.db.model.ParticipantCohortStatus) ArrayList(java.util.ArrayList) ParticipantCohortStatusKey(org.pmiops.workbench.db.model.ParticipantCohortStatusKey) FieldValue(com.google.cloud.bigquery.FieldValue) BigQueryException(com.google.cloud.bigquery.BigQueryException) Date(java.sql.Date)

Aggregations

BigQueryException (com.google.cloud.bigquery.BigQueryException)7 FieldValue (com.google.cloud.bigquery.FieldValue)2 QueryJobConfiguration (com.google.cloud.bigquery.QueryJobConfiguration)2 ArrayList (java.util.ArrayList)2 Logger (com.facebook.airlift.log.Logger)1 BIGQUERY_FAILED_TO_EXECUTE_QUERY (com.facebook.presto.plugin.bigquery.BigQueryErrorCode.BIGQUERY_FAILED_TO_EXECUTE_QUERY)1 ColumnHandle (com.facebook.presto.spi.ColumnHandle)1 ConnectorSession (com.facebook.presto.spi.ConnectorSession)1 ConnectorSplitSource (com.facebook.presto.spi.ConnectorSplitSource)1 ConnectorTableLayoutHandle (com.facebook.presto.spi.ConnectorTableLayoutHandle)1 FixedSplitSource (com.facebook.presto.spi.FixedSplitSource)1 NodeManager (com.facebook.presto.spi.NodeManager)1 PrestoException (com.facebook.presto.spi.PrestoException)1 ConnectorSplitManager (com.facebook.presto.spi.connector.ConnectorSplitManager)1 ConnectorTransactionHandle (com.facebook.presto.spi.connector.ConnectorTransactionHandle)1 ByteArrayContent (com.google.api.client.http.ByteArrayContent)1 GenericUrl (com.google.api.client.http.GenericUrl)1 HttpRequest (com.google.api.client.http.HttpRequest)1 HttpResponse (com.google.api.client.http.HttpResponse)1 HttpResponseException (com.google.api.client.http.HttpResponseException)1