Home  >  Article  >  Java  >  Steps to create a Kafka topic: Using Kafka commands

Steps to create a Kafka topic: Using Kafka commands

WBOY
WBOYOriginal
2024-01-31 16:33:061111browse

Steps to create a Kafka topic: Using Kafka commands

Steps to create a topic using Kafka commands

  1. Start ZooKeeper and Kafka services
zookeeper-server-start.sh config/zookeeper.properties
kafka-server-start.sh config/server.properties
  1. Create topic
kafka-topics.sh --create --topic my-topic --partitions 3 --replication-factor 1
  1. View topic
kafka-topics.sh --list
  1. Send a message to the topic
kafka-console-producer.sh --topic my-topic --message "Hello, world!"
  1. Consume the message from the topic
kafka-console-consumer.sh --topic my-topic --from-beginning

Specific code example

# 启动ZooKeeper服务
zookeeper-server-start.sh config/zookeeper.properties

# 启动Kafka服务
kafka-server-start.sh config/server.properties

# 创建主题
kafka-topics.sh --create --topic my-topic --partitions 3 --replication-factor 1

# 查看主题
kafka-topics.sh --list

# 向主题发送消息
kafka-console-producer.sh --topic my-topic --message "Hello, world!"

# 从主题消费消息
kafka-console-consumer.sh --topic my-topic --from-beginning

Note

  • When creating a topic, you need to specify the name of the topic, the number of partitions, and the number of replicas.
  • The number of partitions determines how many messages the topic can process at the same time.
  • The number of copies determines how many copies of the topic's data there are.
  • When sending a message to a topic, you need to specify the key and value of the message.
  • When consuming messages from a topic, you can specify a consumer group. Consumers in the consumer group will jointly consume the messages in the topic.

The above is the detailed content of Steps to create a Kafka topic: Using Kafka commands. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn