|
| 1 | +// Copyright 2021 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +'use strict'; |
| 16 | + |
| 17 | +// sample-metadata: |
| 18 | +// title: Export Assets To BigQuery |
| 19 | +// description: Export asserts to specified BigQuery table. |
| 20 | +// usage: node exportAssetsBigquery.js <projects/project_id/datasets/dataset_id> <table_name> |
| 21 | + |
| 22 | +async function main(dataSet, table) { |
| 23 | + // [START asset_quickstart_export_assets_bigquery] |
| 24 | + /** |
| 25 | + * TODO(developer): Uncomment these variables before running the sample. |
| 26 | + */ |
| 27 | + // const dataSet = 'projects/project_id/datasets/dataset_id'; |
| 28 | + // const table = 'mytable'; |
| 29 | + |
| 30 | + const {AssetServiceClient} = require('@google-cloud/asset'); |
| 31 | + const client = new AssetServiceClient(); |
| 32 | + |
| 33 | + async function exportAssetsBigquery() { |
| 34 | + const projectId = await client.getProjectId(); |
| 35 | + const projectResource = client.projectPath(projectId); |
| 36 | + const dataset = dataSet; |
| 37 | + |
| 38 | + const request = { |
| 39 | + parent: projectResource, |
| 40 | + outputConfig: { |
| 41 | + bigqueryDestination: { |
| 42 | + dataset: `projects/${projectId}/${dataset}`, |
| 43 | + table: table, |
| 44 | + force: true, |
| 45 | + }, |
| 46 | + }, |
| 47 | + }; |
| 48 | + |
| 49 | + // Handle the operation using the promise pattern. |
| 50 | + const [operation] = await client.exportAssets(request); |
| 51 | + |
| 52 | + // Operation#promise starts polling for the completion of the operation. |
| 53 | + const [result] = await operation.promise(); |
| 54 | + |
| 55 | + // Do things with with the response. |
| 56 | + console.log(result); |
| 57 | + } |
| 58 | + |
| 59 | + exportAssetsBigquery(); |
| 60 | + // [END asset_quickstart_export_assets_bigquery] |
| 61 | +} |
| 62 | + |
| 63 | +main(...process.argv.slice(2)).catch(err => { |
| 64 | + console.error(err.message); |
| 65 | + process.exitCode = 1; |
| 66 | +}); |
0 commit comments