为什么即使我设置了maxresults,我也会使用Google BigQuery的PHP库获取所有行?

时间:2017-07-10 23:12:44

标签: php google-bigquery

我正在尝试对结果进行分页,因为它超过了1M行。我正在使用PHP版的BigQuery。但即使我把10放在maxresults仍然给我所有的结果。或者我做错了。

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

1 个答案:

答案 0 :(得分:0)

QueryResults.rows()函数返回Google\Cloud\Core\Iterator\ItemIterator。循环浏览时会自动获取下一页。设置maxResults = 10意味着它一次只能获取10个,但是当你循环时它仍将获取所有页面。

您可以使用

手动访问第一页
$rows -> $queryResults->rows($options);
$firstPage -> $rows->pageIterator->current();