This page shows how to use Dataflow to read data from Google Cloud Managed Service for Apache Kafka and write the records to a BigQuery table. This tutorial uses the Apache Kafka to BigQuery template to create the Dataflow job.
Overview
Apache Kafka is an open source platform for streaming events. Kafka is commonly used in distributed architectures to enable communication between loosely coupled components. You can use Dataflow to read events from Kafka, process them, and write the results to a BigQuery table for further analysis.
Managed Service for Apache Kafka is a Google Cloud service that helps you run secure and scalable Kafka clusters.
Required permissions
The Dataflow worker service account must have the following Identity and Access Management (IAM) roles:
- Managed Kafka Client (
roles/managedkafka.client) - BigQuery Data Editor (
roles/bigquery.dataEditor)
For more information, see Dataflow security and permissions.
Create a Kafka cluster
In this step, you create a Managed Service for Apache Kafka cluster. For more information, see Create a Managed Service for Apache Kafka cluster.
Console
Go to the Managed Service for Apache Kafka > Clusters page.
Click Create.
In the Cluster name box, enter a name for the cluster.
In the Region list, select a location for the cluster.
Click Create.
gcloud
Use the
managed-kafka clusters create
command.
gcloud managed-kafka clusters create CLUSTER \
--location=REGION \
--cpu=3 \
--memory=3GiB \
--subnets=projects/PROJECT_ID/regions/REGION/subnetworks/SUBNET_NAME
Replace the following:
CLUSTER: a name for the clusterREGION: the region where you created the subnetPROJECT_ID: your project IDSUBNET_NAME: the subnet where you want to deploy the cluster
Creating a cluster usually takes 20-30 minutes.
Create a Kafka topic
After the Managed Service for Apache Kafka cluster is created, create a topic.
Console
Go to the Managed Service for Apache Kafka > Clusters page.
Click the name of the cluster.
In the cluster details page, click Create Topic.
In the Topic name box, enter a name for the topic.
Click Create.
gcloud
Use the
managed-kafka topics create
command.
gcloud managed-kafka topics create TOPIC_NAME \
--cluster=CLUSTER \
--location=REGION \
--partitions=10 \
--replication-factor=3
Replace the following:
TOPIC_NAME: the name of the topic to create
Create a BigQuery table
In this step, you create a BigQuery table with the following schema: