|
| 1 | +/** |
| 2 | + * Copyright 2019 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +'use strict'; |
| 18 | + |
| 19 | +function main( |
| 20 | + datasetId = 'my_dataset', |
| 21 | + tableId = 'my_table', |
| 22 | + fileName = '/path/to/file.csv' |
| 23 | +) { |
| 24 | + // [START bigquery_add_column_load_append] |
| 25 | + // Import the Google Cloud client libraries |
| 26 | + const {BigQuery} = require('@google-cloud/bigquery'); |
| 27 | + |
| 28 | + // Instantiate client |
| 29 | + const bigquery = new BigQuery(); |
| 30 | + |
| 31 | + async function addColumnLoadAppend() { |
| 32 | + // Adds a new column to a BigQuery table while appending rows via a load job. |
| 33 | + |
| 34 | + /** |
| 35 | + * TODO(developer): Uncomment the following lines before running the sample. |
| 36 | + */ |
| 37 | + // const fileName = '/path/to/file.csv'; |
| 38 | + // const datasetId = 'my_dataset'; |
| 39 | + // const tableId = 'my_table'; |
| 40 | + |
| 41 | + // In this example, the existing table contains only the 'Name', 'Age', |
| 42 | + // & 'Weight' columns. 'REQUIRED' fields cannot be added to an existing |
| 43 | + // schema, so the additional column must be 'NULLABLE'. |
| 44 | + const schema = 'Name:STRING, Age:INTEGER, Weight:FLOAT, IsMagic:BOOLEAN'; |
| 45 | + |
| 46 | + // Retrieve destination table reference |
| 47 | + const [table] = await bigquery |
| 48 | + .dataset(datasetId) |
| 49 | + .table(tableId) |
| 50 | + .get(); |
| 51 | + const destinationTableRef = table.metadata.tableReference; |
| 52 | + |
| 53 | + // Set load job options |
| 54 | + const options = { |
| 55 | + schema: schema, |
| 56 | + schemaUpdateOptions: ['ALLOW_FIELD_ADDITION'], |
| 57 | + writeDisposition: 'WRITE_APPEND', |
| 58 | + destinationTable: destinationTableRef, |
| 59 | + }; |
| 60 | + |
| 61 | + // Load data from a local file into the table |
| 62 | + const [job] = await bigquery |
| 63 | + .dataset(datasetId) |
| 64 | + .table(tableId) |
| 65 | + .load(fileName, options); |
| 66 | + |
| 67 | + console.log(`Job ${job.id} completed.`); |
| 68 | + console.log(`New Schema:`); |
| 69 | + console.log(job.configuration.load.schema.fields); |
| 70 | + |
| 71 | + // Check the job's status for errors |
| 72 | + const errors = job.status.errors; |
| 73 | + if (errors && errors.length > 0) { |
| 74 | + throw errors; |
| 75 | + } |
| 76 | + } |
| 77 | + // [END bigquery_add_column_load_append] |
| 78 | + addColumnLoadAppend(); |
| 79 | +} |
| 80 | +main(...process.argv.slice(2)); |
0 commit comments