Export data to Pub/Sub (reverse ETL)

Exporting data to Pub/Sub requires using BigQuerycontinuous queries.

This document describes how you can set up reverse extract-transform-load(RETL) fromBigQuery toPub/Sub.You can do this by using theEXPORT DATA statementin acontinuous query toexport data from BigQuery to aPub/Sub topic.

You can use a RETL workflow to Pub/Sub to combineBigQuery's analytics capabilities with Pub/Sub'sasynchronous and scalable global messaging service. This workflow lets youserve data to downstream applications and services in an event-driven manner.

Prerequisites

You mustcreate a service account. Aservice accountis required to run a continuous query that exports results to aPub/Sub topic.

You must create aPub/Sub topicto receive the continuous query results as messages, and aPub/Sub subscriptionthat the target application can use to receive those messages.

Required roles

This section provides information about the roles and permissions required bythe user account that creates the continuous query, and the service account thatruns the continuous query.

User account permissions

To create a job in BigQuery, the user account must have thebigquery.jobs.create IAM permission. Each of the followingIAM roles grants thebigquery.jobs.create permission:

To submit a job that runs using a service account, the user account must have theService Account User (roles/iam.serviceAccountUser)role. If you are using the same user account to create the service account,then the user account must have theService Account Admin (roles/iam.serviceAccountAdmin)role. For information on how to limit a user's access to single service account,rather than to all service accounts within a project, seeGrant a single role.

If the user account must enable the APIs required for yourcontinuous query use case, the user account must have theService Usage Admin (roles/serviceusage.serviceUsageAdmin)role.

Service account permissions

To export data from a BigQuery table, the service account musthave thebigquery.tables.export IAM permission. Each of thefollowing IAM roles grants thebigquery.tables.exportpermission:

For the service account to access Pub/Sub, you must grant theservice account both of the following IAM roles:

You might also be able to get the required permissions throughcustom roles.

Before you begin

Enable the BigQuery and Pub/Sub APIs.

Roles required to enable APIs

To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enable permission.Learn how to grant roles.

Enable the APIs

Export to Pub/Sub

Use theEXPORT DATA statementto export data to a Pub/Sub topic:

Console

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

    Go to BigQuery

  2. In the query editor, clickMore>Query settings.

  3. In theContinuous Query section, select theUse continuous query mode checkbox.

  4. In theService account box, select the service account that youcreated.

  5. ClickSave.

  6. In the query editor, enter the following statement:

    EXPORTDATAOPTIONS(format='CLOUD_PUBSUB',uri='https://pubsub.googleapis.com/projects/PROJECT_ID/topics/TOPIC_ID')AS(QUERY);

    Replace the following:

    • PROJECT_ID: your project ID.
    • TOPIC_ID: the Pub/Sub topic ID.You can get the topic ID from theTopics pageof the Google Cloud console.
    • QUERY: the SQL statement to select the datato export. The SQL statement must only containsupported operations.You must use theAPPENDS functionin theFROM clause of a continuous query to specify the point intime at which to start processing data.
  7. ClickRun.

bq

  1. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

  2. On the command line, run the continuous query by using thebq query commandwith the following flags:

    • Set the--continuous flag totrue to make the query continuous.
    • Use the--connection_property flag to specify a service account to use.
    bqquery--project_id=PROJECT_ID--use_legacy_sql=false\--continuous=true--connection_property=service_account=SERVICE_ACCOUNT_EMAIL\'EXPORT DATA OPTIONS (format = "CLOUD_PUBSUB", uri = "https://pubsub.googleapis.com/projects/PROJECT_ID/topics/TOPIC_ID") AS (QUERY);'

    Replace the following:

    • PROJECT_ID: your project ID.
    • SERVICE_ACCOUNT_EMAIL: the serviceaccount email. You can get the service account email on theService accounts pageof the Google Cloud console.
    • QUERY: the SQL statement to select the datato export. The SQL statement must only containsupported operations.You must use theAPPENDS functionin theFROM clause of a continuous query to specify the point intime at which to start processing data.

API

  1. Run the continuous query by calling thejobs.insert method.Set the following fields in theJobConfigurationQuery resourceof theJob resource that you pass in:

    • Set thecontinuous field totrue to make the query continuous.
    • Use theconnection_property field to specify a service account to use.
    curl--requestPOST\'https://bigquery.googleapis.com/bigquery/v2/projects/PROJECT_ID/jobs'--header'Authorization: Bearer $(gcloud auth print-access-token) \  --header 'Accept:application/json' \  --header 'Content-Type:application/json' \  --data '("configuration":("query":"EXPORT DATA OPTIONS (format = 'CLOUD_PUBSUB', uri = 'https://pubsub.googleapis.com/projects/PROJECT_ID/topics/TOPIC_ID') AS (QUERY);","useLegacySql":false,"continuous":true,"connectionProperties":["key":"service_account","value":"SERVICE_ACCOUNT_EMAIL"]))'\--compressed

    Replace the following:

    • PROJECT_ID: your project ID.
    • QUERY: the SQL statement to select the datato export. The SQL statement must only containsupported operations.You must use theAPPENDS functionin theFROM clause of a continuous query to specify the point intime at which to start processing data.
    • SERVICE_ACCOUNT_EMAIL: the serviceaccount email. You can get the service account email on theService accounts pageof the Google Cloud console.

Export multiple columns to Pub/Sub

If you want to include multiple columns in your output, you can create a structcolumn to contain the column values, and then convert the struct valueto a JSON string by using theTO_JSON_STRING function.The following example exports data from four columns, formatted as a JSONstring:

EXPORTDATAOPTIONS(format='CLOUD_PUBSUB',uri='https://pubsub.googleapis.com/projects/myproject/topics/taxi-real-time-rides')AS(SELECTTO_JSON_STRING(STRUCT(ride_id,timestamp,latitude,longitude))ASmessageFROMAPPENDS(TABLE`myproject.real_time_taxi_streaming.taxi_rides`,-- Configure the APPENDS TVF start_timestamp to specify when you want to-- start processing data using your continuous query.-- This example starts processing at 10 minutes before the current time.CURRENT_TIMESTAMP()-INTERVAL10MINUTE)WHEREride_status='enroute');

Export optimization

If your continuous query job performance appears to be limited byavailable compute resources,try increasing the size of your BigQueryCONTINUOUS slot reservation assignment.

Limitations

  • The exported data must consist of a singleSTRING orBYTES column.The column name can be whatever you choose.
  • You must use acontinuous query toexport to Pub/Sub.
  • You can't pass a schema to a Pub/Sub topic in thecontinuous query.
  • You can't export data to a Pub/Sub topic that uses aschema.
  • When exporting to Pub/Sub, you can export JSON formatted recordswhere some values areNULL, but you can't export records that consist ofonlyNULL values. You can excludeNULL records from the query results byincluding aWHERE message IS NOT NULL filter in the continuous query.
  • When exporting data to a Pub/Sub topic configured with alocational endpoint,the endpoint must be configured within the same Google Cloud regionalboundary as the BigQuery dataset that contains the table youare querying.
  • Exported data must not exceedPub/Sub quotas.

Pricing

When you export data in a continuous query, you are billed usingBigQuery capacity compute pricing.To run continuous queries, you must have areservation that uses theEnterprise or Enterprise Plus edition,and areservation assignmentthat uses theCONTINUOUS job type.

After the data is exported, you're charged for using Pub/Sub.For more information, seePub/Sub pricing.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-15 UTC.