Google BigQuery与PHP的集成


Google BigQuery integration with PHP

我需要一个帮助,以整合谷歌bigquery代码到PHP。所以我可以从php代码本身执行查询和其他类型的操作。

需要你的帮助,并建议我一些工作的例子链接。

下面的代码

  • 使用https://github.com/google/google-api-php-client
  • 正确创建Google_Client
  • 异步运行作业
  • 显示正在运行的作业ID和状态

你需要有:

  • 服务帐户创建(类似...@developer.gserviceaccount.com)
  • 您的密钥文件(.p12)
  • service_token_file_location(可写路径,用于存储握手后的JSON,有效期为1小时)

代码示例:

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来说看起来很奇怪,你需要学会阅读它,以便能够使用它的方法。

https://github.com/google/google-api-php-client/blob/master/src/Google/Service/Bigquery.php

:

  • Google_Service_Bigquery_TableRow

也可以查看标有[php]和[google-bigquery]的问题https://stackoverflow.com/questions/tagged/google-bigquery + php

您可以使用以下步骤

  1. 在Cloud Console的项目选择器页面中,选择或创建一个Cloud项目
  2. 启用BigQuery API
  3. 设置认证
  4. 在云控制台中,进入"创建业务帐号密钥"页面。
  5. 将环境变量GOOGLE_APPLICATION_CREDENTIALS设置为包含服务帐户密钥的JSON文件的路径。这个变量只适用于当前的shell会话,所以如果您打开一个新会话,请再次设置该变量

完整指南请参阅此处的文档