Skip to content

Commit 7dfeeda

Browse files
authored
feat(Bigquery): Append column samples (GoogleCloudPlatform#1709)
1 parent 4447ca7 commit 7dfeeda

File tree

4 files changed

+168
-0
lines changed

4 files changed

+168
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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) != 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]
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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) != 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_query_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+
46+
// In this example, the existing table contains only the 'Name' and 'Title'.
47+
// A new column 'Description' gets added after the query job.
48+
49+
// Define query
50+
$query = sprintf('SELECT "John" as name, "Unknown" as title, "Dummy person" as description;');
51+
52+
// Set job configs
53+
$queryJobConfig = $bigQuery->query($query);
54+
$queryJobConfig->destinationTable($table);
55+
$queryJobConfig->schemaUpdateOptions(['ALLOW_FIELD_ADDITION']);
56+
$queryJobConfig->writeDisposition('WRITE_APPEND');
57+
58+
// Run query with query job configuration
59+
$bigQuery->runQuery($queryJobConfig);
60+
61+
// Print all the columns
62+
$columns = $table->info()['schema']['fields'];
63+
printf('The columns in the table are ');
64+
foreach ($columns as $column) {
65+
printf('%s ', $column['name']);
66+
}
67+
# [END bigquery_add_column_query_append]

bigquery/api/test/bigqueryTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,31 @@ public function testQueryLegacy()
324324
$this->assertStringContainsString('Found 42 row(s)', $output);
325325
}
326326

327+
public function testAddColumnLoadAppend()
328+
{
329+
$tableId = $this->createTempTable();
330+
$output = $this->runSnippet('add_column_load_append', [
331+
self::$datasetId,
332+
$tableId
333+
]);
334+
335+
$this->assertStringContainsString('name', $output);
336+
$this->assertStringContainsString('title', $output);
337+
$this->assertStringContainsString('description', $output);
338+
}
339+
340+
public function testAddColumnQueryAppend()
341+
{
342+
$tableId = $this->createTempTable();
343+
$output = $this->runSnippet('add_column_query_append', [
344+
self::$datasetId,
345+
$tableId
346+
]);
347+
$this->assertStringContainsString('name', $output);
348+
$this->assertStringContainsString('title', $output);
349+
$this->assertStringContainsString('description', $output);
350+
}
351+
327352
private function runSnippet($sampleName, $params = [])
328353
{
329354
$argv = array_merge([0, self::$projectId], $params);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"Yash Sahu","Learner","Sundays are sleep days"

0 commit comments

Comments
 (0)