use of com.google.api.client.util.Data in project beam by apache.
the class BigqueryClient method insertDataToTable.
/**
* Inserts rows to a table using a BigQuery streaming write.
*/
public void insertDataToTable(String projectId, String datasetId, String tableName, List<Map<String, Object>> rows) throws IOException, InterruptedException {
Sleeper sleeper = Sleeper.DEFAULT;
BackOff backoff = BackOffAdapter.toGcpBackOff(BACKOFF_FACTORY.backoff());
IOException lastException = null;
do {
if (lastException != null) {
LOG.warn("Retrying insert table ({}) after exception", tableName, lastException);
}
try {
List<Rows> dataRows = rows.stream().map(row -> new Rows().setJson(row)).collect(Collectors.toList());
TableDataInsertAllResponse response = this.bqClient.tabledata().insertAll(projectId, datasetId, tableName, new TableDataInsertAllRequest().setRows(dataRows)).execute();
if (response != null && (response.getInsertErrors() == null || response.getInsertErrors().isEmpty())) {
LOG.info("Successfully inserted data into table : " + tableName);
return;
} else {
if (response == null || response.getInsertErrors() == null) {
lastException = new IOException("Expected valid response from insert data job, but received null.");
} else {
lastException = new IOException(String.format("Got insertion error (%s)", response.getInsertErrors().toString()));
}
}
} catch (IOException e) {
// ignore and retry
lastException = e;
}
} while (BackOffUtils.next(sleeper, backoff));
throw new RuntimeException(String.format("Unable to get BigQuery response after retrying %d times for table (%s)", MAX_QUERY_RETRIES, tableName), lastException);
}
Aggregations