Skip to content

Commit 54d31c1

Browse files
feat: add new samples for bigquery (GoogleCloudPlatform#1657)
1 parent 86e3d51 commit 54d31c1

File tree

4 files changed

+141
-2
lines changed

4 files changed

+141
-2
lines changed

bigquery/api/phpunit.xml.dist

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
</logging>
2626
<filter>
2727
<whitelist>
28-
<directory suffix=".php">./snippets</directory>
29-
<exclude>
28+
<directory suffix=".php">./src</directory>
29+
<exclude>
3030
<directory>./vendor</directory>
3131
</exclude>
3232
</whitelist>

bigquery/api/src/dry_run_query.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
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) != 3) {
28+
return printf("Usage: php %s PROJECT_ID SQL_QUERY\n", __FILE__);
29+
}
30+
list($_, $projectId, $query) = $argv;
31+
32+
# [START bigquery_query_dry_run]
33+
use Google\Cloud\BigQuery\BigQueryClient;
34+
35+
/** Uncomment and populate these variables in your code */
36+
// $projectId = 'The Google project ID';
37+
// $query = 'SELECT id, view_count FROM `bigquery-public-data.stackoverflow.posts_questions`';
38+
39+
// Construct a BigQuery client object.
40+
$bigQuery = new BigQueryClient([
41+
'projectId' => $projectId,
42+
]);
43+
44+
// Set job configs
45+
$jobConfig = $bigQuery->query($query);
46+
$jobConfig->useQueryCache(false);
47+
$jobConfig->dryRun(true);
48+
49+
// Extract query results
50+
$queryJob = $bigQuery->startJob($jobConfig);
51+
$info = $queryJob->info();
52+
53+
printf('This query will process %s bytes' . PHP_EOL, $info['statistics']['totalBytesProcessed']);
54+
# [END bigquery_query_dry_run]
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
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) != 3) {
28+
return printf("Usage: php %s PROJECT_ID SQL_QUERY\n", __FILE__);
29+
}
30+
list($_, $projectId, $query) = $argv;
31+
32+
# [START bigquery_query_no_cache]
33+
use Google\Cloud\BigQuery\BigQueryClient;
34+
35+
/** Uncomment and populate these variables in your code */
36+
// $projectId = 'The Google project ID';
37+
// $query = 'SELECT id, view_count FROM `bigquery-public-data.stackoverflow.posts_questions`';
38+
39+
// Construct a BigQuery client object.
40+
$bigQuery = new BigQueryClient([
41+
'projectId' => $projectId,
42+
]);
43+
44+
// Set job configs
45+
$jobConfig = $bigQuery->query($query);
46+
$jobConfig->useQueryCache(false);
47+
48+
// Extract query results
49+
$queryResults = $bigQuery->runQuery($jobConfig);
50+
51+
$i = 0;
52+
foreach ($queryResults as $row) {
53+
printf('--- Row %s ---' . PHP_EOL, ++$i);
54+
foreach ($row as $column => $value) {
55+
printf('%s: %s' . PHP_EOL, $column, json_encode($value));
56+
}
57+
}
58+
printf('Found %s row(s)' . PHP_EOL, $i);
59+
# [END bigquery_query_no_cache]

bigquery/api/test/bigqueryTest.php

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

290+
public function testDryRunQuery()
291+
{
292+
$tableId = $this->createTempTable();
293+
$query = sprintf(
294+
'SELECT * FROM `%s.%s` LIMIT 1',
295+
self::$datasetId,
296+
$tableId
297+
);
298+
299+
$output = $this->runSnippet('dry_run_query', [$query]);
300+
$this->assertStringContainsString('This query will process 126 bytes', $output);
301+
}
302+
303+
public function testQueryNoCache()
304+
{
305+
$tableId = $this->createTempTable();
306+
$query = sprintf(
307+
'SELECT * FROM `%s.%s` LIMIT 1',
308+
self::$datasetId,
309+
$tableId
310+
);
311+
312+
$output = $this->runSnippet('query_no_cache', [$query]);
313+
$this->assertStringContainsString('Found 1 row(s)', $output);
314+
}
315+
290316
public function testQueryLegacy()
291317
{
292318
$output = $this->runSnippet('query_legacy');

0 commit comments

Comments
 (0)