Google Cloud BigQuery Sink Connector for Confluent Cloud

Note

This is a Quick Start for the managed cloud connector. If you are installing the connector locally for Confluent Platform, see Google BigQuery Sink connector for Confluent Platform.

You can use the Kafka Connect Google BigQuery Sink connector for Confluent Cloud to export Avro, JSON Schema, Protobuf, or JSON (schemaless) data from Apache Kafka® topics to BigQuery. The BigQuery table schema is based upon information in the Apache Kafka® schema for the topic.

Features

  • The connector supports insert operations and attempts to detect duplicates. See BigQuery troubleshooting for additional information.

  • The connector uses the BigQuery insertAll streaming api, which inserts records one at a time. The records are immediately available in the table for querying.

  • The connector supports streaming from a list of topics into corresponding tables in BigQuery.

    Note

    Make sure to review BigQuery rate limits if you are planning to use multiple connectors with a high number of tasks.

  • Even though the connector streams records one at a time by default (as opposed to running in batch mode), the connector is scalable because it contains an internal thread pool that allows it to stream records in parallel. The internal thread pool defaults to 10 threads.

  • The connector supports several time-based table partitioning strategies using the property partitioning.type.

  • The connector supports Avro, JSON Schema, Protobuf, or JSON (schemaless) input data formats. Schema Registry must be enabled to use a Schema Registry-based format (for example, Avro, JSON_SR (JSON Schema), or Protobuf). See Schema Registry Enabled Environments for additional information.

  • For Avro, the connector provides the following configuration properties that support automated table creation and updates. You can select these properties in the UI or add them to the connector configuration, if using the Confluent CLI.

    • autoCreateTables: Automatically create BigQuery tables if they don’t already exist.
    • sanitizeTopics: Automatically sanitize topic names before using them as BigQuery table names. If not enabled, topic names are used as table names.
    • autoUpdateSchemas: Automatically update BigQuery tables.
    • sanitizeFieldNames Automatically sanitize field names before using them as column names in BigQuery. Note that Kafka field names become column names in BigQuery.

For more information and examples to use with the Confluent Cloud API for Connect, see the Confluent Cloud API for Connect section.

Limitations

Be sure to review the following information.

Quick Start

Use this quick start to get up and running with the Confluent Cloud Google BigQuery Sink connector. The quick start provides the basics of selecting the connector and configuring it to stream events to a BigQuery data warehouse.

Prerequisites
  • An active GCP account with authorization to create resources.

  • A BigQuery project is required. The project can be created using the Google Cloud Console.

  • A BigQuery dataset is required in the project.

  • A service account that can access the BigQuery project containing the dataset. You can create this service account in the Google Cloud Console.

  • The service account must have access to the BigQuery project containing the dataset. You create and download a key when creating a service account. The key must be downloaded as a JSON file. It resembles the example below:

    {
     "type": "service_account",
     "project_id": "confluent-842583",
     "private_key_id": "...omitted...",
     "private_key": "-----BEGIN PRIVATE ...omitted... =\n-----END PRIVATE KEY-----\n",
     "client_email": "confluent2@confluent-842583.iam.gserviceaccount.com",
     "client_id": "...omitted...",
     "auth_uri": "https://accounts.google.com/oauth2/auth",
     "token_uri": "https://oauth2.googleapis.com/token",
     "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/certs",
     "client_x509_cert_url": "https://www.googleapis.com/robot/metadata/confluent2%40confluent-842583.iam.gserviceaccount.com"
    }
    

    According to GCP specifications, the service account will either have to have the BigQueryEditor primitive IAM role or the bigquery.dataEditor predefined IAM role. The minimum permissions are:

    bigquery.datasets.get
    bigquery.tables.create
    bigquery.tables.get
    bigquery.tables.getData
    bigquery.tables.list
    bigquery.tables.update
    bigquery.tables.updateData
    
  • Kafka cluster credentials. The following lists the different ways you can provide credentials.

    • Enter an existing service account resource ID.
    • Create a Confluent Cloud service account for the connector. Make sure to review the ACL entries required in the service account documentation. Some connectors have specific ACL requirements.
    • Create a Confluent Cloud API key and secret. To create a key and secret, you can use confluent api-key create or you can autogenerate the API key and secret directly in the Cloud Console when setting up the connector.
  • The Confluent CLI installed and configured for the cluster. See Install the Confluent CLI.

  • Schema Registry must be enabled to use a Schema Registry-based format (for example, Avro, JSON_SR (JSON Schema), or Protobuf). See Schema Registry Enabled Environments for additional information.

  • You must create a BigQuery table before using the connector, if you leave Auto create tables (or autoCreateTables) set to false (the default).

  • You may need to create a schema in BigQuery, depending on how you set the Auto update schemas property (or autoUpdateSchemas).

    • Auto update schemas set to true: You do not have to create a schema.

    • Auto update schemas set to false (the default): You must create a schema in BigQuery (as shown below). The connector does not automatically update the table.

      Auto update schemas set to false

