简体   繁体   中英

Why do I get all rows with the PHP library for Google BigQuery, even when I set maxresults?

I am trying to paginate the results since its more than 1M rows. I am using the PHP version of BigQuery. but even if I put 10 as maxresults still gives me all the results. or am I doing it wrong.

function run_query_as_job($query, $maxResults = 10, $startIndex = 0)
{

    $options = [
        'maxResults' => $maxResults,
        'startIndex' => $startIndex
    ];
    $bigQuery = new BigQueryClient([
        'projectId' => 'xxx',
        'keyFilePath' => 'xxxx-21c721cefe2c.json'
    ]);
    $job = $bigQuery->runQueryAsJob(
        $query,
        ['jobConfig' => ['useLegacySql' => true]]);

    $backoff = new ExponentialBackoff(10);
    $backoff->execute(function () use ($job) {
        print('Waiting for job to complete' . PHP_EOL);
        $job->reload();
        if (!$job->isComplete()) {
            throw new Exception('Job has not yet completed', 500);
        }
    });
    $queryResults = $job->queryResults($options);

    print_r($options);
    if ($queryResults->isComplete()) {
        $i = 0;
        $rows = $queryResults->rows($options);
        foreach ($rows as $row) {
            printf('--- Row %s ---' . "<br>", ++$i);
        }
        printf('Found %s row(s)' . "<br>", $i);


    } else {
        throw new Exception('The query failed to complete');
    }
}

The QueryResults.rows() function returns a Google\\Cloud\\Core\\Iterator\\ItemIterator . This automatically gets the next page when you loop through it. Setting maxResults = 10 means that it only fetches 10 at a time, but it will still fetch all pages when you loop through.

You can manually access just the first page with

$rows -> $queryResults->rows($options);
$firstPage -> $rows->pageIterator->current();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM