public function testGetRows() { $request = new Google_Service_Bigquery_QueryRequest(); $request->setQuery(self::$shakespeareQuery); $request->setMaxResults(3); $query = self::$bigquery->jobs->query(self::$projectId, $request); $this->assertTrue($query->getJobComplete()); $rows = getRows(self::$bigquery, self::$projectId, $query->getJobReference()->getJobId(), 3); $this->assertTrue($this->isShakespeare($rows)); }
function syncQuery(Google_Service_Bigquery $bigquery, $projectId, $queryString, $timeout = 10000) { $request = new Google_Service_Bigquery_QueryRequest(); $request->setQuery($queryString); $request->setTimeoutMs($timeout); $response = $bigquery->jobs->query($projectId, $request); if (!$response->getJobComplete()) { return; } return $response->getRows() ? $response->getRows() : array(); }
function requestBigQuery($sql) { $query = new Google_Service_Bigquery_QueryRequest(); $query->setQuery($sql); $query->setTimeoutMs(0); $response = $this->service->jobs->query($this->project_id, $query); $job_id = $response->getJobReference()->getJobId(); $pageToken = null; do { $queryResults = $this->service->jobs->getQueryResults($this->project_id, $job_id); $queryResults->setPageToken($pageToken); } while (!$queryResults->getJobComplete()); return $queryResults; }
return $service; } // [END build_service] $bigquery = createAuthorizedClient(); $projectId = ''; if ($projectId) { // The programmer already set the projectId above. } elseif ($argc > 1) { $projectId = $argv[1]; } else { echo 'Enter the project ID: '; $projectId = trim(fgets(STDIN)); } // [START run_query] // Pack a BigQuery request. $request = new Google_Service_Bigquery_QueryRequest(); $request->setQuery('SELECT TOP(corpus, 10) as title, COUNT(*) as unique_words ' . 'FROM [publicdata:samples.shakespeare]'); $response = $bigquery->jobs->query($projectId, $request); $rows = $response->getRows(); // [END run_query] // [START print_results] // Print the results to stdout in a human-readable way. echo "\nQuery Results:\n------------\n"; foreach ($rows as $row) { foreach ($row['f'] as $field) { printf('%-30s', $field['v']); } echo "\n"; } // [END print_results] // [END all]
<?php require_once 'google-api-php-client-1-master/src/Google/autoload.php'; // or wherever autoload.php is located session_start(); $clientID = "208611099847-oamsfojtgfnlco3dddkjiadqgljdadt7.apps.googleusercontent.com"; $clientSecret = "QRS6vOu3CfPgxDhimrWrXv1q"; $client = new Google_Client(); $client->setClientId($clientID); $client->setClientSecret($clientSecret); $client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/BigQuery/redirect.php'); $client->addScope(Google_Service_Bigquery::BIGQUERY); if (isset($_SESSION['access_token']) && $_SESSION['access_token']) { $client->setAccessToken($_SESSION['access_token']); $bigquery = new Google_Service_Bigquery($client); $projectId = 'big-query-dashboard'; $request = new Google_Service_Bigquery_QueryRequest(); $request->setQuery('SELECT * FROM [publicdata:samples.gsod] LIMIT 1000'); $response = $bigquery->jobs->query($projectId, $request); $rows = $response->getRows(); echo "\nQuery Results:\n------------\n"; foreach ($rows as $row) { foreach ($row['f'] as $field) { printf('%-30s', $field['v']); } echo "\n"; } } else { $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/BigQuery/redirect .php'; header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL)); }
/** * @param $project_id * @param $sql * @param array $params * @return array|\Exception */ public function bigQuerySql($project_id, $sql, $params = array()) { $query = new \Google_Service_Bigquery_QueryRequest(); $query->setQuery($sql); $result = $this->bigqueryService->jobs->query($project_id, $query, $params); $fieldsOject = $result->getSchema()->getFields(); $rows = $result->getRows(); $totalCount = $result->count(); $fields = array(); foreach ($fieldsOject as $field) { $fields[] = $field->name; } $count = 0; $newResults = array(); foreach ($rows as $row) { $newRow = array(); foreach ($row['f'] as $field) { array_push($newRow, $field['v']); } //making the result an associative array $newResults['row_' . $count] = array_combine($fields, $newRow); $count++; } return array('count' => $totalCount, 'results' => $newResults); }