Search in sources :

Example 26 with FieldValue

use of com.google.cloud.bigquery.FieldValue in project java-docs-samples by GoogleCloudPlatform.

the class QuerySample method runQuery.

// [END query_config_batch]
// [START run_query]
public static void runQuery(QueryJobConfiguration queryConfig) throws TimeoutException, InterruptedException {
    BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
    // Create a job ID so that we can safely retry.
    JobId jobId = JobId.of(UUID.randomUUID().toString());
    Job queryJob = bigquery.create(JobInfo.newBuilder(queryConfig).setJobId(jobId).build());
    // Wait for the query to complete.
    queryJob = queryJob.waitFor();
    // Check for errors
    if (queryJob == null) {
        throw new RuntimeException("Job no longer exists");
    } else if (queryJob.getStatus().getError() != null) {
        // errors, not just the latest one.
        throw new RuntimeException(queryJob.getStatus().getError().toString());
    }
    // Get the results.
    TableResult result = queryJob.getQueryResults();
    // Print all pages of the results.
    while (result != null) {
        for (List<FieldValue> row : result.iterateAll()) {
            for (FieldValue val : row) {
                System.out.printf("%s,", val.toString());
            }
            System.out.printf("\n");
        }
        result = result.getNextPage();
    }
}
Also used : BigQuery(com.google.cloud.bigquery.BigQuery) TableResult(com.google.cloud.bigquery.TableResult) FieldValue(com.google.cloud.bigquery.FieldValue) Job(com.google.cloud.bigquery.Job) JobId(com.google.cloud.bigquery.JobId)

Example 27 with FieldValue

use of com.google.cloud.bigquery.FieldValue in project java-docs-samples by GoogleCloudPlatform.

the class QueryParametersSample method runArray.

// [END bigquery_query_params]
/**
 * Query the baby names database to find the most popular names for a gender in a list of states.
 */
// [START bigquery_query_params_arrays]
private static void runArray(String gender, String[] states) throws InterruptedException {
    BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
    String queryString = "SELECT name, sum(number) as count\n" + "FROM `bigquery-public-data.usa_names.usa_1910_2013`\n" + "WHERE gender = @gender\n" + "AND state IN UNNEST(@states)\n" + "GROUP BY name\n" + "ORDER BY count DESC\n" + "LIMIT 10;";
    QueryJobConfiguration queryRequest = QueryJobConfiguration.newBuilder(queryString).addNamedParameter("gender", QueryParameterValue.string(gender)).addNamedParameter("states", QueryParameterValue.array(states, String.class)).setUseLegacySql(false).build();
    // Execute the query.
    TableResult result = bigquery.query(queryRequest);
    // Print all pages of the results.
    while (result != null) {
        for (List<FieldValue> row : result.iterateAll()) {
            System.out.printf("%s: %d\n", row.get(0).getStringValue(), row.get(1).getLongValue());
        }
        result = result.getNextPage();
    }
}
Also used : BigQuery(com.google.cloud.bigquery.BigQuery) TableResult(com.google.cloud.bigquery.TableResult) FieldValue(com.google.cloud.bigquery.FieldValue) QueryJobConfiguration(com.google.cloud.bigquery.QueryJobConfiguration)

Example 28 with FieldValue

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

the class WorkspacesControllerTest method stubBigQueryCohortCalls.

private void stubBigQueryCohortCalls() {
    QueryResult queryResult = mock(QueryResult.class);
    Iterable testIterable = new Iterable() {

        @Override
        public Iterator iterator() {
            List<FieldValue> list = new ArrayList<>();
            list.add(null);
            return list.iterator();
        }
    };
    Map<String, Integer> rm = ImmutableMap.<String, Integer>builder().put("person_id", 0).put("birth_datetime", 1).put("gender_concept_id", 2).put("race_concept_id", 3).put("ethnicity_concept_id", 4).put("count", 5).build();
    when(bigQueryService.filterBigQueryConfig(null)).thenReturn(null);
    when(bigQueryService.executeQuery(null)).thenReturn(queryResult);
    when(bigQueryService.getResultMapper(queryResult)).thenReturn(rm);
    when(queryResult.iterateAll()).thenReturn(testIterable);
    when(bigQueryService.getLong(null, 0)).thenReturn(0L);
    when(bigQueryService.getString(null, 1)).thenReturn("1");
    when(bigQueryService.getLong(null, 2)).thenReturn(0L);
    when(bigQueryService.getLong(null, 3)).thenReturn(0L);
    when(bigQueryService.getLong(null, 4)).thenReturn(0L);
    when(bigQueryService.getLong(null, 5)).thenReturn(0L);
}
Also used : QueryResult(com.google.cloud.bigquery.QueryResult) ArrayList(java.util.ArrayList) FieldValue(com.google.cloud.bigquery.FieldValue) Matchers.anyString(org.mockito.Matchers.anyString)

Example 29 with FieldValue

use of com.google.cloud.bigquery.FieldValue 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)

Example 30 with FieldValue

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

the class CohortMaterializationService method materializeCohort.

