In Managed Service for Apache Kafka, messages are organized in topics. A topic is made up of partitions. A partition is an ordered, immutable sequence of records that is owned by a single broker within a Kafka cluster. You must create a topic to publish or consume messages.
To create a topic you can use the Google Cloud console, the Google Cloud CLI, the client library, the Managed Kafka API, or the open source Apache Kafka APIs.
Before you begin
You must first create a cluster before creating a topic. Ensure that you have set up the following:
Required roles and permissions to create a topic
To get the permissions that you need to create a topic,
ask your administrator to grant you the
Managed Kafka Topic Editor (roles/managedkafka.topicEditor
) 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 topic. To see the exact permissions that are required, expand the Required permissions section:
Required permissions
The following permissions are required to create a topic:
-
Create a topic:
managedkafka.topics.create
You might also be able to get these permissions with custom roles or other predefined roles.
The Managed Kafka Topic Editor also contains the Managed Kafka viewer role. For more information about this role, see Managed Service for Apache Kafka predefined roles.
Properties of a Managed Service for Apache Kafka topic
When you create or update a Managed Service for Apache Kafka topic, you must specify the following properties.
Topic name
The name of the Managed Service for Apache Kafka topic that you are creating. For guidelines on how to name a topic, see Guidelines to name a Managed Service for Apache Kafka resource. The name of a topic is immutable.
Partition count
The number of partitions in the topic. You can edit a topic to increase the partition count for a topic, but you cannot decrease it. Increasing the number of partitions for a topic that uses a key might change how messages are distributed.
Replication factor
The number of replicas for each partition. If you don't specify the value, the cluster's default replication factor is used.
A higher replication factor can improve data consistency in the event of broker failures, as data is replicated to multiple brokers. For production environments, a replication factor of 3 or higher is recommended. Higher replica counts increase local storage and data transfer costs for the topic. However, they don't increase the persistent storage costs. The replication factor cannot exceed the number of available brokers.
Other parameters
You can also set other Apache Kafka topic-level configuration parameters.
These are specified as key=value
pairs that override the cluster defaults.
Configurations related to topics have a server default and an optional
per-topic override. The format is a comma-separated list of KEY=VALUE
pairs,
where KEY
is the name of the Kafka topic configuration property, and VALUE
is the required setting.These key-value pairs help you override the cluster
defaults. Examples include flush.ms=10
and compression.type=producer
.
For a list of all supported topic-level configurations, see Apache Kafka topic-level configs.
Create a topic
Before you create a topic, review the topic properties.
Console
In the Google Cloud console console, go to the Clusters page.
- Click the cluster for which you want to create a topic.
The Cluster details page opens.
- In the cluster details page, click Create Topic.
The Create Kafka topic page opens.
- For the Topic name, enter a string.
- For Partition count, enter the number of partitions you want or keep the default value.
- For Replication factor, enter the replication factor you want or keep the default value.
- (Optional) To alter any topic configurations, add them as comma-separated key-value pairs in the Configurations field.
- 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 beta managed-kafka topics create
command:gcloud beta managed-kafka topics create TOPIC_ID \ --cluster=CLUSTER_ID --location=LOCATION_ID \ --partitions=PARTITIONS_NUM \ --replication-factor=REPLICATION_FACTOR \ --configs=CONFIGS
Replace the following:
TOPIC_ID: The name of the topic.
CLUSTER: The name of the cluster where you want to create the topic.
LOCATION: The region of the cluster.
For more information about supported locations, see Supported Managed Kafka locations.
PARTITIONS: The number of partitions for the topic.
REPLICATION_FACTOR: The replication factor for the topic.
CONFIGS:Topic-level optional parameters. Specify as comma-separated key-value pairs. For example,
compression.type=producer
.
Terraform
You can use a Terraform resource to create a topic.
To learn how to apply or remove a Terraform configuration, see Basic Terraform commands.