Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert data into BigQuery table

Basically my code looks like official example at https://cloud.google.com/bigquery/streaming-data-into-bigquery

My code:

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

But I getting error:

invalid : JSON map specified for non-record field at null

Seems that I've missed something, but have no idea what's wrong with my code. I have two fields, a String and Date, and error message doesn't make any sense to me.

How to insert data in BigQuery table?

like image 374
Igor Artamonov Avatar asked Feb 17 '15 15:02

Igor Artamonov


1 Answers

After few hours of debugging I found that BigQuery Java Client doesn't support Date values. And com.google.api.client.util.DateTime wrapper should be used.

So, instead of

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

there should be:

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

Hope it will help somebody.

like image 178
Igor Artamonov Avatar answered Sep 30 '22 06:09

Igor Artamonov