我正在使用的数据有一个嵌套字段'location',如下所示:
"location": {
"city": "Amherst",
"region": "NS",
"country": "CA"
},
如何使用Java API指定嵌套字段的架构?
目前,我的代码如下:
List<TableFieldSchema> fields = new ArrayList<TableFieldSchema>();
TableFieldSchema fieldLocation = new TableFieldSchema();
fieldFoo.setName("location");
fieldFoo.setType("record");
TableFieldSchema fieldLocationCity = new TableFieldSchema();
fieldBar.setName("location.city");
fieldBar.setType("string");
...
fields.add(fieldLocation);
fields.add(fieldLocationCity);
TableSchema schema = new TableSchema();
schema.setFields(fields);
这不起作用,因为我收到以下错误:
CONFIG: {
"error": {
"errors": [
{
"domain": "global",
"reason": "invalid",
"message": "Record field location must have a schema."
}
],
"code": 400,
"message": "Record field location must have a schema."
}
答案 0 :(得分:3)
我认为您想要做以下事情:
List<TableFieldSchema> inner = new ArrayList<TableFieldSchema>();
List<TableFieldSchema> outer = new ArrayList<TableFieldSchema>();
TableFieldSchema fieldLocationCity = new TableFieldSchema();
fieldLocationCity.setName("city");
fieldLocationCity.setType("string");
// Add the inner fields to the list of fields in the record.
inner.add(fieldLocationCity);
...(add region & country, etc)...
TableFieldSchema fieldLocation = new TableFieldSchema();
fieldLocation.setName("location");
fieldLocation.setType("record");
// Add the inner fields to the location record.
fieldLocation.setFields(inner);
outer.add(fieldLocation);
TableSchema schema = new TableSchema();
schema.setFields(outer);