使用php脚本在bigquery中运行SQL查询

时间:2017-11-14 06:00:54

标签: php google-bigquery

我尝试使用php脚本运行sql查询,但我收到错误消息

  

致命错误:调用未定义的方法   谷歌\云\大量查询\ BigQueryClient :: runQueryAsJob()

我在这里解释我的代码

use Google\Cloud\BigQuery\BigQueryClient;
use Google\Cloud\Core\ExponentialBackoff;
putenv('GOOGLE_APPLICATION_CREDENTIALS=key.json');
$projectId="MY PROJECT ID";

$bigQuery = new BigQueryClient([
    'projectId' => $projectId,
        ]);

function run_query_as_job($projectId, $query, $useLegacySql) {
    $bigQuery = new BigQueryClient([
        'projectId' => $projectId,
    ]);
    $job = $bigQuery->runQueryAsJob(
            $query, ['jobConfig' => ['useLegacySql' => $useLegacySql]]);
    $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();

    if ($queryResults->isComplete()) {
        $i = 0;
        $rows = $queryResults->rows();
        foreach ($rows as $row) {
            printf('--- Row %s ---' . PHP_EOL, ++$i);
            foreach ($row as $column => $value) {
                printf('%s: %s' . PHP_EOL, $column, $value);
            }
        }
        printf('Found %s row(s)' . PHP_EOL, $i);
    } else {
        throw new Exception('The query failed to complete');
    }
}

$query = "SELECT * FROM MY_DATASETID.TABLENAME LIMIT 1000";
run_query_as_job($projectId, $query, TRUE/FALSE); // Here i have try both Used TRUE/FALSE

这里还定义了参考链接 pls open link

请帮助我。

0 个答案:

没有答案