gpt4 book ai didi

google-bigquery - BigQuery Java API 读取记录数组 : "Retrieving field value by name is not supported" exception

转载 作者:行者123 更新时间:2023-12-01 04:44:11 32 4
gpt4 key购买 nike

我在 BigQuery 中的当前表有一个使用复杂类型的列。 “家庭”列实际上是一个记录列表(“重复”功能)(有 2 个字段:id 和 name)。

当我尝试使用以下语法获取 1 行的第一个“id”值时:

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

我得到了异常(exception):
Method threw 'java.lang.UnsupportedOperationException' exception.
Retrieving field value by name is not supported when there is no fields schema provided

这有点烦人,因为我的表有一个明确定义的架构。当我使用相同的 Java 调用执行“读取”查询时,我还可以看到正确找到了此架构:
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}]

由于此异常,我发现的解决方法是传递记录字段的“索引”而不是为其命名
c.get("family").getRepeatedValue().get(0).getRecordValue().get(0).getLongValue();

然而,这寻求传递一个索引而不是一个名称很尴尬。

有没有更好的方法来获取数组内记录中字段的值(如果我的列只是一个记录,没有数组,那么我不会得到异常)?

这个异常正常吗?

最佳答案

您可以使用“of”静态方法将未命名的 FieldValueList 与命名的 FieldValueList 包装起来:

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");

“of”方法采用 FieldValueList(在本例中由 getRecordValue() 返回)和 FieldList(此处为 subSchema),并返回相同的 FieldValueList,但具有命名访问权限。

关于google-bigquery - BigQuery Java API 读取记录数组 : "Retrieving field value by name is not supported" exception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48320495/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com