Search in sources :

Example 1 with ReadRowsResponse

use of com.google.cloud.bigquery.storage.v1beta1.Storage.ReadRowsResponse in project presto by prestodb.

the class ReadRowsHelper method readRows.

// TODO: iterator based implementation, instead of fetching all result at once
public Iterator<ReadRowsResponse> readRows() {
    List<ReadRowsResponse> readRowResponses = new ArrayList<>();
    long readRowsCount = 0;
    int retries = 0;
    Iterator<ReadRowsResponse> serverResponses = fetchResponses(request);
    while (serverResponses.hasNext()) {
        try {
            ReadRowsResponse response = serverResponses.next();
            readRowsCount += response.getRowCount();
            readRowResponses.add(response);
        } catch (RuntimeException e) {
            // if relevant, retry the read, from the last read position
            if (BigQueryUtil.isRetryable(e) && retries < maxReadRowsRetries) {
                request.getReadPositionBuilder().setOffset(readRowsCount);
                serverResponses = fetchResponses(request);
                retries++;
            } else {
                // to safely close the client
                try (BigQueryStorageClient ignored = client) {
                    throw e;
                }
            }
        }
    }
    return readRowResponses.iterator();
}
Also used : BigQueryStorageClient(com.google.cloud.bigquery.storage.v1beta1.BigQueryStorageClient) ReadRowsResponse(com.google.cloud.bigquery.storage.v1beta1.Storage.ReadRowsResponse) ArrayList(java.util.ArrayList)

Aggregations

BigQueryStorageClient (com.google.cloud.bigquery.storage.v1beta1.BigQueryStorageClient)1 ReadRowsResponse (com.google.cloud.bigquery.storage.v1beta1.Storage.ReadRowsResponse)1 ArrayList (java.util.ArrayList)1