Google Cloud Spanner Sink Connector for Confluent Cloud

Note

If you are installing the connector locally for Confluent Platform, see Google Cloud Spanner Sink Connector for Confluent Platform.

The Google Cloud Spanner Sink connector moves data from Apache Kafka® to a Google Cloud Spanner database. It writes data from a topic in Kafka to a table in the specified Spanner database. Table auto-creation and limited auto-evolution are supported.

Important

Once this connector moves from Preview to Generally Availability (GA), it will require a subscription for Confluent Cloud commitment for Confluent Cloud Enterprise customers. Without a Confluent Cloud commitment, Confluent Cloud Enterprise customers will not have access to these connectors in GA. Contact your Confluent Account Executive to learn more and to update your subscription, if necessary.

Features

  • The Kafka Connect Google Cloud Spanner sink connector inserts and upserts Kafka records into a Google Cloud Spanner database.
  • auto.create and auto-evolve are supported. If tables or columns are missing, they can be created automatically.

Refer to Cloud connector limitations for additional information.

Caution

Preview connectors are not currently supported and are not recommended for production use. For specific connector limitations, see Cloud connector limitations.

Quick Start

Use this quick start to get up and running with the Confluent Cloud Google Cloud Spanner sink connector. The quick start provides the basics of selecting the connector and configuring it to stream events to a Spanner database.

Prerequisites
  • Authorized access to a Confluent Cloud cluster on GCP.
  • The Confluent Cloud CLI installed and configured for the cluster. See Install and Configure the Confluent Cloud CLI.
  • An operating Google Cloud Spanner instance and database (a table can be auto-created). For the steps necessary to create an instance using the Google Cloud Console, see Quickstart using the console.
  • A GCP service account. You download service account credentials as a JSON file. These credentials are used when setting up the connector configuration.
  • Either one of the following to use for the Kafka cluster credentials fields:
    • A Confluent Cloud API key and secret. After you have created your cluster, go to Cluster settings > API access > Create Key.
    • A Confluent Cloud service account.

Using the Confluent Cloud GUI

Step 1: Launch your Confluent Cloud cluster.

See the Confluent Cloud Quick Start for installation instructions.

Step 2: Add a connector.

Click Connectors > Add connector.

Add a connector

Step 3: Select your connector.

Click the Google Spanner Sink connector icon.

Step 4: Set up the connection.

Note

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

Complete the following and click Continue.

  1. Select one or more topics.
  2. Enter a Connector Name.
  3. Enter your Kafka Cluster credentials. The credentials are either the API key and secret or the service account API key and secret.
  4. Select the input message format.
  5. Enter your GCP credentials. Open the JSON file you downloaded when creating the service account. Copy and paste all file contents into the credentials field.
  6. Enter the Spanner instance ID.
  7. Enter the database ID where topic tables are located or will be created.
  8. Select one of the following insert modes:
    • INSERT: Use the standard INSERT row function. An error occurs if the row already exists in the table.
    • UPDATE: Use the standard UPDATE row function. An error occurs if the row does not exist in the table.
    • UPSERT: This mode is similar to INSERT. However, if the row already exists, the UPSERT function overwrites column values with the new values provided.
  9. Enter the maximum size for batched records. A typical entry here is 1000.
  10. Select whether to automatically create a table or column if it is missing relative to the input record schema.
  11. Enter the maximum number of tasks the connector can run. See Confluent Cloud connector limitations for additional task information.

Configuration properties that are not shown in the Confluent Cloud UI use the default values. For default values and property definitions, see Google Cloud Spanner Sink Connector Configuration Properties.

Step 5: Launch the connector.

Verify the connection details and click Launch.

Launch the connector

Step 6: Check the connector status.

The status for the connector should go from Provisioning to Running.

Step 7: Check the results in Spanner.

  1. From the Google Cloud Console, go to your Spanner project.
  2. Verify that new records are being added to the Spanner database.

For additional information about this connector, see Google Cloud Spanner Sink Connector for Confluent Platform. Note that not all Confluent Platform connector features are provided in the Confluent Cloud connector.

See also

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

