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



我正在尝试对结果进行分页,因为它的行超过 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');
}
}

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

您可以手动访问第一页

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

最新更新