0

我正在尝试使用 Firebase 设置 Google Bigquery,但遇到了一些问题。我已经gcloud安装在我的机器(MacOS Sierra)上,并在我的项目中通过 composer 安装了谷歌云。

我的项目中的以下代码:

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

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

# Your Google Cloud Platform project ID
$projectId = 'hidden here only';

# Instantiates a client
$bigquery = new BigQueryClient([
    'projectId' => $projectId
]);

# The name for the new dataset
$datasetName = 'test_dataset';

# Creates the new dataset
$dataset = $bigquery->createDataset($datasetName);

echo 'Dataset ' . $dataset->id() . ' created.';

我要做的只是通过库在 bigquery 中创建一个数据集,但由于以下错误,我无法做到:

Fatal error: Uncaught Google\Cloud\Exception\ServiceException: Could not load the default credentials. Browse to https://developers.google.com/accounts/docs/application-default-credentials for more information in /Applications/MAMP/htdocs/projects/work/bigquery-tests/vendor/google/cloud/src/RequestWrapper.php on line 219

我已经尝试gcloud beta auth applications-default login按照示例代码所说的那样运行,但是在登录浏览器后,错误仍然存​​在。任何帮助都会很棒,谢谢!

4

1 回答 1

4

您非常接近,只需要设置服务帐户默认凭据,请参见带有putenv和的行useApplicationDefaultCredentials()。这是我使用库https://github.com/google/google-api-php-client的工作代码您需要从控制台获取服务帐户密钥文件:https://console.cloud.google。 com/iam-admin/serviceaccounts/

作曲家.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);
于 2017-01-04T16:44:29.523 回答