Skip to content

Commit 2196bff

Browse files
Bigquery: Added sample to get large dataset with legacy SQL (GoogleCloudPlatform#1592)
* Bigquery: Added sample to query dataset with legacy SQL
1 parent 2db3322 commit 2196bff

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

bigquery/api/src/query_legacy.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
/**
3+
* Copyright 2022 Google LLC.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigquery/api/README.md
22+
*/
23+
24+
// Include Google Cloud dependendencies using Composer
25+
require_once __DIR__ . '/../vendor/autoload.php';
26+
27+
if (count($argv) != 2) {
28+
return printf("Usage: php %s PROJECT_ID\n", __FILE__);
29+
}
30+
list($_, $projectId) = $argv;
31+
32+
// [START bigquery_query_legacy]
33+
use Google\Cloud\BigQuery\BigQueryClient;
34+
35+
$query = 'SELECT corpus FROM [bigquery-public-data:samples.shakespeare] GROUP BY corpus';
36+
37+
$bigQuery = new BigQueryClient([
38+
'projectId' => $projectId,
39+
]);
40+
$jobConfig = $bigQuery->query($query)->useLegacySql(true);
41+
42+
$queryResults = $bigQuery->runQuery($jobConfig);
43+
44+
$i = 0;
45+
foreach ($queryResults as $row) {
46+
printf('--- Row %s ---' . PHP_EOL, ++$i);
47+
foreach ($row as $column => $value) {
48+
printf('%s: %s' . PHP_EOL, $column, json_encode($value));
49+
}
50+
}
51+
printf('Found %s row(s)' . PHP_EOL, $i);
52+
// [END bigquery_query_legacy]

bigquery/api/test/bigqueryTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,14 @@ public function testRunQueryAsJob()
287287
$this->assertStringContainsString('Found 1 row(s)', $output);
288288
}
289289

290+
public function testQueryLegacy()
291+
{
292+
$output = $this->runSnippet('query_legacy');
293+
$this->assertStringContainsString('tempest', $output);
294+
$this->assertStringContainsString('kinghenryviii', $output);
295+
$this->assertStringContainsString('Found 42 row(s)', $output);
296+
}
297+
290298
private function runSnippet($sampleName, $params = [])
291299
{
292300
$argv = array_merge([0, self::$projectId], $params);

0 commit comments

Comments
 (0)