|
| 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) != 4) { |
| 28 | + return printf("Usage: php %s PROJECT_ID DATASET_ID TABLE_ID\n", __FILE__); |
| 29 | +} |
| 30 | +list($_, $projectId, $datasetId, $tableId) = $argv; |
| 31 | + |
| 32 | +# [START bigquery_add_column_load_append] |
| 33 | +use Google\Cloud\BigQuery\BigQueryClient; |
| 34 | + |
| 35 | +/** Uncomment and populate these variables in your code */ |
| 36 | +// $projectId = 'The Google project ID'; |
| 37 | +// $datasetId = 'The BigQuery dataset ID'; |
| 38 | +// $tableId = 'Table ID of the table in dataset'; |
| 39 | + |
| 40 | +$bigQuery = new BigQueryClient([ |
| 41 | + 'projectId' => $projectId, |
| 42 | +]); |
| 43 | +$dataset = $bigQuery->dataset($datasetId); |
| 44 | +$table = $dataset->table($tableId); |
| 45 | +// In this example, the existing table contains only the 'Name' and 'Title'. |
| 46 | +// A new column 'Description' gets added after load job. |
| 47 | + |
| 48 | +$schema = [ |
| 49 | + 'fields' => [ |
| 50 | + ['name' => 'name', 'type' => 'string', 'mode' => 'nullable'], |
| 51 | + ['name' => 'title', 'type' => 'string', 'mode' => 'nullable'], |
| 52 | + ['name' => 'description', 'type' => 'string', 'mode' => 'nullable'] |
| 53 | + ] |
| 54 | +]; |
| 55 | + |
| 56 | +$source = __DIR__ . '/../test/data/test_data_extra_column.csv'; |
| 57 | + |
| 58 | +// Set job configs |
| 59 | +$loadConfig = $table->load(fopen($source, 'r')); |
| 60 | +$loadConfig->destinationTable($table); |
| 61 | +$loadConfig->schema($schema); |
| 62 | +$loadConfig->schemaUpdateOptions(['ALLOW_FIELD_ADDITION']); |
| 63 | +$loadConfig->sourceFormat('CSV'); |
| 64 | +$loadConfig->writeDisposition('WRITE_APPEND'); |
| 65 | + |
| 66 | +// Run the job with load config |
| 67 | +$job = $bigQuery->runJob($loadConfig); |
| 68 | + |
| 69 | +// Print all the columns |
| 70 | +$columns = $table->info()['schema']['fields']; |
| 71 | +printf('The columns in the table are '); |
| 72 | +foreach ($columns as $column) { |
| 73 | + printf('%s ', $column['name']); |
| 74 | +} |
| 75 | +# [END bigquery_add_column_load_append] |
0 commit comments