|
| 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] |
0 commit comments