1

我正在使用的数据有一个嵌套字段“位置”,如下所示:

"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."
 }
4

1 回答 1

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);
于 2013-04-08T22:28:04.143 回答