Flash Thunder
Flash Thunder

Reputation: 12036

BigQuery example for current Google API in PHP

All the question about examples are like 2 years old, and I can't find ANY working example for current API client version (https://github.com/google/google-api-php-client). Every single one is missing something or throwing exception....

Could anybody provide working example? There is no documentation AT ALL, anywhere.

This is the most working one:

<?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));
?>

But I am getting Fatal errors about Google_Dataset and Google_Table not defined...

Upvotes: 2

Views: 2987

Answers (1)

Pentium10
Pentium10

Reputation: 207830

Here is a code that

  • properly creates a Google_Client
  • runs a job async
  • displays the running job ID and status

You need to have:

  • service account created (something like [email protected])
  • your key file (.p12)
  • service_token_file_location (writable path to store the JSON from the handshake, it will be valid for 1h)

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;

You can grab the results with:

$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);

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

like:

  • Google_Service_Bigquery_TableRow

Upvotes: 7

Related Questions