Using the Confluent Cloud CLI

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

Note

Make sure you have all your prerequisites completed.

Step 1: List the available connectors.

Enter the following command to list available connectors:

ccloud connector-catalog list

Step 2: Show the required connector configuration properties.

Enter the following command to show the required connector properties:

ccloud connector-catalog describe <connector-catalog-name>

For example:

ccloud connector-catalog describe SpannerSink

Example output:

Following are the required configs:
connector.class: SpannerSink
name
kafka.api.key
kafka.api.secret
topics
input.data.format
gcp.spanner.credentials.json
gcp.spanner.instance.id
gcp.spanner.database.id
tasks.max

Step 3: Create the connector configuration file.

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

{
  "connector.class": "SpannerSink",
  "name": "spanner-sink-connector",
  "kafka.api.key": "<my-kafka-api-key?",
  "kafka.api.secret": "<my-kafka-api-secret>",
  "topics": "pageviews",
  "input.data.format": "AVRO",
  "gcp.spanner.credentials.json": "<my-gcp-credentials>",
  "gcp.spanner.instance.id": "<my-spanner-instance-id>",
  "gcp.spanner.database.id": "<my-spanner-dabase-id>",
  "auto.create": "true",
  "auto.evolve": "true",
  "tasks.max": "1"
}

Note the following property definitions:

  • "name": Sets a name for your new connector.
  • "connector.class": Identifies the connector plugin name.
  • "topics": Identifies the topic name or a comma-separated list of topic names.
  • "input.data.format": Currently supports AVRO only.
  • "gcp.spanner.credentials.json": This contains the contents of the downloaded JSON file. See Formatting GCP credentials for details about how to format and use the contents of the downloaded credentials file.
  • "auto.create" (tables) and "auto-evolve" (columns): (Optional) Sets whether to automatically create tables or columns if they are missing relative to the input record schema. If not entered in the configuration, both default to false.
  • "tasks.max": Maximum number of tasks the connector can run. See Confluent Cloud connector limitations for additional task information.

Configuration properties that are not listed use the default values. For default values and property definitions, see Google Cloud Spanner Sink Connector Configuration Properties.

Formatting GCP 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 additional escape \ characters where needed. See Stringify GCP Credentials.

       {
         "connector.class": "SpannerSink",
         "name": "spanner-sink-connector",
         "kafka.api.key": "<my-kafka-api-key?",
         "kafka.api.secret": "<my-kafka-api-secret>",
         "topics": "pageviews",
         "input.data.format": "AVRO",
         "gcp.spanner.credentials.json": "{\"type\":\"service_account\",\"project_id\":\"connect-
         1234567\",\"private_key_id\":\"omitted\",
         \"private_key\":\"-----BEGIN PRIVATE KEY-----
         \\nMIIEvAIBADANBgkqhkiG9w0BA
         \\n6MhBA9TIXB4dPiYYNOYwbfy0Lki8zGn7T6wovGS5pzsIh
         \\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\"}",
         "gcp.spanner.instance.id": "<my-spanner-instance-id>",
         "gcp.spanner.database.id": "<my-spanner-dabase-id>",
         "auto.create": "true",
         "auto.evolve": "true",
         "tasks.max": "1"
       }
    
  3. Add all the converted string content to the 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:

ccloud connector create --config <file-name>.json

For example:

ccloud connector create --config spanner-sink-config.json

Example output:

Created connector spanner-sink-connector jtt-ix4dl

Step 5: Check the connector status.

Enter the following command to check the connector status:

ccloud connector list

Example output:

ID          |       Name              | Status  | Type
+-----------+-------------------------+---------+------+
jtt-ix4dl   | spanner-sink-connector  | RUNNING | sink

Step 7: Check the results in Spanner.

  1. From the Google Cloud Console, go to your Spanner project.
  2. Verify that new records are being added to the Spanner database.

For additional information about this connector, see Google Cloud Spanner Sink Connector for Confluent Platform. Note that not all Confluent Platform connector features are provided in the Confluent Cloud connector.

Next Steps

Try out a Confluent Cloud demo.

See also

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