【问题标题】:Google BigQuery integration with PHPGoogle BigQuery 与 PHP 的集成
【发布时间】:2015-08-09 06:24:44
【问题描述】:

我需要帮助将 google bigquery 代码集成到 PHP 中。 所以我可以从 php 代码本身执行查询和其他类型的操作。

需要您的帮助并向我推荐一些工作示例链接。

提前致谢。

【问题讨论】:

    标签: php api integration google-bigquery


    【解决方案1】:

    这是一段代码

    你需要:

    • 已创建服务帐户(类似于...@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

    【讨论】:

    • 我被代码卡住了。你能帮我吗? --- 致命错误:在 C:\xampp\htdocs\big02\google-api-php-client\src\Google\Auth\OAuth2.php:179 中带有消息“无法 json 解码令牌”的未捕获异常“Google_Auth_Exception”堆栈跟踪:#0 C:\xampp\htdocs\big02\google-api-php-client\src\Google\Client.php(215): Google_Auth_OAuth2->setAccessToken(false) #1 C:\xampp\htdocs\big02 \home.php(12): Google_Client->setAccessToken(false) #2 C:\xampp\htdocs\big02\home.php(28): getGoogleClient() #3 {main} 在 C:\xampp\htdocs\ big02\google-api-php-client\src\Google\Auth\OAuth2.php 在第 179 行
    • 致命错误:在 C:\xampp\htdocs\big02\google-api-php-client\src\Google\Auth\ OAuth2.php:179 堆栈跟踪:#0 C:\xampp\htdocs\big02\google-api-php-client\src\Google\Client.php(215): Google_Auth_OAuth2->setAccessToken(false) #1 C:\ xampp\htdocs\big02\home.php(11): Google_Client->setAccessToken(false) #2 C:\xampp\htdocs\big02\home.php(29): getGoogleClient() #3 {main} 在 C 中抛出: \xampp\htdocs\big02\google-api-php-client\src\Google\Auth\OAuth2.php 在第 179 行
    • 您需要创建一个可写文件来存储令牌,还需要设置正确的 p12 服务文件(来自您的服务帐户)+ 您需要在 PHP 安装中支持 JSON。
    • 你有这个程序的工作设置文件夹结构吗?所以你可以通过我的邮件 ID 发送给我,或者我们在那里聊天。如果您可以与我分享,请告诉我。这对我来说非常有用。我在文件夹中有 p12 和 JSON 文件。但无法执行。
    • amitpatange88@gmail.com
    【解决方案2】:

    您可以使用以下步骤

    1. 在 Cloud Console 的项目选择器页面上,选择或创建 Cloud 项目
    2. 启用 BigQuery API。
    3. 设置身份验证
    4. 在 Cloud Console 中,转到创建服务帐号密钥页面。
    5. 将环境变量 GOOGLE_APPLICATION_CREDENTIALS 设置为包含您的服务帐户密钥的 JSON 文件的路径。此变量仅适用于您当前的 shell 会话,因此如果您打开一个新会话,请再次设置该变量

    有关完整指南,请参阅此文档here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多