问题描述
我需要帮助谷歌的BigQuery code集成到PHP。
这样我就可以执行查询等实物PHP $ C $运行C本身。
I need a help to integrate google bigquery code into the PHP.So i can execute query and other kind of operation from php code itself.
需要您的帮助和建议我的一些工作实例链接。
Need your help and suggest me some working examples link.
先谢谢了。
推荐答案
下面是一个code,它
Here is a code that
- 正确创建
Google_Client
使用https://github.com/google/google-api-php-client - 运行作业异步
- 显示正在运行的作业ID和状态
您需要有:
- 创建服务帐户(类似
... @ developer.gserviceaccount.com
) - 您的密钥文件(
的.p12
) - service_token_file_location(从握手存储JSON写的路径,它的有效期为1小时)
code样品:
code sample:
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);
您可以在这里看到,您可以使用您的其他BigQuery的呼叫类。当你阅读文件,请知道正在从其他来源生成的文件,因此它看起来很奇怪的PHP,你需要学习,以便能够使用的方法从中读取它。
You can see here the classes that you can use for your other BigQuery calls. When you read the file, please know that file is being generated from other sources, hence it looks strange for PHP, and you need to learn reading it in order to be able to use the methods from it.
https://github.com/google/google-api-php-client/blob/master/src/Google/Service/Bigquery.php
这样的:
- Google_Service_Bigquery_TableRow
还检查了标有[PHP]问题和[谷歌的BigQuery]
http://stackoverflow.com/questions/tagged/google-bigquery+php
Also check out the questions tagged with [php] and [google-bigquery]http://stackoverflow.com/questions/tagged/google-bigquery+php
这篇关于用PHP谷歌BigQuery整合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!