Get batch predictions using Cloud Storage as a source and BigQuery as a destination

Creates a batch prediction job with a Cloud Storage file as input and BigQuery as a destination.

Code sample

Node.js

To authenticate to AutoML Tables, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.


/**
 * Demonstrates using the AutoML client to request prediction from
 * automl tables using GCS
 * 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 = '[GCS_PATH]' e.g., "gs://<bucket-name>/<csv file>",
// `The Google Cloud Storage 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() {
  const inputConfig = {
    gcsSource: {
      inputUris: [inputUri],
    },
  };

  // Get the Big Query output URIs.
  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();

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.