Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bigquery + PHP examples

Can somebody provide working example of using the Bigquery API with PHP. I see there are examples for python and java but could not find anything for PHP.

Here is the bigquery browser https://bigquery.cloud.google.com/?pli=1

For e.g You can run this SQL in the browser

SELECT corpus,count(*) FROM publicdata:samples.shakespeare 
group by corpus limit 5;

I want to simulate similar call via PHP.

Even a rough example of how to use the PHP API will help a lot.

like image 334
Nilesh Avatar asked Sep 13 '12 19:09

Nilesh


People also ask

Is BigQuery OLTP or OLAP?

BigQuery focuses on Online Analytical Processing (OLAP) by using table scans and not point lookups. If you need OLTP-like behavior (single-row updates or inserts), consider a database designed to support OLTP use cases such as Cloud SQL.

Is BigQuery better than MySQL?

Google BigQuery vs MySQL Comparison These scores are from 299 and 1440 reviews, respectively. But what about these solutions' feature sets? With 46 features, Google BigQuery has a significant advantage compared to MySQL, which only provides 28 features. Note that the products in this category average 40 features.

Is BigQuery a ETL tool?

BigQuery and Snowflake are two of the top ETL software solutions for organizations looking to manage their data from various sources and gain the most from their data insights.


1 Answers

Use the Google API Client for PHP. Here's a simple example of a script that does a single synchronous query job. This uses the class names found in the downloadable API client. Note: the source pulled from SVN features different class names. Note where you must add your own values for client secret, client id, redirect URI, and project id.

<?php

require_once 'google-api-php-client/src/apiClient.php';
require_once 'google-api-php-client/src/contrib/apiBigqueryService.php';

session_start();

$client = new apiClient();
// Visit https://developers.google.com/console to generate your
// oauth2_client_id, oauth2_client_secret, and to register your oauth2_redirect_uri.

$client->setClientId('XXXXXXXXXXXXXXX.apps.googleusercontent.com');
$client->setClientSecret('XXXXXXXXXXXXXXXXXXX');
$client->setRedirectUri('http://www_your_domain.com/somescript.php');

// Your project id
$project_id = 'XXXXXXXXXXXXXXXXXXXX';

// Instantiate a new BigQuery Client 
$bigqueryService = new apiBigqueryService($client);

if (isset($_REQUEST['logout'])) {
  unset($_SESSION['access_token']);
}

if (isset($_SESSION['access_token'])) {
  $client->setAccessToken($_SESSION['access_token']);
} else {
  $client->setAccessToken($client->authenticate());
  $_SESSION['access_token'] = $client->getAccessToken();
}

if (isset($_GET['code'])) {
  $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
?>
<!doctype html>
<html>
<head>
  <title>BigQuery API Sample</title>
</head>
<body>
<div id='container'>
  <div id='top'><h1>BigQuery API Sample</h1></div>
  <div id='main'>
<?php
  $query = new QueryRequest();
  $query->setQuery('SELECT TOP( title, 10) as title, COUNT(*) as revision_count FROM [publicdata:samples.wikipedia] WHERE wp_namespace = 0;');

  $jobs = $bigqueryService->jobs;
  $response = $jobs->query($project_id, $query);

  // Do something with the BigQuery API $response data
  print_r($response);

?>
  </div>
</div>
</body>
</html>
like image 73
Michael Manoochehri Avatar answered Sep 18 '22 20:09

Michael Manoochehri