在 Ubuntu 上配置 Kafka 主题(Topic),通常分为 使用命令行方式 和 服务端配置方式 两类。下面以 Kafka 3.x(Kafka 自带 ZooKeeper 或 KRaft 模式) 为例说明。
确保你已经:
/opt/kafka 或 /usr/local/kafka)bootstrap-server(如 localhost:9092)# ZooKeeper 模式
bin/zookeeper-server-start.sh config/zookeeper.properties &
bin/kafka-server-start.sh config/server.properties &
# KRaft 模式
bin/kafka-server-start.sh config/kraft/server.properties &
bin/kafka-topics.sh \
--create \
--bootstrap-server localhost:9092 \
--topic test-topic \
--partitions 3 \
--replication-factor 1
参数说明:
--topic:主题名--partitions:分区数--replication-factor:副本数(不能大于 broker 数量)bin/kafka-topics.sh --list --bootstrap-server localhost:9092
bin/kafka-topics.sh --describe --topic test-topic --bootstrap-server localhost:9092
bin/kafka-topics.sh \
--alter \
--topic test-topic \
--partitions 5 \
--bootstrap-server localhost:9092
⚠️ 分区数只能 增加不能减少
bin/kafka-topics.sh \
--delete \
--topic test-topic \
--bootstrap-server localhost:9092
⚠️ 需确保:
delete.topic.enable=true
bin/kafka-topics.sh \
--create \
--topic test-topic \
--bootstrap-server localhost:9092 \
--partitions 3 \
--replication-factor 1 \
--config retention.ms=604800000 \
--config cleanup.policy=delete
常用配置:
retention.ms:消息保留时间retention.bytes:最大存储大小cleanup.policy:delete / compactbin/kafka-configs.sh \
--bootstrap-server localhost:9092 \
--entity-type topics \
--entity-name test-topic \
--alter \
--add-config retention.ms=86400000
编辑:
vim config/server.properties
示例:
num.partitions=3
default.replication.factor=1
log.retention.hours=168
修改后需 重启 Kafka。
auto.create.topics.enable=true
replication factor larger than available brokers副本数 > broker 数量
delete.topic.enable=true如果你告诉我:
我可以给你 更精确的配置示例。