|
| 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); |
0 commit comments