9

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?

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.