Stay organized with collections Save and categorize content based on your preferences.
Generate video embeddings by using the ML.GENERATE_EMBEDDING functionThis document shows you how to create a BigQuery ML remote model that references a Vertex AI embedding foundation model. You then use that model with the ML.GENERATE_EMBEDDING
function to create video embeddings by using data from a BigQuery object table.
To create a remote model and generate embeddings, you need the following Identity and Access Management (IAM) roles:
roles/bigquery.dataEditor
) on your project.Create, delegate, and use BigQuery connections: BigQuery Connections Admin (roles/bigquery.connectionsAdmin
) on your project.
If you don't have a default connection configured, you can create and set one as part of running the CREATE MODEL
statement. To do so, you must have BigQuery Admin (roles/bigquery.admin
) on your project. For more information, see Configure the default connection.
Grant permissions to the connection's service account: Project IAM Admin (roles/resourcemanager.projectIamAdmin
) on the project that contains the Vertex AI endpoint. This is the current project for remote models that you create by specifying the model name as an endpoint. This is the project identified in the URL for remote models that you create by specifying a URL as an endpoint.
If you use the remote model to analyze unstructured data from an object table, and the Cloud Storage bucket that you use in the object table is in a different project than your Vertex AI endpoint, you must also have Storage Admin (roles/storage.admin
) on the Cloud Storage bucket used by the object table.
Create BigQuery jobs: BigQuery Job User (roles/bigquery.jobUser
) on your project.
These predefined roles contain the permissions required to perform the tasks in this document. To see the exact permissions that are required, expand the Required permissions section:
Required permissionsbigquery.datasets.create
bigquery.connections.*
resourcemanager.projects.getIamPolicy
and resourcemanager.projects.setIamPolicy
bigquery.tables.create
and bigquery.tables.update
bigquery.jobs.create
bigquery.models.create
bigquery.models.getData
bigquery.models.updateData
bigquery.models.updateMetadata
You might also be able to get these permissions with custom roles or other predefined roles.
Before you beginIn the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Note: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.Verify that billing is enabled for your Google Cloud project.
Enable the BigQuery, BigQuery Connection, Cloud Storage, and Vertex AI APIs.
Create a BigQuery dataset to contain your resources:
ConsoleIn the Google Cloud console, go to the BigQuery page.
In the Explorer pane, click your project name.
Click more_vert View actions > Create dataset.
On the Create dataset page, do the following:
For Dataset ID, type a name for the dataset.
For Location type, select a location for the dataset.
Click Create dataset.
To create a new dataset, use the bq mk
command with the --location
flag:
bq --location=LOCATION mk -d DATASET_ID
Replace the following:
LOCATION
: the dataset's location.DATASET_ID
is the ID of the dataset that you're creating.Confirm that the dataset was created:
bq ls
You can skip this step if you either have a default connection configured, or you have the BigQuery Admin role.
Create a Cloud resource connection for the remote model to use, and get the connection's service account. Create the connection in the same location as the dataset that you created in the previous step.
Select one of the following options:
ConsoleGo to the BigQuery page.
In the Explorer pane, click add Add data:
The Add data dialog opens.
In the Filter By pane, in the Data Source Type section, select Business Applications.
Alternatively, in the Search for data sources field, you can enter Vertex AI
.
In the Featured data sources section, click Vertex AI.
Click the Vertex AI Models: BigQuery Federation solution card.
In the Connection type list, select Vertex AI remote models, remote functions and BigLake (Cloud Resource).
In the Connection ID field, enter a name for your connection.
Click Create connection.
Click Go to connection.
In the Connection info pane, copy the service account ID for use in a later step.
In a command-line environment, create a connection:
bq mk --connection --location=REGION --project_id=PROJECT_ID \ --connection_type=CLOUD_RESOURCE CONNECTION_ID
The --project_id
parameter overrides the default project.
Replace the following:
REGION
: your connection regionPROJECT_ID
: your Google Cloud project IDCONNECTION_ID
: an ID for your connectionWhen you create a connection resource, BigQuery creates a unique system service account and associates it with the connection.
Troubleshooting: If you get the following connection error, update the Google Cloud SDK:
Flags parsing error: flag --connection_type=CLOUD_RESOURCE: value should be one of...
Retrieve and copy the service account ID for use in a later step:
bq show --connection PROJECT_ID.REGION.CONNECTION_ID
The output is similar to the following:
name properties 1234.REGION.CONNECTION_ID {"serviceAccountId": "connection-1234-9u56h9@gcp-sa-bigquery-condel.iam.gserviceaccount.com"}
Use the google_bigquery_connection
resource.
To authenticate to BigQuery, set up Application Default Credentials. For more information, see Set up authentication for client libraries.
The following example creates a Cloud resource connection named my_cloud_resource_connection
in the US
region:
To apply your Terraform configuration in a Google Cloud project, complete the steps in the following sections.
Prepare Cloud ShellSet the default Google Cloud project where you want to apply your Terraform configurations.
You only need to run this command once per project, and you can run it in any directory.
export GOOGLE_CLOUD_PROJECT=PROJECT_ID
Environment variables are overridden if you set explicit values in the Terraform configuration file.
Each Terraform configuration file must have its own directory (also called a root module).
.tf
extension—for example main.tf
. In this tutorial, the file is referred to as main.tf
.
mkdir DIRECTORY && cd DIRECTORY && touch main.tf
If you are following a tutorial, you can copy the sample code in each section or step.
Copy the sample code into the newly created main.tf
.
Optionally, copy the code from GitHub. This is recommended when the Terraform snippet is part of an end-to-end solution.
terraform init
Optionally, to use the latest Google provider version, include the -upgrade
option:
terraform init -upgrade
terraform plan
Make corrections to the configuration as necessary.
yes
at the prompt:
terraform apply
Wait until Terraform displays the "Apply complete!" message.
Grant the connection's service account the Vertex AI User and Storage Object Viewer roles.
If you plan to specify the endpoint as a URL when you create the remote model, for example endpoint = 'https://us-central1-aiplatform.googleapis.com/v1/projects/myproject/locations/us-central1/publishers/google/models/gemini-2.0-flash'
, grant this role in the same project you specify in the URL.
If you plan to specify the endpoint by using the model name when you create the remote model, for example endpoint = 'gemini-2.0-flash'
, grant this role in the same project where you plan to create the remote model.
Granting the role in a different project results in the error bqcx-1234567890-wxyz@gcp-sa-bigquery-condel.iam.gserviceaccount.com does not have the permission to access resource
.
To grant these roles, follow these steps:
ConsoleGo to the IAM & Admin page.
Click person_add Add.
The Add principals dialog opens.
In the New principals field, enter the service account ID that you copied earlier.
In the Select a role field, select Vertex AI, and then select Vertex AI User.
Click Add another role.
In the Select a role field, choose Cloud Storage, and then select Storage Object Viewer.
Click Save.
Use the gcloud projects add-iam-policy-binding
command.
gcloud projects add-iam-policy-binding 'PROJECT_NUMBER' --member='serviceAccount:MEMBER' --role='roles/aiplatform.user' --condition=None gcloud projects add-iam-policy-binding 'PROJECT_NUMBER' --member='serviceAccount:MEMBER' --role='roles/storage.objectViewer' --condition=None
Replace the following:
PROJECT_NUMBER
: the project number of the project in which to grant the role.MEMBER
: the service account ID that you copied earlier.To analyze videos without moving them from Cloud Storage, create an object table.
To create an object table:
SQLUse the CREATE EXTERNAL TABLE
statement.
In the Google Cloud console, go to the BigQuery page.
In the query editor, enter the following statement:
CREATE EXTERNAL TABLE `PROJECT_ID.DATASET_ID.TABLE_NAME` WITH CONNECTION {`PROJECT_ID.REGION.CONNECTION_ID`| DEFAULT} OPTIONS( object_metadata = 'SIMPLE', uris = ['BUCKET_PATH'[,...]], max_staleness = STALENESS_INTERVAL, metadata_cache_mode = 'CACHE_MODE');
Replace the following:
PROJECT_ID
: your project ID.DATASET_ID
: the ID of the dataset that you created.TABLE_NAME
: the name of the object table.REGION
: the region or multi-region that contains the connection.CONNECTION_ID
: the ID of the connection that you created.
When you view the connection details in the Google Cloud console, this is the value in the last section of the fully qualified connection ID that is shown in Connection ID, for example projects/myproject/locations/connection_location/connections/myconnection
.
To use a default connection, specify DEFAULT
instead of the connection string containing PROJECT_ID.REGION.CONNECTION_ID
.
BUCKET_PATH
: the path to the Cloud Storage bucket that contains the videos, in the format ['gs://bucket_name/[folder_name/]*']
.
The Cloud Storage bucket that you use should be in the same project where you plan to create the model and call the ML.GENERATE_EMBEDDING
function. If you want to call the ML.GENERATE_EMBEDDING
function in a different project than the one that contains the Cloud Storage bucket used by the object table, you must grant the Storage Admin role at the bucket level to the service-A@gcp-sa-aiplatform.iam.gserviceaccount.com
service account.
STALENESS_INTERVAL
: specifies whether cached metadata is used by operations against the object table, and how fresh the cached metadata must be in order for the operation to use it. For more information on metadata caching considerations, see Metadata caching for performance.
To disable metadata caching, specify 0. This is the default.
To enable metadata caching, specify an interval literal value between 30 minutes and 7 days. For example, specify INTERVAL 4 HOUR
for a 4 hour staleness interval. With this value, operations against the table use cached metadata if it has been refreshed within the past 4 hours. If the cached metadata is older than that, the operation retrieves metadata from Cloud Storage instead.
CACHE_MODE
: specifies whether the metadata cache is refreshed automatically or manually. For more information on metadata caching considerations, see Metadata caching for performance.
Set to AUTOMATIC
for the metadata cache to be refreshed at a system-defined interval, usually somewhere between 30 and 60 minutes.
Set to MANUAL
if you want to refresh the metadata cache on a schedule you determine. In this case, you can call the BQ.REFRESH_EXTERNAL_METADATA_CACHE
system procedure to refresh the cache.
You must set CACHE_MODE
if STALENESS_INTERVAL
is set to a value greater than 0.
Click play_circle Run.
For more information about how to run queries, see Run an interactive query.
bqUse the bq mk
command.
bq mk --table \ --external_table_definition=BUCKET_PATH@REGION.CONNECTION_ID \ --object_metadata=SIMPLE \ --max_staleness=STALENESS_INTERVAL \ --metadata_cache_mode=CACHE_MODE \ PROJECT_ID:DATASET_ID.TABLE_NAME
Replace the following:
BUCKET_PATH
: the path to the Cloud Storage bucket that contains the videos, in the format ['gs://bucket_name/[folder_name/]*']
.
The Cloud Storage bucket that you use should be in the same project where you plan to create the model and call the ML.GENERATE_EMBEDDING
function. If you want to call the ML.GENERATE_EMBEDDING
function in a different project than the one that contains the Cloud Storage bucket used by the object table, you must grant the Storage Admin role at the bucket level to the service-A@gcp-sa-aiplatform.iam.gserviceaccount.com
service account.
REGION
: the region or multi-region that contains the connection.CONNECTION_ID
: the ID of the connection that you created.
When you view the connection details in the Google Cloud console, this is the value in the last section of the fully qualified connection ID that is shown in Connection ID, for example projects/myproject/locations/connection_location/connections/myconnection
.
STALENESS_INTERVAL
: specifies whether cached metadata is used by operations against the object table, and how fresh the cached metadata must be in order for the operation to use it. For more information on metadata caching considerations, see Metadata caching for performance.
To disable metadata caching, specify 0. This is the default.
To enable metadata caching, specify an interval literal value between 30 minutes and 7 days. For example, specify INTERVAL 4 HOUR
for a 4 hour staleness interval. With this value, operations against the table use cached metadata if it has been refreshed within the past 4 hours. If the cached metadata is older than that, the operation retrieves metadata from Cloud Storage instead.
CACHE_MODE
: specifies whether the metadata cache is refreshed automatically or manually. For more information on metadata caching considerations, see Metadata caching for performance.
Set to AUTOMATIC
for the metadata cache to be refreshed at a system-defined interval, usually somewhere between 30 and 60 minutes.
Set to MANUAL
if you want to refresh the metadata cache on a schedule you determine. In this case, you can call the BQ.REFRESH_EXTERNAL_METADATA_CACHE
system procedure to refresh the cache.
You must set CACHE_MODE
if STALENESS_INTERVAL
is set to a value greater than 0.
PROJECT_ID
: your project ID.DATASET_ID
: the ID of the dataset that you created.TABLE_NAME
: the name of the object table.In the Google Cloud console, go to the BigQuery page.
Using the SQL editor, create a remote model:
CREATE OR REPLACE MODEL `PROJECT_ID.DATASET_ID.MODEL_NAME` REMOTE WITH CONNECTION {DEFAULT | `PROJECT_ID.REGION.CONNECTION_ID`} OPTIONS (ENDPOINT = 'ENDPOINT');
Replace the following:
PROJECT_ID
: your project ID.DATASET_ID
: the ID of the dataset that you created.MODEL_NAME
: the name of the model.REGION
: the region or multi-region that contains the connection.CONNECTION_ID
: the ID of the connection that you created.
When you view the connection details in the Google Cloud console, this is the value in the last section of the fully qualified connection ID that is shown in Connection ID, for example projects/myproject/locations/connection_location/connections/myconnection
.
ENDPOINT
: the embedding model to use, in this case multimodalembedding@001
.
If you specify a URL as the endpoint when you create the remote model, for example endpoint = 'https://us-central1-aiplatform.googleapis.com/v1/projects/myproject/locations/us-central1/publishers/google/models/multimodalembedding@001'
, make sure that the project that you specify in the URL is the project in which you have granted the Vertex AI user role to the connection's. service account.
The multimodalembedding@001
model must be available in the location where you are creating the remote model. For more information, see Locations.
Generate video embeddings with the ML.GENERATE_EMBEDDING
function by using video data from an object table:
SELECT * FROM ML.GENERATE_EMBEDDING( MODEL `PROJECT_ID.DATASET_ID.MODEL_NAME`, TABLE PROJECT_ID.DATASET_ID.TABLE_NAME, STRUCT(FLATTEN_JSON AS flatten_json_output, START_SECOND AS start_second, END_SECOND AS end_second, INTERVAL_SECONDS AS interval_seconds) );
Replace the following:
PROJECT_ID
: your project ID.DATASET_ID
: the ID of the dataset that contains the model.MODEL_NAME
: the name of the remote model over a multimodalembedding@001
model.TABLE_NAME
: the name of the object table that contains the videos to embed.FLATTEN_JSON
: a BOOL
value that indicates whether to parse the embedding into a separate column. The default value is TRUE
.START_SECOND
: a FLOAT64
value that specifies the second in the video at which to start the embedding. The default value is 0
. This value must be positive and less than the end_second
value.END_SECOND
: a FLOAT64
value that specifies the second in the video at which to end the embedding. The default value is 120
. This value must be positive and greater than the start_second
value.INTERVAL_SECONDS
: a FLOAT64
value that specifies the interval to use when creating embeddings. For example, if you set start_second = 0
, end_second = 120
, and interval_seconds = 10
, then the video is split into twelve 10 second segments ([0, 10), [10, 20), [20, 30)...
) and embeddings are generated for each segment. This value must be greater than 4
and less than 120
. The default value is 16
.The following example shows how to create embeddings for the videos in the videos
object table. Embeddings are created for each 5 second interval between the 10 second and 40 second marks in each video.
SELECT * FROM ML.GENERATE_EMBEDDING( MODEL `mydataset.embedding_model`, TABLE `mydataset.videos`, STRUCT(TRUE AS flatten_json_output, 10 AS start_second, 40 AS end_second, 5 AS interval_seconds) );What's next
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-08-07 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-07 UTC."],[[["This guide explains how to generate video embeddings using the `ML.GENERATE_EMBEDDING` function in BigQuery ML, leveraging data from a BigQuery object table."],["A remote model is created, referencing a Vertex AI embedding foundation model, which is then used with `ML.GENERATE_EMBEDDING` to create the video embeddings."],["Proper IAM permissions are necessary for creating connections, models, and running inferences, as well as for the connection's service accounts to access the model and the object table."],["An object table must be set up to store the video data, and the associated Cloud Storage bucket should generally be in the same project or have specific Storage Admin permissions granted to a specific service account."],["Video embeddings are generated by specifying a start and end time in seconds, and an interval in seconds, allowing the videos to be segmented and analyzed accordingly."]]],[]]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4