Insert data into a BigQuery table

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

My code is:

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 get the error:

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

It seems like I missed something, but I have no idea what happened to my code. I have two fields: String and Date , and the error message does not make any sense to me.

How to insert data into a BigQuery table?

+5
source share
1 answer

After hours of debugging, I found that BigQuery Java Client does not support Date values. You must use the com.google.api.client.util.DateTime wrapper.

So instead

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

should be:

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

Hope this helps someone.

+13
source

Source: https://habr.com/ru/post/1213532/


All Articles