Skip to content

Commit f4b1e30

Browse files
authored
feat(BigQuery): Added sample to Insert rows into table without insertIds (GoogleCloudPlatform#1705)
1 parent 87e6ba7 commit f4b1e30

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
namespace Google\Cloud\Samples\BigQuery;
25+
26+
# [START bigquery_table_insert_rows_explicit_none_insert_ids]
27+
use Google\Cloud\BigQuery\BigQueryClient;
28+
29+
/**
30+
* Insert rows into the given table with explicitly giving row ids.
31+
*
32+
* @param string $projectId The project Id of your Google Cloud Project.
33+
* @param string $datasetId The BigQuery dataset ID.
34+
* @param string $tableId The BigQuery table ID.
35+
* @param string $rowData1 Json encoded data to insert.
36+
* @param string $rowData2 Json encoded data to insert. For eg,
37+
* $rowData1 = json_encode([
38+
* "field1" => "value1",
39+
* "field2" => "value2"
40+
* ]);
41+
* $rowData2 = json_encode([
42+
* "field1" => "value1",
43+
* "field2" => "value2"
44+
* ]);
45+
*/
46+
function table_insert_rows_explicit_none_insert_ids(
47+
string $projectId,
48+
string $datasetId,
49+
string $tableId,
50+
string $rowData1,
51+
string $rowData2
52+
): void {
53+
$bigQuery = new BigQueryClient([
54+
'projectId' => $projectId,
55+
]);
56+
$dataset = $bigQuery->dataset($datasetId);
57+
$table = $dataset->table($tableId);
58+
59+
$rowData1 = json_decode($rowData1, true);
60+
$rowData2 = json_decode($rowData2, true);
61+
// Omitting insert Id's in following rows.
62+
$rows = [
63+
['data' => $rowData1],
64+
['data' => $rowData2]
65+
];
66+
$insertResponse = $table->insertRows($rows);
67+
68+
if ($insertResponse->isSuccessful()) {
69+
printf('Rows successfully inserted into table without insert ids' . PHP_EOL);
70+
} else {
71+
foreach ($insertResponse->failedRows() as $row) {
72+
foreach ($row['errors'] as $error) {
73+
printf('%s: %s' . PHP_EOL, $error['reason'], $error['message']);
74+
}
75+
}
76+
}
77+
}
78+
# [END bigquery_table_insert_rows_explicit_none_insert_ids]
79+
require_once __DIR__ . '/../../../testing/sample_helpers.php';
80+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

bigquery/api/test/bigqueryTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,23 @@ public function testRunQuery()
284284
$this->assertStringContainsString('Found 10 row(s)', $output);
285285
}
286286

287+
public function testTableInsertRowsExplicitNoneInsertIds()
288+
{
289+
$tempTableId = $this->createTempEmptyTable();
290+
291+
$output = $this->runFunctionSnippet('table_insert_rows_explicit_none_insert_ids', [
292+
self::$datasetId,
293+
$tempTableId,
294+
json_encode(['name' => 'Yash Sahu', 'title' => 'Noogler dev']),
295+
json_encode(['name' => 'Friday', 'title' => 'Are the best'])
296+
]);
297+
298+
$tempTable = self::$dataset->table($tempTableId);
299+
$expectedOutput = sprintf('Rows successfully inserted into table without insert ids');
300+
$this->assertStringContainsString($expectedOutput, $output);
301+
$this->verifyTable($tempTable, 'Yash Sahu', 2);
302+
}
303+
287304
public function testRunQueryAsJob()
288305
{
289306
$tableId = $this->createTempTable();

0 commit comments

Comments
 (0)