PHP中当前Google API的BigQuery示例
所有关于示例的问题都有两年的历史了,我找不到任何适用于当前API客户机版本()的工作示例。每个人都遗漏了一些东西或抛出异常 谁能提供一个有效的例子?在任何地方都没有任何文档 这是最有效的一个:PHP中当前Google API的BigQuery示例,php,google-bigquery,google-api-php-client,Php,Google Bigquery,Google Api Php Client,所有关于示例的问题都有两年的历史了,我找不到任何适用于当前API客户机版本()的工作示例。每个人都遗漏了一些东西或抛出异常 谁能提供一个有效的例子?在任何地方都没有任何文档 这是最有效的一个: <?php require_once 'inc/google-api/autoload.php'; // or wherever autoload.php is located $client = new Google_Client(); $client->setAp
<?php
require_once 'inc/google-api/autoload.php'; // or wherever autoload.php is located
$client = new Google_Client();
$client->setApplicationName("whatever");
$client->setDeveloperKey("some key");
$service = new Google_Service_Bigquery($client);
$postBody = "[
'datasetReference' => [
'datasetId' => $datasetId,
'projectId' => $projectId,
],
'friendlyName' => $name,
'description' => $description,
'access' => [
['role' => 'READER', 'specialGroup' => 'projectReaders'],
['role' => 'WRITER', 'specialGroup' => 'projectWriters'],
['role' => 'OWNER', 'specialGroup' => 'projectOwners'],
],
]";
$dataset = $service->datasets->insert($projectId, new Google_Dataset($postBody));
$postBody = "[
'tableReference' => [
'projectId' => 'test_project_id',
'datasetId' => 'test_data_set',
'tableId' => 'test_data_table'
]
]";
$table = $service->tables->insert($projectId, $datasetId, new Google_Table($postBody));
?>
这是一个
- 正确创建
Google\u客户端
- 异步运行作业
- 显示正在运行的作业ID和状态
您需要:
- 已创建服务帐户(类似于
…@developer.gserviceaccount.com
)
- 您的密钥文件(
.p12
)
- 服务\u令牌\u文件\u位置(用于存储握手产生的JSON的可写路径,有效期为1h)
代码示例:
function getGoogleClient($data = null) {
global $service_token_file_location, $key_file_location, $service_account_name;
$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
$old_service_token = null;
$service_token = @file_get_contents($service_token_file_location);
$client->setAccessToken($service_token);
$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials(
$service_account_name, array(
'https://www.googleapis.com/auth/bigquery',
'https://www.googleapis.com/auth/devstorage.full_control'
), $key
);
$client->setAssertionCredentials($cred);
if ($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($cred);
$service_token = $client->getAccessToken();
}
return $client;
}
$client = getGoogleClient();
$bq = new Google_Service_Bigquery($client);
/**
* @see https://developers.google.com/bigquery/docs/reference/v2/jobs#resource
*/
$job = new Google_Service_Bigquery_Job();
$config = new Google_Service_Bigquery_JobConfiguration();
$config->setDryRun(false);
$queryConfig = new Google_Service_Bigquery_JobConfigurationQuery();
$config->setQuery($queryConfig);
$job->setConfiguration($config);
$destinationTable = new Google_Service_Bigquery_TableReference();
$destinationTable->setDatasetId(DATASET_ID);
$destinationTable->setProjectId(PROJECT_ID);
$destinationTable->setTableId('table1');
$queryConfig->setDestinationTable($destinationTable);
$sql = "select * from publicdata:samples.github_timeline limit 10";
$queryConfig->setQuery($sql);
try {
// print_r($job);
// exit;
$job = $bq->jobs->insert(PROJECT_ID, $job);
$status = new Google_Service_Bigquery_JobStatus();
$status = $job->getStatus();
// print_r($status);
if ($status->count() != 0) {
$err_res = $status->getErrorResult();
die($err_res->getMessage());
}
} catch (Google_Service_Exception $e) {
echo $e->getMessage();
exit;
}
//print_r($job);
$jr = $job->getJobReference();
//var_dump($jr);
$jobId = $jr['jobId'];
if ($status)
$state = $status['state'];
echo 'JOBID:' . $jobId . " ";
echo 'STATUS:' . $state;
您可以通过以下方式获取结果:
$res = $bq->jobs->getQueryResults(PROJECT_ID, $_GET['jobId'], array('timeoutMs' => 1000));
if (!$res->jobComplete) {
echo "Job not yet complete";
exit;
}
echo "<p>Total rows: " . $res->totalRows . "</p>\r\n";
//see the results made it as an object ok
//print_r($res);
$rows = $res->getRows();
$r = new Google_Service_Bigquery_TableRow();
$a = array();
foreach ($rows as $r) {
$r = $r->getF();
$temp = array();
foreach ($r as $v) {
$temp[] = $v->v;
}
$a[] = $temp;
}
print_r($a);
$res=$bq->jobs->getQueryResults(项目ID,$\u GET['jobId',数组('timeoutMs'=>1000));
如果(!$res->jobComplete){
回应“作业尚未完成”;
出口
}
echo“总行数:”$res->totalRows。“\r\n”;
//查看将其作为对象生成的结果ok
//印刷品(港币);;
$rows=$res->getRows();
$r=新的Google_服务_Bigquery_TableRow();
$a=数组();
foreach($r行){
$r=$r->getF();
$temp=array();
外汇($r为$v){
$temp[]=$v->v;
}
$a[]=$temp;
}
印刷费($a);
您可以在这里看到可用于其他BigQuery调用的类。当您阅读该文件时,请知道该文件是从其他来源生成的,因此对于PHP来说它看起来很奇怪,您需要学习如何阅读它,以便能够使用其中的方法
比如:
- Google\u服务\u Bigquery\u表格行
这不是答案,因为我知道如何与API本身通信,但不知道如何与BigQuery通信。它比您想象的要复杂,我不想要Oauth方法,而是KEY one…但它不是公钥,而是服务器密钥。这些不同。您的代码无法工作,因为您没有进行身份验证。你还没有包括谷歌php客户端库。我有。。。请注意,新版本的加载完全不同。自动加载器就是这样做的。使用服务器密钥,您不需要任何进一步的身份验证-这就是开发人员控制台所说的。。。我已经设置了$service\u account\u name='xxx.apps.googleusercontent.com'代码>$key_file_location='yyy.p12'代码>$service\u token\u file\u location='tmp'代码>(目录可写)。。。但获取错误:致命错误:未捕获异常“Google\u Auth\u exception”,在…/OAuth2.php:180中显示消息“无法对令牌进行json解码”。。。我做错了什么?(当然,这些是开发者控制台中的真实变量)$service\u token\u file\u location
='tmp'必须是一个文件,而不是目录。确定,但在我将其更改为可写文件时仍然存在相同的错误。。。我想它还没到使用它的时候……我只是运行了上面的代码,它就工作了。可能缺少一些权限或PHP依赖项,但它可以工作。可能我提供了错误的服务帐户?我的不是电子邮件。。。好。。。把它改成emal一点也没有改变。