Remote functions and Translation API tutorial
This tutorial describes how to create a BigQuery remote function , invoke the Cloud Translation API , and perform content translation from any language to Spanish using SQL and Python.
Use cases for this function include the following:
- Translate user comments on a website into a local language
- Translate support requests from many languages into one common language for support case workers
Objectives
- Assign necessary roles to your account.
- Create a Cloud Run functions function.
- Create a BigQuery dataset.
- Create a BigQuery connection and service account.
- Grant permissions to the BigQuery service account.
- Create a BigQuery remote function.
- Call the BigQuery remote function.
Costs
In this document, you use the following billable components of Google Cloud:
To generate a cost estimate based on your projected usage, use the pricing calculator .
Before you begin
We recommend that you create a Google Cloud project for this tutorial. Also, make sure that you have the required roles to complete this tutorial.
Set up a Google Cloud project
To set up a Google Cloud project for this tutorial, complete these steps:
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Verify that billing is enabled for your Google Cloud project .
-
Enable the BigQuery, BigQuery Connection, Cloud Translation, Cloud Run functions, Cloud Build, Cloud Logging, Cloud Pub/Sub, Artifact Registry, and Cloud Run Admin APIs.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Verify that billing is enabled for your Google Cloud project .
-
Enable the BigQuery, BigQuery Connection, Cloud Translation, Cloud Run functions, Cloud Build, Cloud Logging, Cloud Pub/Sub, Artifact Registry, and Cloud Run Admin APIs.
Required roles for your account
To get the permissions that you need to perform the tasks in this tutorial, ask your administrator to grant you the following IAM roles on your project:
- BigQuery Data Owner
(
roles/bigquery.dataOwner
) - BigQuery Connection Admin
(
roles/bigquery.connectionAdmin
) - Cloud Functions Developer
(
roles/cloudfunctions.developer
)
For more information about granting roles, see Manage access to projects, folders, and organizations .
These predefined roles contain the permissions required to perform the tasks in this tutorial. To see the exact permissions that are required, expand the Required permissionssection:
Required permissions
The following permissions are required to perform the tasks in this tutorial:
-
bigquery.datasets.create
-
bigquery.connections.create
-
bigquery.connections.get
-
cloudfunctions.functions.create
You might also be able to get these permissions with custom roles or other predefined roles .
Required roles for the Compute Engine default service account
When you enabled the API for Cloud Run functions, a Compute Engine default service account was created. To complete this tutorial, you must give this default service account the Cloud Translation API User role.
-
Copy your Compute Engine default service account. Your default service account looks like this:
PROJECT_NUMBER -compute@developer.gserviceaccount.com
Replace
PROJECT_NUMBER
with your project ID. -
In the Google Cloud console, go to the IAMpage.
-
Select your project.
-
Click Grant access, and then in the New principalsfield, paste the Compute Engine default service account that you copied earlier.
-
In the Assign roleslist, search for and select Cloud Translation API User.
-
Click Save.
Create a Cloud Run functions function
Using Cloud Run functions, create a function that translates input text into Spanish.
-
Create a Cloud Run functions function with the following specifications:
- For Environment, select 2nd gen.
- For Function name, enter
translation-handler
. - For Region, select us-central1.
-
For Maximum number of instances, enter
10
.This setting is in the Runtime, build, connections and security settingssection.
In this tutorial, we use a lower value than the default to control the request rate sent to Translation.
-
For Runtime, select Python 3.10.
-
For Entry point, enter
handle_translation
.
-
In the file list, select main.py, and then paste the following code.
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 .