Using the Confluent Cloud Console

Step 1: Launch your Confluent Cloud cluster.

See the Quick Start for Confluent Cloud for installation instructions.

Step 2: Add a connector.

In the left navigation menu, click Data integration, and then click Connectors. If you already have connectors in your cluster, click + Add connector.

Step 3: Select your connector.

Click the Google BigQuery Sink connector card.

Google BigQuery Sink Connector Card

Step 4: Enter the connector details.

Note

  • Ensure you have all your prerequisites completed.
  • An asterisk ( * ) designates a required entry.

At the Add Google BigQuery Sink Connector screen, complete the following:

If you’ve already populated your Kafka topics, select the topic(s) you want to connect from the Topics list.

To create a new topic, click +Add new topic.

Step 5: Check the results in BigQuery.

  1. From the Google Cloud Console, go to your BigQuery project.
  2. Query your datasets and verify that new records are being added.

For more information and examples to use with the Confluent Cloud API for Connect, see the Confluent Cloud API for Connect section.

Tip

When you launch a connector, a Dead Letter Queue topic is automatically created. See Dead Letter Queue for details.

Note

Make sure to review BigQuery rate limits if you are planning to use multiple connectors with a high number of tasks.

See also

For an example that shows fully-managed Confluent Cloud connectors in action with Confluent Cloud ksqlDB, see the Cloud ETL Demo. This example also shows how to use Confluent CLI to manage your resources in Confluent Cloud.

../_images/topology.png

Using the Confluent CLI

Complete the following steps to set up and run the connector using the Confluent CLI.

Note

  • Make sure you have all your prerequisites completed.
  • The example commands use Confluent CLI version 2. For more information see, Confluent CLI v2.

Step 1: List the available connectors.

Enter the following command to list available connectors:

confluent connect plugin list

Step 2: Show the required connector configuration properties.

Enter the following command to show the required connector properties:

confluent connect plugin describe <connector-catalog-name>

For example:

confluent connect plugin describe BigQuerySink

Example output:

Following are the required configs:
connector.class
name
kafka.auth.mode
kafka.api.key
kafka.api.secret
keyfile
project
datasets
input.data.format
tasks.max
topics

Step 3: Create the connector configuration file.

Create a JSON file that contains the connector configuration properties. The following example shows the required connector properties.

{
    "name" : "confluent-bigquery-sink",
    "connector.class" : "BigQuerySink",
    "kafka.auth.mode": "KAFKA_API_KEY",
    "kafka.api.key" : "<my-kafka-api-key>",
    "kafka.api.secret" : "<my-kafka-api-secret>",
    "keyfile" : "omitted",
    "project" : "<my-BigQuery-project>",
    "datasets" : "<my-BigQuery-dataset>",
    "input.data.format" : "AVRO",
    "autoCreateTables" : "true"
    "sanitizeTopics" : "true"
    "autoUpdateSchemas" : "true"
    "sanitizeFieldNames" : "true"
    "tasks.max" : "1"
    "topics" : "pageviews",
}

