From db9deb60f821f562998c036334e2aa9004457f02 Mon Sep 17 00:00:00 2001 From: Mike <45373284+munkhuushmgl@users.noreply.github.com> Date: Mon, 20 Jul 2020 12:37:41 -0700 Subject: [PATCH] samples: restored deleted sample and added test for it (#430) --- .../predict-bq-source-bq-dest.v1beta1.js | 85 +++++++++++++++++++ .../test/automlTablesPredict.v1beta1.test.js | 8 +- 2 files changed, 89 insertions(+), 4 deletions(-) create mode 100644 automl/tables/predict-bq-source-bq-dest.v1beta1.js diff --git a/automl/tables/predict-bq-source-bq-dest.v1beta1.js b/automl/tables/predict-bq-source-bq-dest.v1beta1.js new file mode 100644 index 00000000000..09c217b9d79 --- /dev/null +++ b/automl/tables/predict-bq-source-bq-dest.v1beta1.js @@ -0,0 +1,85 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +async function main( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + modelId = 'MODEL_ID', + inputUri = 'BIGQUERY_PATH', + outputUri = 'BIGQUERY_DIRECTORY' +) { + // [START automl_tables_predict_using_bq_source_and_bq_dest] + + /** + * Demonstrates using the AutoML client to request prediction from + * automl tables using bigQuery. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const modelId = '[MODEL_ID]' e.g., "TBL4704590352927948800"; + // const inputUri = '[BIGQUERY_PATH]' + // e.g., "bq://..", + // `The Big Query URI containing the inputs`; + // const outputUri = '[BIGQUERY_PATH]' e.g., "bq://", + // `The destination Big Query URI for storing outputs`; + + const automl = require('@google-cloud/automl'); + + // Create client for prediction service. + const automlClient = new automl.v1beta1.PredictionServiceClient(); + + // Get the full path of the model. + const modelFullId = automlClient.modelPath(projectId, computeRegion, modelId); + + async function batchPredict() { + // Construct request + // Get the Big Query input URI. + const inputConfig = { + bigquerySource: { + inputUri: inputUri, + }, + }; + + // Get the Big Query output URI. + const outputConfig = { + bigqueryDestination: { + outputUri: outputUri, + }, + }; + + const [, operation] = await automlClient.batchPredict({ + name: modelFullId, + inputConfig: inputConfig, + outputConfig: outputConfig, + }); + + // Get the latest state of long-running operation. + console.log(`Operation name: ${operation.name}`); + } + + batchPredict(); + // [END automl_tables_predict_using_bq_source_and_bq_dest] +} + +main(...process.argv.slice(2)).catch(err => { + console.error(err.message); + process.exitCode = 1; +}); +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); diff --git a/automl/test/automlTablesPredict.v1beta1.test.js b/automl/test/automlTablesPredict.v1beta1.test.js index 2522787c8bf..675b73c9fd2 100644 --- a/automl/test/automlTablesPredict.v1beta1.test.js +++ b/automl/test/automlTablesPredict.v1beta1.test.js @@ -26,8 +26,8 @@ const region = 'us-central1'; const modelId = process.env.TABLE_MODEL_ID; const gcsInputUri = `gs://${projectId}-tables/predictTest.csv`; const gcsOutputUriPrefix = `gs://${projectId}-tables/test_outputs/`; -const bqInputUri = 'bq://automl-tables-bg-input'; -const bqOutputUriPrefix = 'bq://automl-tables-bg-output'; +const bqInputUri = `bq://${projectId}.automl_test.bank_marketing`; +const bqOutputUriPrefix = `bq://${projectId}`; const exec = cmd => execSync(cmd, {encoding: 'utf8'}); @@ -90,11 +90,11 @@ describe('Tables PredictionAPI', () => { assert.include(output, 'Operation name:'); }); - it.skip(`should perform batch prediction using BQ as source and + it(`should perform batch prediction using BQ as source and BQ as destination`, async () => { // Run batch prediction using BQ as source and BQ as destination const output = exec( - `node tables/predict-gcs-source-bq-dest.v1beta1.js predict-using-bq-source-and-bq-dest "${modelId}"` + + `node tables/predict-bq-source-bq-dest.v1beta1.js "${projectId}" "${region}" "${modelId}"` + ` "${bqInputUri}" "${bqOutputUriPrefix}"` ); assert.match(output, /Operation name:/);