|
16 | 16 |
|
17 | 17 | 'use strict'; |
18 | 18 |
|
19 | | -async function extractTableToGCS(datasetId, tableId, bucketName, filename) { |
20 | | - // Exports my_dataset:my_table to gcs://my-bucket/my-file as raw CSV. |
21 | | - |
| 19 | +function main(datasetId, tableId, bucketName, filename) { |
22 | 20 | // [START bigquery_extract_table] |
23 | 21 | // Import the Google Cloud client libraries |
24 | 22 | const {BigQuery} = require('@google-cloud/bigquery'); |
25 | 23 | const {Storage} = require('@google-cloud/storage'); |
26 | 24 |
|
27 | | - /** |
28 | | - * TODO(developer): Uncomment the following lines before running the sample. |
29 | | - */ |
30 | | - // const datasetId = "my_dataset"; |
31 | | - // const tableId = "my_table"; |
32 | | - // const bucketName = "my-bucket"; |
33 | | - // const filename = "file.csv"; |
34 | | - |
35 | | - // Instantiate clients |
36 | | - const bigquery = new BigQuery(); |
37 | | - const storage = new Storage(); |
38 | | - |
39 | | - // Location must match that of the source table. |
40 | | - const options = { |
41 | | - location: 'US', |
42 | | - }; |
43 | | - |
44 | | - // Export data from the table into a Google Cloud Storage file |
45 | | - const [job] = await bigquery |
46 | | - .dataset(datasetId) |
47 | | - .table(tableId) |
48 | | - .extract(storage.bucket(bucketName).file(filename), options); |
49 | | - // load() waits for the job to finish |
50 | | - console.log(`Job ${job.id} completed.`); |
51 | | - |
52 | | - // Check the job's status for errors |
53 | | - const errors = job.status.errors; |
54 | | - if (errors && errors.length > 0) { |
55 | | - throw errors; |
| 25 | + async function extractTableToGCS() { |
| 26 | + // Exports my_dataset:my_table to gcs://my-bucket/my-file as raw CSV. |
| 27 | + |
| 28 | + /** |
| 29 | + * TODO(developer): Uncomment the following lines before running the sample. |
| 30 | + */ |
| 31 | + // const datasetId = "my_dataset"; |
| 32 | + // const tableId = "my_table"; |
| 33 | + // const bucketName = "my-bucket"; |
| 34 | + // const filename = "file.csv"; |
| 35 | + |
| 36 | + // Instantiate clients |
| 37 | + const bigqueryClient = new BigQuery(); |
| 38 | + const storageClient = new Storage(); |
| 39 | + |
| 40 | + // Location must match that of the source table. |
| 41 | + const options = { |
| 42 | + location: 'US', |
| 43 | + }; |
| 44 | + |
| 45 | + // Export data from the table into a Google Cloud Storage file |
| 46 | + const [job] = await bigqueryClient |
| 47 | + .dataset(datasetId) |
| 48 | + .table(tableId) |
| 49 | + .extract(storageClient.bucket(bucketName).file(filename), options); |
| 50 | + // load() waits for the job to finish |
| 51 | + console.log(`Job ${job.id} completed.`); |
| 52 | + |
| 53 | + // Check the job's status for errors |
| 54 | + const errors = job.status.errors; |
| 55 | + if (errors && errors.length > 0) { |
| 56 | + throw errors; |
| 57 | + } |
56 | 58 | } |
| 59 | + extractTableToGCS(); |
57 | 60 | // [END bigquery_extract_table] |
58 | 61 | } |
59 | | - |
60 | | -extractTableToGCS(...process.argv.slice(2)).catch(console.error); |
| 62 | +main(...process.argv.slice(2)); |
0 commit comments