Note the following property definitions:

  • "name": Sets a name for your new connector.
  • "connector.class": Identifies the connector plugin name.
  • "kafka.auth.mode": Identifies the connector authentication mode you want to use. There are two options: SERVICE_ACCOUNT or KAFKA_API_KEY (the default). To use an API key and secret, specify the configuration properties kafka.api.key and kafka.api.secret, as shown in the example configuration (above). To use a service account, specify the Resource ID in the property kafka.service.account.id=<service-account-resource-ID>. To list the available service account resource IDs, use the following command:

    confluent iam service-account list
    

    For example:

    confluent iam service-account list
    
       Id     | Resource ID |       Name        |    Description
    +---------+-------------+-------------------+-------------------
       123456 | sa-l1r23m   | sa-1              | Service account 1
       789101 | sa-l4d56p   | sa-2              | Service account 2
    
  • "topics": Identifies the topic name or a comma-separated list of topic names.

  • "keyfile": This contains the contents of the downloaded JSON file. See Formatting keyfile credentials for details about how to format and use the contents of the downloaded credentials file.

  • "input.data.format": Sets the input Kafka record value format (data coming from the Kafka topic). Valid entries are AVRO, JSON_SR, PROTOBUF, or JSON. You must have Confluent Cloud Schema Registry configured if using a schema-based message format (for example, Avro, JSON_SR (JSON Schema), or Protobuf).

The following are additional properties you can use. These properties default to false if not used.

  • "autoCreateTables": Designates whether to automatically create BigQuery tables if they don’t already exist.

  • "sanitizeTopics": Designates whether to automatically sanitize topic names before using them as table names. If not enabled, topic names are used as table names. Source topic names must comply with BigQuery naming conventions even if sanitizeTopics is set to true.

  • "autoUpdateSchemas": Designates whether or not to automatically update BigQuery tables. Used with Schema Registry.

  • "sanitizeFieldNames": Designates whether to automatically sanitize field names before using them as column names in BigQuery. BigQuery specifies that field names can only contain letters, numbers, and underscores. The sanitizer replaces invalid symbols with underscores. If the field name starts with a digit, the sanitizer adds an underscore in front of the field name.

    Caution

    Fields a.b and a_b will have the same value after sanitizing, which could cause a key duplication error. If not used, field names are used as column names.

  • "partitioning.type": Select a partitioning type to use:

    • "INGESTION_TIME": To use this type, existing tables must be partitioned by ingestion time. The connector writes to the partition for the current wall clock time. When "autoCreateTables" is true, the connector creates tables partitioned by ingestion time.
    • "NONE": The connector relies only on how the existing tables are set up. When "autoCreateTables" is true, the connector creates non-partitioned tables.
    • "RECORD_TIME": To use this type, existing tables must be partitioned by record time. The connector writes to the partition that corresponds to the Kafka record’s timestamp. When "autoCreateTables" is true, the connector creates tables partitioned by record time.
    • "TIMESTAMP_COLUMN": The connector relies only on how existing tables are set up. When "autoCreateTables" is true, the connector creates tables partitioned using a field in a Kafka record value.
  • "time.partitioning.type": When using INGESTION_TIME, RECORD_TIME, or TIMESTAMP_COLUMN, enter a time span for time partitioning. If you enter NONE, the connector honors the existing BigQuery table partitioning. When "autoCreateTables" is true, the connector creates a table without a specific partitioning strategy.

  • "timestamp.partition.field.name": To use this property, "partitioning.type" must be TIMESTAMP_COLUMN and "autoCreateTables" must be set to true. Enter the field name for the value that contains the timestamp to partition in BigQuery. This enables timestamp partitioning for each table.

Single Message Transforms: See the Single Message Transforms (SMT) documentation for details about adding SMTs using the CLI. See Unsupported transformations for a list of SMTs that are not supported with this connector.

See Configuration Properties for all property values and definitions.

Formatting keyfile credentials

