public static function setUpBeforeClass() { $path = getenv('GOOGLE_APPLICATION_CREDENTIALS'); self::$hasCredentials = $path && file_exists($path) && filesize($path) > 0; if (self::$hasCredentials) { self::$bigquery = createAuthorizedClient(); self::$projectId = getenv('GOOGLE_PROJECT_ID'); self::$shakespeareQuery = 'SELECT TOP(corpus, 10) as title, COUNT(*) as unique_words ' . 'FROM [publicdata:samples.shakespeare]'; } }
if (!$json_credentials_path) { throw new Exception('Set the environment variable ' . 'GOOGLE_APPLICATION_CREDENTIALS to the path to your .json file.'); } $contents = file_get_contents($json_credentials_path); $json_array = json_decode($contents, true); $credentials = new Google_Auth_AssertionCredentials($json_array['client_email'], [Google_Service_Bigquery::BIGQUERY], $json_array['private_key']); $client = new Google_Client(); $client->setAssertionCredentials($credentials); if ($client->getAuth()->isAccessTokenExpired()) { $client->getAuth()->refreshTokenWithAssertion(); } $service = new Google_Service_Bigquery($client); 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();