public MaterializeCohortResponse materializeCohort(@Nullable CohortReview cohortReview, SearchRequest searchRequest, MaterializeCohortRequest request) {
    long offset = 0L;
    FieldSet fieldSet = request.getFieldSet();
    List<CohortStatus> statusFilter = request.getStatusFilter();
    String paginationToken = request.getPageToken();
    int pageSize = request.getPageSize();
    // TODO: add CDR version ID here
    Object[] paginationParameters = new Object[] { searchRequest, statusFilter };
    if (paginationToken != null) {
        PaginationToken token = PaginationToken.fromBase64(paginationToken);
        if (token.matchesParameters(paginationParameters)) {
            offset = token.getOffset();
        } else {
            throw new BadRequestException(String.format("Use of pagination token %s with new parameter values", paginationToken));
        }
    }
    int limit = pageSize + 1;
    if (statusFilter == null) {
        statusFilter = ALL_STATUSES;
    }
    ParticipantCriteria criteria;
    MaterializeCohortResponse response = new MaterializeCohortResponse();
    if (statusFilter.contains(CohortStatus.NOT_REVIEWED)) {
        Set<Long> participantIdsToExclude;
        if (statusFilter.size() < CohortStatus.values().length) {
            // Find the participant IDs that have statuses which *aren't* in the filter.
            Set<CohortStatus> statusesToExclude = Sets.difference(ImmutableSet.copyOf(CohortStatus.values()), ImmutableSet.copyOf(statusFilter));
            participantIdsToExclude = getParticipantIdsWithStatus(cohortReview, ImmutableList.copyOf(statusesToExclude));
        } else {
            participantIdsToExclude = ImmutableSet.of();
        }
        criteria = new ParticipantCriteria(searchRequest, participantIdsToExclude);
    } else {
        Set<Long> participantIds = getParticipantIdsWithStatus(cohortReview, statusFilter);
        if (participantIds.isEmpty()) {
            // return an empty response.
            return response;
        }
        criteria = new ParticipantCriteria(participantIds);
    }
    TableQueryAndConfig tableQueryAndConfig = getTableQueryAndConfig(fieldSet);
    QueryJobConfiguration jobConfiguration = fieldSetQueryBuilder.buildQuery(criteria, tableQueryAndConfig, limit, offset);
    QueryResult result;
    try {
        result = bigQueryService.executeQuery(bigQueryService.filterBigQueryConfig(jobConfiguration));
    } catch (BigQueryException e) {
        if (e.getCode() == HttpServletResponse.SC_SERVICE_UNAVAILABLE) {
            throw new ServerUnavailableException("BigQuery was temporarily unavailable, try again later", e);
        } else if (e.getCode() == HttpServletResponse.SC_FORBIDDEN) {
            throw new ForbiddenException("Access to the CDR is denied", e);
        } else {
            throw new ServerErrorException(String.format("An unexpected error occurred materializing the cohort with " + "query = (%s), params = (%s)", jobConfiguration.getQuery(), jobConfiguration.getNamedParameters()), e);
        }
    }
    Map<String, Integer> rm = bigQueryService.getResultMapper(result);
    int numResults = 0;
    boolean hasMoreResults = false;
    ArrayList<Object> results = new ArrayList<>();
    for (List<FieldValue> row : result.iterateAll()) {
        if (numResults == pageSize) {
            hasMoreResults = true;
            break;
        }
        Map<String, Object> resultMap = fieldSetQueryBuilder.extractResults(tableQueryAndConfig, row);
        results.add(resultMap);
        numResults++;
    }
    response.setResults(results);
    if (hasMoreResults) {
        // TODO: consider pagination based on cursor / values rather than offset
        PaginationToken token = PaginationToken.of(offset + pageSize, paginationParameters);
        response.setNextPageToken(token.toBase64());
    }
    return response;
}
Also used : ServerUnavailableException(org.pmiops.workbench.exceptions.ServerUnavailableException) ArrayList(java.util.ArrayList) MaterializeCohortResponse(org.pmiops.workbench.model.MaterializeCohortResponse) FieldSet(org.pmiops.workbench.model.FieldSet) ParticipantIdAndCohortStatus(org.pmiops.workbench.db.model.ParticipantIdAndCohortStatus) CohortStatus(org.pmiops.workbench.model.CohortStatus) QueryResult(com.google.cloud.bigquery.QueryResult) FieldValue(com.google.cloud.bigquery.FieldValue) QueryJobConfiguration(com.google.cloud.bigquery.QueryJobConfiguration) PaginationToken(org.pmiops.workbench.utils.PaginationToken) ForbiddenException(org.pmiops.workbench.exceptions.ForbiddenException) TableQueryAndConfig(org.pmiops.workbench.cohortbuilder.TableQueryAndConfig) BadRequestException(org.pmiops.workbench.exceptions.BadRequestException) ParticipantCriteria(org.pmiops.workbench.cohortbuilder.ParticipantCriteria) BigQueryException(com.google.cloud.bigquery.BigQueryException) ServerErrorException(org.pmiops.workbench.exceptions.ServerErrorException)

Aggregations

FieldValue (com.google.cloud.bigquery.FieldValue)31 QueryResult (com.google.cloud.bigquery.QueryResult)13 Test (org.junit.Test)10 QueryJobConfiguration (com.google.cloud.bigquery.QueryJobConfiguration)9 QueryResponse (com.google.cloud.bigquery.QueryResponse)9 QueryRequest (com.google.cloud.bigquery.QueryRequest)8 List (java.util.List)8 TableId (com.google.cloud.bigquery.TableId)7 BigQuery (com.google.cloud.bigquery.BigQuery)5 ArrayList (java.util.ArrayList)5 ParticipantCriteria (org.pmiops.workbench.cohortbuilder.ParticipantCriteria)5 Job (com.google.cloud.bigquery.Job)4 TableResult (com.google.cloud.bigquery.TableResult)4 ImmutableList (com.google.common.collect.ImmutableList)4 InsertAllResponse (com.google.cloud.bigquery.InsertAllResponse)3 Schema (com.google.cloud.bigquery.Schema)3 TableInfo (com.google.cloud.bigquery.TableInfo)3 SimpleDateFormat (java.text.SimpleDateFormat)3 HashMap (java.util.HashMap)3 PageRequest (org.pmiops.workbench.cohortreview.util.PageRequest)3