The code for processing the results of a query is a bit convoluted.
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
@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.
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.