我正在尝试使用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
,因为示例代码说要做,但在登录浏览器后,错误仍然存在。任何帮助都会很棒,谢谢!
答案 0 :(得分:4)
您非常接近,只需要设置服务帐户默认凭据即可看到包含putenv
和useApplicationDefaultCredentials()
的行。这是我使用库https://github.com/google/google-api-php-client的工作代码您需要从控制台获取服务帐户密钥文件: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);