googleBigQuery:可捕获的致命错误:参数1传递给Google \ Cloud \ BigQuery \ BigQueryClient :: runQuery()

时间:2018-01-17 09:16:54

标签: php google-bigquery

我遇到了这个错误,请告诉我是否有任何解决方法:

  

googleBigQuery: Catchable fatal error: Argument 1 passed to

     

Google\Cloud\BigQuery\BigQueryClient::runQuery() must be an instance

     

of Google\Cloud\BigQuery\JobConfigurationInterface, string given

代码示例:

<?php

require __DIR__ . '/vendor/autoload.php';

use Google\Cloud\BigQuery\BigQueryClient;

// get the project ID as the first argument    
$projectId = 'bigquery-public-data';

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

$query = 'SELECT TOP(corpus, 10) as title, COUNT(*) as unique_words ' .
         'FROM [bigquery-public-data:samples.shakespeare]';

$options = ['useLegacySql' => true];
$queryResults = $bigQuery->runQuery($query, $options);

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

2 个答案:

答案 0 :(得分:1)

我发帖给你一个完整的例子,因为这样更容易。

您需要设置服务帐户默认凭据,并查看包含putenvuseApplicationDefaultCredentials()的行。这是我使用库https://github.com/googlecloudplatform/google-cloud-php

的工作代码

您需要从控制台获取服务帐户密钥文件:https://console.cloud.google.com/iam-admin/serviceaccounts/

<强> composer.json

{
    "require": {
        "google/cloud": "^0.13.0",
        "google/apiclient": "^2.0"
    }
}

php文件

# Imports the Google Cloud client library
use Google\Cloud\BigQuery\BigQueryClient;
use Google\Cloud\ServiceBuilder;

$query="SELECT repository_url, 
       repository_has_downloads 
FROM   [publicdata:samples.github_timeline]
LIMIT  10";
$client = new Google_Client();
putenv('GOOGLE_APPLICATION_CREDENTIALS='.dirname(__FILE__) . '/.ssh/dummyname-7f0004z148e1.json');//this can be created with other ENV mode server side
$client->useApplicationDefaultCredentials();

$builder = new ServiceBuilder([
                'projectId' => 'edited',
        ]);

        $bigQuery = $builder->bigQuery();

        $job = $bigQuery->runQueryAsJob($query);
        $info=$job->info();
//      print_r($info);
//      exit;
        $queryResults = $job->queryResults();

        /*$queryResults = $bigQuery->runQuery(
            $query,
            ['useLegacySql' => true]);*/

        if ($queryResults->isComplete()) 
        {
            $i = 0;
            $rows = $queryResults->rows();

            foreach ($rows as $row) 
            {
                $i++;

                $result[$i] = $row;
            }
        } 
        else 
        {
            throw new Exception('The query failed to complete');
        }

        print_r($result);

答案 1 :(得分:0)

Resolved by using below code..

<?php
# Includes the autoloader for libraries installed with composer
require __DIR__ . '/vendor/autoload.php';

# Imports the Google Cloud client library
use Google\Cloud\BigQuery\BigQueryClient;
use Google\Cloud\Core\ServiceBuilder;


// Authenticate using a keyfile path
$cloud = new ServiceBuilder([
    'keyFilePath' => 'keyfile.json'
]);

$bigQuery = $cloud->bigQuery();

//$query = 'SELECT commit FROM `bigquery-public-data.github_repos.commits` WHERE message = ? LIMIT 100';

$queryJobConfig = $bigQuery->query($query)
    ->parameters(['A commit message.']);
$queryResults = $bigQuery->runQuery($queryJobConfig);