The contents of the downloaded credentials file must be converted to string format before it can be used in the connector configuration.

  1. Convert the JSON file contents into string format. You can use an online converter tool to do this. For example: JSON to String Online Converter.

  2. Add the escape character \ before all \n entries in the Private Key section so that each section begins with \\n (see the highlighted lines below). The example below has been formatted so that the \\n entries are easier to see. Most of the credentials key has been omitted.

    Tip

    A script is available that converts the credentials to a string and also adds the additional escape characters where needed. See Stringify GCP Credentials.

      {
          "name" : "confluent-bigquery-sink",
          "connector.class" : "GcsSink",
          "kafka.api.key" : "<my-kafka-api-keyk>",
          "kafka.api.secret" : "<my-kafka-api-secret>",
          "topics" : "pageviews",
          "keyfile" : "{\"type\":\"service_account\",\"project_id\":\"connect-
          1234567\",\"private_key_id\":\"omitted\",
          \"private_key\":\"-----BEGIN PRIVATE KEY-----
          \\nMIIEvAIBADANBgkqhkiG9w0BA
          \\n6MhBA9TIXB4dPiYYNOYwbfy0Lki8zGn7T6wovGS5\opzsIh
          \\nOAQ8oRolFp\rdwc2cC5wyZ2+E+bhwn
          \\nPdCTW+oZoodY\\nOGB18cCKn5mJRzpiYsb5eGv2fN\/J
          \\n...rest of key omitted...
          \\n-----END PRIVATE KEY-----\\n\",
          \"client_email\":\"pub-sub@connect-123456789.iam.gserviceaccount.com\",
          \"client_id\":\"123456789\",\"auth_uri\":\"https:\/\/accounts.google.com\/o\/oauth2\/
          auth\",\"token_uri\":\"https:\/\/oauth2.googleapis.com\/
          token\",\"auth_provider_x509_cert_url\":\"https:\/\/
          www.googleapis.com\/oauth2\/v1\/
          certs\",\"client_x509_cert_url\":\"https:\/\/www.googleapis.com\/
          robot\/v1\/metadata\/x509\/pub-sub%40connect-
          123456789.iam.gserviceaccount.com\"}",
          "project": "<my-BigQuery-project>",
          "datasets":"<my-BigQuery-dataset>",
          "data.format":"AVRO",
          "tasks.max" : "1"
      }
    
  3. Add all the converted string content to the "keyfile" credentials section of your configuration file as shown in the example above.

Step 4: Load the configuration file and create the connector.

Enter the following command to load the configuration and start the connector:

confluent connect create --config <file-name>.json

For example:

confluent connect create --config bigquery-sink-config.json

Example output:

Created connector confluent-bigquery-sink lcc-ix4dl

Step 5: Check the connector status.

Enter the following command to check the connector status:

confluent connect list

Example output:

ID          |       Name              | Status  | Type
+-----------+-------------------------+---------+------+
lcc-ix4dl   | confluent-bigquery-sink | RUNNING | sink

Step 6: Check the results in BigQuery.

  1. From the Google Cloud Console, go to your BigQuery project.
  2. Query your datasets and verify that new records are being added.

For more information and examples to use with the Confluent Cloud API for Connect, see the Confluent Cloud API for Connect section.

Tip

When you launch a connector, a Dead Letter Queue topic is automatically created. See Dead Letter Queue for details.

Note

Make sure to review BigQuery rate limits if you are planning to use multiple connectors with a high number of tasks.

Configuration Properties

Use the following configuration properties with this connector.

Note

These are properties for the managed cloud connector. If you are installing the connector locally for Confluent Platform, see Google BigQuery Sink connector for Confluent Platform.

Which topics do you want to get data from?

topics

Identifies the topic name or a comma-separated list of topic names.

  • Type: list
  • Importance: high

Input messages

input.data.format

Sets the input Kafka record value format. Valid entries are AVRO, JSON_SR, PROTOBUF, and JSON. Note that you need to have Confluent Cloud Schema Registry configured if using a schema-based message format like AVRO, JSON_SR, or PROTOBUF.

  • Type: string
  • Importance: high

How should we connect to your data?

name

Sets a name for your connector.

  • Type: string
  • Valid Values: A string at most 64 characters long
  • Importance: high

Kafka Cluster credentials

kafka.auth.mode

Kafka Authentication mode. It can be one of KAFKA_API_KEY or SERVICE_ACCOUNT. It defaults to KAFKA_API_KEY mode.

  • Type: string
  • Default: KAFKA_API_KEY
  • Valid Values: KAFKA_API_KEY, SERVICE_ACCOUNT
  • Importance: high
