首页 > 解决方案 > 通过 BigQuery 库发送的时间戳对象返回错误“此字段不是记录”

问题描述

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

例如

Hashmap<String,Object> rowContent = new Hashmap<>();
rowContent.put("Time", new Date());
rowContent.put("Name", "Harry");

接着

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

返回一个 response.hasErrors() true。

还报告了此处的python 和此处firebase 以及此处javascript

标签: datetimegoogle-bigquery

解决方案


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

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

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

(注意:不知道如何处理毫秒,例如BigQuery not handling timestamp in millisecond with partition column,我会找出并回来)


推荐阅读