Bigquery + PHP示例 [英] Bigquery + PHP examples

查看:99
本文介绍了Bigquery + PHP示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以提供使用PHP的BigQuery API的工作示例。我看到有一些python和java的例子,但是找不到任何的PHP。

google.com/?pli=1rel =nofollow> https://bigquery.cloud.google.com/?pli=1



例如,您可以在浏览器中运行此SQL

  SELECT语料库,count(*)FROM publicdata:samples.shakespeare 
组由语料库限制5个;

我想通过PHP模拟类似的调用。

即使是一个如何使用PHP API的粗略例子也会有所帮助。

/ div>

使用Google API for PHP。以下是执行单个同步查询作业的脚本的简单示例。这使用可下载API客户端中的类名。注意:从SVN提取的源代码具有不同的类名称。请注意,您必须为客户机秘密,客户机ID,重定向URI和项目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();
//访问https://developers.google.com/console以生成您的
// oauth2_client_id,oauth2_client_secret,并注册您的oauth2_redirect_uri。

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

//您的项目ID
$ project_id ='XXXXXXXXXXXXXXXXXXXX';

//实例化一个新的BigQuery客户端
$ 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示例< / title>
< / head>
< body>
< div id ='container'>
< div id ='top'>< h1> BigQuery API示例< / 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);

//用BigQuery API做一些事情$ $ response data
print_r($ response);

?>
< / div>
< / div>
< / body>
< / html>


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.

解决方案

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>

这篇关于Bigquery + PHP示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