kafka.api.key
  • Type: password
  • Importance: high
kafka.service.account.id

The Service Account that will be used to generate the API keys to communicate with Kafka Cluster.

  • Type: string
  • Importance: high
kafka.api.secret
  • Type: password
  • Importance: high

GCP credentials

keyfile

GCP service account JSON file with write permissions for BigQuery.

  • Type: password
  • Importance: high

BigQuery details

project

ID for the GCP project where BigQuery is located.

  • Type: string
  • Importance: high
datasets

Name for the dataset Kafka topics write to.

  • Type: string
  • Importance: high

SQL/DDL Support

auto.create.tables

Designates whether or not to automatically create BigQuery tables. Note: Supports AVRO, JSON_SR, and PROTOBUF message format only.

  • Type: boolean
  • Default: false
  • Importance: high
auto.update.schemas

Designates whether or not to automatically update BigQuery schemas. New fields in record schemas must be nullable. Note: Supports AVRO, JSON_SR, and PROTOBUF message format only.

  • Type: boolean
  • Default: false
  • Importance: high
sanitize.topics

Designates whether to automatically sanitize topic names before using them as table names in BigQuery. If not enabled, topic names are used as table names.

  • Type: boolean
  • Default: true
  • Importance: high
sanitize.field.names

Whether to automatically sanitize field names before using them as field names in BigQuery. BigQuery specifies that field names can only contain letters, numbers, and underscores. The sanitizer replaces invalid symbols with underscores. If the field name starts with a digit, the sanitizer adds an underscore in front of field name. Caution: Fields a.b and a_b will have same value after sanitizing, which could cause a key duplication error.

  • Type: boolean
  • Default: false
  • Importance: high
partitioning.type

The partitioning type to use.

NONE: Connector relies only on how the existing tables are set up; with auto table creation on, connector will create non-partitioned tables.

INGESTION_TIME: Existing tables should be partitioned by ingestion time, and the connector will write to the partition for the current wall clock time; with auto table creation on, the connector will create tables partitioned by ingestion time.

RECORD_TIME: Existing tables should be partitioned by ingestion time, and the connector will write to the partition corresponding to each Kafka record’s timestamp; with auto table creation on, the connector will create tables partitioned by ingestion time. The only supported time.partitioning.type value for RECORD_TIME is DAY.

TIMESTAMP_COLUMN: Connector relies only on how the existing tables are set up; with auto table creation on, connector will create tables partitioned by a field in a Kafka record value.

  • Type: string
  • Default: INGESTION_TIME
  • Importance: high
time.partitioning.type

The time partitioning type to use when creating new tables for partitioning.type INGESTION_TIME, RECORD_TIME or TIMESTAMP_COLUMN. Existing tables will not be altered to use this partitioning type.

  • Type: string
  • Default: DAY
  • Importance: low
timestamp.partition.field.name

The name of the field in the value that contains the timestamp to partition by in BigQuery and to enable timestamp partitioning for each table. This config will be ignored if partitioning.type is not TIMESTAMP_COLUMN or auto.create.tables is false.

  • Type: string
  • Importance: low
allow.schema.unionization

If enabled, will cause record schemas to be combined with the current schema of the BigQuery table when performing schema updates. This can be useful if, for example, some Kafka records have schemas that are missing some fields that correspond to columns that are already present in the table’s schema. However, this may pose a risk if unrelated records are accidentally produced to a topic consumed by the connector, as instead of failing on invalid data, the connector would simply append all of the fields for those unrelated records’ schemas to the BigQuery table’s schema.

  • Type: boolean
  • Default: false
  • Importance: low

Number of tasks for this connector

tasks.max
  • Type: int
  • Valid Values: [1,…]
  • Importance: high

Next Steps

See also

For an example that shows fully-managed Confluent Cloud connectors in action with Confluent Cloud ksqlDB, see the Cloud ETL Demo. This example also shows how to use Confluent CLI to manage your resources in Confluent Cloud.

../_images/topology.png