Java 将数据插入BigQuery表

Java 将数据插入BigQuery表,java,google-app-engine,google-bigquery,google-cloud-platform,Java,Google App Engine,Google Bigquery,Google Cloud Platform,基本上,我的代码看起来像官方的示例 我的代码: TableRow data = new TableRow(); data.set("type", eventType); data.set("timestamp", new Date()); TableDataInsertAllRequest.Rows row = new TableDataInsertAllRequest.Rows(); row.setInsertId(System.currentTimeMillis()); row.setJs

基本上,我的代码看起来像官方的示例

我的代码:

TableRow data = new TableRow();
data.set("type", eventType);
data.set("timestamp", new Date());

TableDataInsertAllRequest.Rows row = new TableDataInsertAllRequest.Rows();
row.setInsertId(System.currentTimeMillis());
row.setJson(data);
request = new TableDataInsertAllRequest();
request.setRows(Arrays.asList(row));

TableDataInsertAllResponse response = bigquery.tabledata().insertAll(projectId, datasetId, tableId, request).execute();
for (TableDataInsertAllResponse.InsertErrors err: response.getInsertErrors()) {
    for (ErrorProto ep: err.getErrors()) {
        log.error(ep.getReason() + " : " + ep.getMessage() + " at " + ep.getLocation());
    }
}
但我有一个错误:

invalid : JSON map specified for non-record field at null
似乎我遗漏了什么,但不知道我的代码出了什么问题。我有两个字段,一个
字符串
日期
,错误消息对我来说没有任何意义


如何在BigQuery表中插入数据?

经过几个小时的调试后,我发现BigQuery Java客户端不支持
日期
值。应该使用
com.google.api.client.util.DateTime
包装器

因此,与其

data.set("timestamp", new Date());
应该有:

data.set("timestamp", new DateTime(new Date()));

希望它能帮助某人。

@AlexMartelli可能会接受,但他必须等待48小时才能接受。我遇到了以下错误:为非记录字段指定了JSON对象:时间戳。。。“时间戳”字段的类型是什么?我的是DATE,但应该是DATE\u TIME吗?如果我尝试使用您提到的内容,我会遇到一个异常:
“消息”:“此字段不是记录”,“原因”:“无效”
@IoTuser我还发现Java API不喜欢DateTime,并将抛出该异常。我让它工作的唯一方法是使用UTC以来的浮点秒数。然而,我的表期望的是时间戳而不是日期,因此您的里程数可能会有所不同。我的代码:tableRow.set(“时间戳”,毫秒nceUTC/1000.0d);你好,你能回答这个问题吗