通过 BigQuery 库发送的时间戳对象返回错误“此字段不是记录”

Mar*_*ima 3 datetime google-bigquery

当将日期字段作为对象发送到类型为时间戳的 BigQuery 表时,Google Java API 库不会引发异常,但不会出现数据着陆。检查“InsertAllResponse”响应类型返回包括错误“此字段不是记录”。

例如

Hashmap<String,Object> rowContent = new Hashmap<>();
rowContent.put("Time", new Date());
rowContent.put("Name", "Harry");
Run Code Online (Sandbox Code Playgroud)

进而

BigQuery bq = BigQueryOptions.getDefaultInstance().getService();
TableId tableId = TableId.of(datasetName, tableName);
InsertAllRequest.Builder insertReqBuilder = InsertAllRequest.newBuilder(tableId);
insertReqBuilder.addRow({some string}, row);
InsertAllResponse response = bigquery.insertAll(insertReqBuilder.build());
Run Code Online (Sandbox Code Playgroud)

返回一个response.hasErrors() true。

还报告了python 这里和firebase 这里和javascript 这里

Mar*_*ima 5

看来,将日期作为对象发送会导致客户端 API 创建 JSON 记录而不是单个字段(这还表明日期时间类型尚未显式映射,因此可能会引入时区问题)。

相反,以 1970 年以来的 UTC秒数发送日期/时间,即修改上述内容:

Hashmap<String,Object> rowContent = new Hashmap<>();
rowContent.put("Time", Math.floor(new Date().getTime()/1000));
rowContent.put("Name", "Harry");
Run Code Online (Sandbox Code Playgroud)

(注意:不确定如何处理毫秒,请参见例如BigQuery not deal with timestamp in millisecond with partition columns,我会找出并返回)

  • 是的,发送作为即时或任何日期对象归档的日期,BIG Query API 会将日期对象构造为 JSON 空对象(示例 date_field :{})。因此将日期对象转换为字符串。 (2认同)