Google-cloud-java: bigquery: support loading a POJO or Map like GSON does

Created on 3 Nov 2016  路  2Comments  路  Source: googleapis/google-cloud-java

The code for processing the results of a query is a bit convoluted.

Example from https://github.com/GoogleCloudPlatform/java-docs-samples/blob/2e3f285b7dccfd978b14033f9869e728e8c13e56/bigquery/cloud-client/src/main/java/com/example/bigquery/SimpleApp.java#L53

    QueryResult result = response.result();

    while (result != null) {
      Iterator<List<FieldValue>> iter = result.iterateAll();

      while (iter.hasNext()) {
        List<FieldValue> row = iter.next();
        List<FieldValue> titles = row.get(0).repeatedValue();
        System.out.println("titles:");

        for (FieldValue titleValue : titles) {
          List<FieldValue> titleRecord = titleValue.recordValue();
          String title = titleRecord.get(0).stringValue();
          long uniqueWords = titleRecord.get(1).longValue();
          System.out.printf("\t%s: %d\n", title, uniqueWords);
        }

        long uniqueWords = row.get(1).longValue();
        System.out.printf("total unique words: %d\n", uniqueWords);
      }

      result = result.nextPage();

For a fixed query, I'd love to be able to fill in the object the way that I can in GSON. Then I'd be able to do something like:

    QueryResult result = response.result();

    while (result != null) {
      Iterator<List<FieldValue>> iter = result.iterateAll();

      while (iter.hasNext()) {
        List<FieldValue> row = iter.next();
        // I can't do this yet, but I'd love to be able to load an object
        // like I can with GSON.
        // I'm thinking maybe it only makes sense per-row rather than for
        // a whole query, due to paging.
        WordCounts counts = row.as(WordCounts.class);
        System.out.println("titles:");

        for (Title title : counts.getTitles()) {
          System.out.printf("\t%s: %d\n", title.getTitle(), title.getUniqueWords());
        }

        System.out.printf("total unique words: %d\n", counts.getTotalWords());
      }

      result = result.nextPage();

For context, here is a similar issue in the Go libraries: https://github.com/GoogleCloudPlatform/google-cloud-go/issues/399

bigquery p2 triaged for GA feature request

Most helpful comment

@tswast yeah this would be nice to have. It's in the roadmap but we haven't had time to work on it yet.

All 2 comments

@tswast yeah this would be nice to have. It's in the roadmap but we haven't had time to work on it yet.

This has been added to our feature backlog: https://github.com/GoogleCloudPlatform/google-cloud-java/wiki/Feature-backlog. This issue will be closed but is linked in the backlog and can continue to be used for comment and discussion.

Was this page helpful?
0 / 5 - 0 ratings