Cloud Storage Sink connectors stream data from Kafka to Cloud Storage buckets, which lets you store and process large volumes of data in a cost-effective and scalable manner.
Use cases for Cloud Storage Sink connectors include the following:
Data lake ingestion. Store Kafka data in a data lake for long-term archival and batch processing.
Archive data to meet regulatory requirements.
Before you begin
Before creating a Cloud Storage Sink connector, ensure you have the following:
Create a Managed Service for Apache Kafka cluster for your Connect cluster. This is the primary Kafka cluster associated with the Connect cluster. This is also the source cluster that forms one end of the connector pipeline.
Create a Connect cluster to host your Cloud Storage Sink connector.
Create a Cloud Storage bucket to store the data streamed from Kafka.
Create and configure a Kafka topic within the source cluster. Data moves from this Kafka topic to the destination Cloud Storage bucket.
Required roles and permissions
To get the permissions that
you need to create a connector,
ask your administrator to grant you the
Managed Kafka Connector Editor (roles/managedkafka.connectorEditor) IAM role on your project.
For more information about granting roles, see Manage access to projects, folders, and organizations.
This predefined role contains the permissions required to create a connector. To see the exact permissions that are required, expand the Required permissions section:
Required permissions
The following permissions are required to create a connector:
-
Create a connector:
managedkafka.connectors.create
You might also be able to get these permissions with custom roles or other predefined roles.
Grant permissions to write to the Cloud Storage bucket
The Managed Kafka service account must have the following permissions to write messages to the Cloud Storage bucket:
storage.objects.createstorage.objects.delete
Grant the Storage Object User (roles/storage.objectUser) role to the
service account on the project containing the Cloud Storage bucket.
The Managed Kafka service account has the following format:
service-PROJECT_NUMBER@gcp-sa-managedkafka.iam.gserviceaccount.com,
where PROJECT_NUMBER is the project number of the Connect cluster.
If your Connect cluster is in a different project from your Managed Service for Apache Kafka cluster, see Create a Connect cluster in a different project.
Create a Cloud Storage Sink connector
To create a Cloud Storage Sink connector, perform the following steps.
Console
In the Google Cloud console, go to the Connect Clusters page.
Click the Connect cluster for which you want to create the connector.
The Connect cluster details page is displayed.
Click Create connector.
The Create Kafka connector page is displayed.
For the connector name, enter a string.
For guidelines on how to name a connector, see Guidelines to name a Managed Service for Apache Kafka resource.
For Connector plugin, select Cloud Storage Sink.
Specify the Topics from which you can stream data.
Choose the Storage Bucket to store the data.
(Optional) Configure additional settings in the Configuration section.
Select the Task restart policy. For more information, see Task restart policy.
Click Create.
gcloud
-
In the Google Cloud console, activate Cloud Shell.
At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.
Run the
gcloud managed-kafka connectors createcommand:gcloud managed-kafka connectors create CONNECTOR_ID \ --location=LOCATION \ --connect-cluster=CONNECT_CLUSTER_ID \ --config-file=CONFIG_FILEReplace the following:
CONNECTOR_ID: The ID or name of the connector. For guidelines on how to name a connector, see Guidelines to name a Managed Service for Apache Kafka resource. The name of a connector is immutable.LOCATION: The location where you create the connector. This must be the same location where you created the Connect cluster.CONNECT_CLUSTER_ID: The ID of the Connect cluster where the connector is created.CONFIG_FILE: The path to the YAML configuration file for the connector.
Here is an example of a configuration file for the Cloud Storage Sink connector:
connector.class: "io.aiven.kafka.connect.gcs.GcsSinkConnector" gcs.bucket.name: "BUCKET_NAME" gcs.credentials.default: "true" format.output.type: "json" key.converter: "org.apache.kafka.connect.storage.StringConverter" value.converter: "org.apache.kafka.connect.json.JsonConverter" value.converter.schemas.enable: "false" topics: "TOPIC_LIST" tasks.max: "3"Replace the following:
BUCKET_NAME: The name of the Cloud Storage bucket to write to.TOPIC_LIST: A comma-separated list of Kafka topics to read from. The list can also include regular expressions.
Terraform
You can use a Terraform resource to create a connector.
To learn how to apply or remove a Terraform configuration, see Basic Terraform commands.
Go
Before trying this sample, follow the Go setup instructions in Install the client libraries. For more information, see the Managed Service for Apache Kafka Go API reference documentation.
To authenticate to Managed Service for Apache Kafka, set up Application Default Credentials(ADC). For more information, see Set up ADC for a local development environment.
Java
Before trying this sample, follow the Java setup instructions in Install the client libraries. For more information, see the Managed Service for Apache Kafka Java API reference documentation.
To authenticate to Managed Service for Apache Kafka, set up Application Default Credentials. For more information, see Set up ADC for a local development environment.
Python
Before trying this sample, follow the Python setup instructions in Install the client libraries. For more information, see the Managed Service for Apache Kafka Python API reference documentation.
To authenticate to Managed Service for Apache Kafka, set up Application Default Credentials. For more information, see Set up ADC for a local development environment.
After you create a connector, you can edit, delete, pause, stop, or restart the connector.
Configure the connector
For a complete list of the properties that are specific to this connector, see Configuration in the Cloud Storage Sink connector documentation.
Output fields
By default, the connector writes only the record value from each Kafka record.
To include record keys or headers, set the format.output.fields property to a
comma-separated list of fields to include. The supported fields are key,
value, headers, and timestamp. For example:
format.output.fields=key,value,headers
Output formats
The connector supports writing Kafka records in CSV, JSON, and Parquet format. The following sections describe how to configure the output format.
CSV format
To write CSV files to Cloud Storage, set format.output.type=csv.
Use the following converters:
Key converter:
org.apache.kafka.connect.storage.StringConverterororg.apache.kafka.connect.json.JsonConverterValue converter:
org.apache.kafka.connect.converters.ByteArrayConverter
JSON format
To write JSON-formatted files to Cloud Storage, set
format.output.type to one of the following values:
json: Each output file contains a single array of JSON objects, where each JSON object represents one Kafka record.jsonl: The connector writes each Kafka record as a JSON object on a separate line.
The following example shows how the connector writes a Kafka record in JSON format:
{"headers":[{"key":"k1","value":"v1"},{"key":"k2","value":"v2"}],"value":"Hello world","key":"my-key"}
If you set the key or value converter to
org.apache.kafka.connect.converters.ByteArrayConverter, then the connector
writes that field as a base64-encoded value.
The connector doesn't include a JSON schema in the output, even if
value.converter.schemas.enable is true.
Parquet format
To write Parquet files to Cloud Storage, set
format.output.type=parquet.
If the key or value converter is org.apache.kafka.connect.json.JsonConverter,
then the JSON data must include a schema. Set the converter's schemas.enable
property to true.
For JSON data, the connector writes the data as a struct type in the Parquet
file. For example, if the Kafka record value has a field named userid, the
connector writes a column named value.userid. To flatten the column names,
set format.output.envelope=false. If format.output.envelope is false, then
format.output.fields must specify a single output field.
Cloud Storage file settings
The following settings control how the connector writes files to Cloud Storage.
gcs.bucket.name: Required. Specifies the name of the Cloud Storage bucket where the connector writes data.file.compression.type: Specifies the compression type for the output files. Examples aregzip,snappy,zstd, andnone. The default value isnone.file.name.prefix: Specifies a prefix to be added to the name of each file. The default value is empty ("").file.name.template: Specifies a template for the filename. The connector supports placeholder variables for the template, such as{{topic}}for topic name and{{partition}}for partition number. For more information, see the Cloud Storage Sink connector documentation.
Record grouping
When the Cloud Storage Sink connector writes Kafka records, by default
it groups the records by topic and partition. For example, records from
partition 0 of topic my-topic are written to files named
my-topic-0-OFFSET, where
OFFSET is the starting offset.
If the file.name.template parameter includes the
{{key}} placeholder variable, then the
connector groups records by key. In this mode, the connector writes a separate
file for each record key. The connector overwrites each file with the latest
record for that key.
Credentials
The gcs.credentials.default property must be set to true. This setting
enables the connector to discover Google Cloud credentials automatically.