Skip to content

Commit

Permalink
samples: restored deleted sample and added test for it (#430)
Browse files Browse the repository at this point in the history
  • Loading branch information
munkhuushmgl authored and Ace Nassri committed Nov 15, 2022
1 parent 3bc3536 commit 5b60efb
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 4 deletions.
85 changes: 85 additions & 0 deletions automl/tables/predict-bq-source-bq-dest.v1beta1.js
Original file line number Diff line number Diff line change
@@ -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://<project_id>.<dataset_id>.<table_id>",
// `The Big Query URI containing the inputs`;
// const outputUri = '[BIGQUERY_PATH]' e.g., "bq://<project_id>",
// `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;
});
8 changes: 4 additions & 4 deletions automl/test/automlTablesPredict.v1beta1.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'});

Expand Down Expand Up @@ -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:/);
Expand Down

0 comments on commit 5b60efb

Please sign in to comment.