Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BigQuery Java API to read an Array of Record : "Retrieving field value by name is not supported" exception

My current table in BigQuery has a column that uses complex types. The "family" column is actually a list ("repeated" feature) of records (with 2 fields: id & name).

When I try to get the 1st "id" value of 1 row with the following syntax:

FieldValueList c = qr.getValues().iterator().next();    
c.get("family").getRepeatedValue().get(0).getRecordValue().get("id");

I get the exception:

Method threw 'java.lang.UnsupportedOperationException' exception.
Retrieving field value by name is not supported when there is no fields schema provided

This is a bit annoying because my table has a clearly defined schema. And when I do the "read" query with the same Java call, I can also see that this schema is correctly found:

qr.getSchema().getFields().get("family").getSubFields().toString();
-->
[Field{name=id, type=INTEGER, mode=NULLABLE, description=null}, Field{name=name, type=STRING, mode=NULLABLE, description=null}]

Due to this exception, the workaround that I have found is to pass the "index" of the record field instead of giving it its name

c.get("family").getRepeatedValue().get(0).getRecordValue().get(0).getLongValue();

However, this seeks awkward to pass an index instead of a name.

Is there a better way to get the value of a field in a record inside an array (if my column is only a record, without array, then I don't get the exception) ?

Is this exception normal?

like image 505
Sourygna Avatar asked Jan 18 '18 11:01

Sourygna


1 Answers

You can wrap the unnamed FieldValueList with a named one using the "of" static method:

FieldList subSchema = qr.getSchema().getFields().get("family").getSubFields();
FieldValueList c = qr.getValues().iterator().next();
FieldValueList.of(
  c.get("family").getRepeatedValue().get(0).getRecordValue(),
  subSchema).get("id");

The "of" method takes a FieldValueList (returned by getRecordValue() in this case) and a FieldList (subSchema here), and returns the same FieldValueList but with named access.

like image 91
Ofri Mann Avatar answered Oct 23 '22 19:10

Ofri Mann