无需登录身份验证即可运行BigQuery

时间:2015-06-09 13:02:45

标签: php authentication google-bigquery

我试图通过php语言运行bigquery。 首先,我能够通过身份验证成功运行bigquery(首先是它要求登录而不是查询将运行)。 我的问题是我可以在没有身份验证的情况下在bigquery中运行查询吗? 提前致谢

2 个答案:

答案 0 :(得分:2)

这是一个代码

你需要:

  • 已创建服务帐户(类似...@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 :(得分:0)

必须对所有对BigQuery API的请求进行身份验证。您可以使用标准OAuth流作为普通用户帐户进行身份验证,也可以将其作为后台任务的服务帐户进行身份验证(请参阅Pentium10的答案)。