Update model metadata

This page shows you how to update BigQuery ML model metadata. You can update model metadata by:

  • Using the Google Cloud console.
  • Using the bq update command in the bq command-line tool.
  • Calling the models.patch API method directly or by using the client libraries.

The following model metadata can be updated:

  • Description: Can be updated by using the Google Cloud console, bq command-line tool, API, or client libraries.
  • Labels: Can be updated by using the Google Cloud console, bq command-line tool, API, or client libraries.
  • Expiration time: Can be updated by using the bq tool, API, or client libraries.

Required permissions

To update model metadata, you must be assigned the WRITER role on the dataset, or you must be assigned a project-level Identity and Access Management (IAM) role that includes bigquery.models.updateMetadata permissions. If you are granted bigquery.models.updateMetadata permissions at the project level, you can update metadata for models in any dataset in the project. The following predefined, project-level IAM roles include bigquery.models.updateMetadata permissions:

  • bigquery.dataEditor
  • bigquery.dataOwner
  • bigquery.admin

For more information on IAM roles and permissions in BigQuery ML, see Access control.

Update a model's description

A model's description is a text string that is used to identify the model.

To update a model's description:

Console

  1. In the Google Cloud console, go to the BigQuery page.

    Go to the BigQuery page

  2. In the Explorer pane, expand your project and then expand a dataset.

  3. Expand the Models folder in the dataset, and then click a model name to select the model.

  4. Click the Details tab.

  5. To update the model's description, click Edit .

  6. In the Edit detail dialog, update the description and then click Save.

bq

To update a model's description, issue the bq update command with the --model or -m flag and the --description flag.

If you are updating a model in a project other than your default project, add the project ID to the dataset in the following format: [PROJECT_ID]:[DATASET].

bq update --model --description "[STRING]" PROJECT_ID:DATASET.MODEL

Replace the following:

  • STRING is the text string that describes your model in quotes.
  • PROJECT_ID is your project ID.
  • DATASET is the name of the dataset.
  • MODEL is the name of the model.

The command output looks like the following:

Model 'myproject.mydataset.mymodel' successfully updated.

You can confirm your changes by issuing the bq show command. For more information, see Get model metadata.

Examples:

Enter the following command to update the description of mymodel in mydataset in your default project.

bq update --model --description "My updated description" \
mydataset.mymodel

Enter the following command to update the description of mymodel in mydataset in myotherproject.

bq update --model --description "My updated description" \
myotherproject:mydataset.mymodel

API

To update a model's description by using the API, call the models.patch method and provide the projectId, datasetId, and modelId. To modify the description, add to or update the "description" property for the model resource.

Go

Before trying this sample, follow the Go setup instructions in the BigQuery quickstart using client libraries. For more information, see the BigQuery Go API reference documentation.

To authenticate to BigQuery, set up Application Default Credentials. For more information, see Set up authentication for client libraries.

import (
	"context"
	"fmt"

	"cloud.google.com/go/bigquery"
)

// updateModelDescription demonstrates fetching BigQuery ML model metadata and updating the
// Description metadata.
func updateModelDescription(projectID, datasetID, modelID string) error {
	// projectID := "my-project-id"
	// datasetID := "mydataset"
	// modelID := "mymodel"
	ctx := context.Background()
	client, err := bigquery.NewClient(ctx, projectID)
	if err != nil {
		return fmt.Errorf("bigquery.NewClient: %w", err)
	}
	defer client.Close()

	model := client.Dataset(datasetID).Model(modelID)
	oldMeta, err := model.Metadata(ctx)
	if err != nil {
		return fmt.Errorf("couldn't retrieve model metadata: %w", err)
	}
	update := bigquery.ModelMetadataToUpdate{
		Description: "This model was modified from a Go program",
	}
	if _, err = model.Update(ctx, update, oldMeta.ETag); err != nil {
		return fmt.Errorf("couldn't update model: %w", err)
	}
	return nil
}

Java

Before trying this sample, follow the Java setup instructions in the BigQuery quickstart using client libraries. For more information, see the BigQuery Java API reference documentation.

To authenticate to BigQuery, set up Application Default Credentials. For more information, see Set up authentication for client libraries.

import com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.BigQueryException;
import com.google.cloud.bigquery.BigQueryOptions;
import com.google.cloud.bigquery.Model;
import com.google.cloud.bigquery.ModelId;

// Sample to update description on a model
public class UpdateModelDescription {

  public static void runUpdateModelDescription() {
    // TODO(developer): Replace these variables before running the sample.
    String datasetName = "MY_DATASET_NAME";
    String modelName = "MY_MODEL_NAME";
    String newDescription = "A really great model.";
    updateModelDescription(datasetName, modelName, newDescription);
  }

  public static void updateModelDescription(
      String datasetName, String modelName, String newDescription) {
    try {
      // Initialize client that will be used to send requests. This client only needs to be created
      // once, and can be reused for multiple requests.
      BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();

      Model model = bigquery.getModel(ModelId.of(datasetName, modelName));
      bigquery.update(model.toBuilder().setDescription(newDescription).build());
      System.out.println("Model description updated successfully to " + newDescription);
    } catch (BigQueryException e) {
      System.out.println("Model description was not updated \n" + e.toString());
    }
  }
}

Node.js

Before trying this sample, follow the Node.js setup instructions in the BigQuery quickstart using client libraries. For more information, see the BigQuery Node.js API reference documentation.

To authenticate to BigQuery, set up Application Default Credentials. For more information, see Set up authentication for client libraries.

// Import the Google Cloud client library
const {BigQuery} = require('@google-cloud/bigquery');
const bigquery = new BigQuery();

async function updateModel() {
  // Updates a model's metadata.

  /**
   * TODO(developer): Uncomment the following lines before running the sample
   */
  // const datasetId = "my_dataset";
  // const modelId = "my__model";

  const metadata = {
    description: 'A really great model.',
  };

  const dataset = bigquery.dataset(datasetId);
  const [apiResponse] = await dataset.model(modelId).setMetadata(metadata);
  const newDescription = apiResponse.description;

  console.log(`${modelId} description: ${newDescription}`);
}

Python

Before trying this sample, follow the Python setup instructions in the BigQuery quickstart using client libraries. For more information, see the BigQuery Python API reference documentation.

To authenticate to BigQuery, set up Application Default Credentials. For more information, see Set up authentication for client libraries.


from google.cloud import