命令行操作
kafka命令行操作主要分为三大类:
- 主题命令行操作
- 生产者命令行操作
- 消费者命令行操作
下面以Kafka 2.3.0版本为例介绍常用的脚本使用方法,如果要下载其它Kafka版本,访问该链接Kakfa Download
主题命令行
查看主题命令行参数
进出kafka部署路径下的bin目录,执行kafka-topics.sh脚本,所有执行参数如下所示:
Create, delete, describe, or change a topic.
Option Description
------ -----------
--alter Alter the number of partitions, replica assignment, and/or
configuration for the topic.
--at-min-isr-partitions if set when describing topics, only
show partitions whose isr count is
equal to the configured minimum. Not
supported with the --zookeeper
option.
--bootstrap-server <String: server to REQUIRED: The Kafka server to connect
connect to> to. In case of providing this, a
direct Zookeeper connection won't be
required.
--command-config <String: command Property file containing configs to be
config property file> passed to Admin Client. This is used
only with --bootstrap-server option
for describing and altering broker
configs.
--config <String: name=value> A topic configuration override for the
topic being created or altered.The
following is a list of valid
configurations:
cleanup.policy
compression.type
delete.retention.ms
file.delete.delay.ms
flush.messages
flush.ms
follower.replication.throttled.
replicas
index.interval.bytes
leader.replication.throttled.replicas
max.compaction.lag.ms
max.message.bytes
message.downconversion.enable
message.format.version
message.timestamp.difference.max.ms
message.timestamp.type
min.cleanable.dirty.ratio
min.compaction.lag.ms
min.insync.replicas
preallocate
retention.bytes
retention.ms
segment.bytes
segment.index.bytes
segment.jitter.ms
segment.ms
unclean.leader.election.enable
See the Kafka documentation for full
details on the topic configs.It is
supported only in combination with --
create if --bootstrap-server option
is used.
--create Create a new topic.
--delete Delete a topic
--delete-config <String: name> A topic configuration override to be
removed for an existing topic (see
the list of configurations under the
--config option). Not supported with
the --bootstrap-server option.
--describe List details for the given topics.
--disable-rack-aware Disable rack aware replica assignment
--exclude-internal exclude internal topics when running
list or describe command. The
internal topics will be listed by
default
--force Suppress console prompts
--help Print usage information.
--if-exists if set when altering or deleting or
describing topics, the action will
only execute if the topic exists.
Not supported with the --bootstrap-
server option.
--if-not-exists if set when creating topics, the
action will only execute if the
topic does not already exist. Not
supported with the --bootstrap-
server option.
--list List all available topics.
--partitions <Integer: # of partitions> The number of partitions for the topic
being created or altered (WARNING:
If partitions are increased for a
topic that has a key, the partition
logic or ordering of the messages
will be affected
--replica-assignment <String: A list of manual partition-to-broker
broker_id_for_part1_replica1 : assignments for the topic being
broker_id_for_part1_replica2 , created or altered.
broker_id_for_part2_replica1 :
broker_id_for_part2_replica2 , ...>
--replication-factor <Integer: The replication factor for each
replication factor> partition in the topic being created.
--topic <String: topic> The topic to create, alter, describe
or delete. It also accepts a regular
expression, except for --create
option. Put topic name in double
quotes and use the '\' prefix to
escape regular expression symbols; e.
g. "test\.topic".
--topics-with-overrides if set when describing topics, only
show topics that have overridden
configs
--unavailable-partitions if set when describing topics, only
show partitions whose leader is not
available
--under-min-isr-partitions if set when describing topics, only
show partitions whose isr count is
less than the configured minimum.
Not supported with the --zookeeper
option.
--under-replicated-partitions if set when describing topics, only
show under replicated partitions
--version Display Kafka version.
--zookeeper <String: hosts> DEPRECATED, The connection string for
the zookeeper connection in the form
host:port. Multiple hosts can be
given to allow fail-over.
上面比较常用的有 --bootstrap-server(连接的Kafka Broker主机名称和端口号),–topic(操作的topic名称), --create(创建主题), --delete(删除主题), --alter(修改主题), --list(查看所有主题), --describle(查看主题详细描述)
查看当前主机的所有主题
防止kafka宕机可以使用两台
[root@hadoop102 ~]# /opt/module/kafka/bin/kafka-topics.sh --bootstrap-server hadoop102:9092,hadoop103:9092 --list
测试一般使用一台即可
[root@hadoop102 ~]# /opt/module/kafka/bin/kafka-topics.sh --bootstrap-server hadoop102:9092 --list
如果产生下图的报错,说明Kafka服务没有启动,需要检查Kafka服务,此处注意要先启动ZK,然后在启动Kafka服务。
创建主题
-
创建一个first主题并指定分区为1,分区副本3份。
[root@hadoop102 ~]# /opt/module/kafka/bin/kafka-topics.sh --bootstrap-server hadoop102:9092 --topic first --create --partitions 1 --replication-factor 3
-
查看所有的主题
[root@hadoop102 ~]# /opt/module/kafka/bin/kafka-topics.sh --bootstrap-server hadoop102:9092 --list
-
查看指定主题的详细信息
[root@hadoop102 ~]# /opt/module/kafka/bin/kafka-topics.sh --bootstrap-server hadoop102:9092 --topic first --describe
修改分区数
注意: 分区数只能增加,不能减少
[root@hadoop102 ~]# /opt/module/kafka/bin/kafka-topics.sh --bootstrap-server hadoop102:9092 --alter --topic first --partitions 3
查看当前分区详情
删除主题
[root@hadoop102 ~]# /opt/module/kafka/bin/kafka-topics.sh --bootstrap-server hadoop102:9092 --delete --topic first
生产者与消费者命令操作
查看命令参数
- 查看生产者命令参数(/kafka-console-producer.sh)
参数 | 描述 |
---|---|
–bootstrap-server <String: server toconnect to> | 连接的 Kafka Broker 主机名称和端口号 |
–topic <String: topic> | 操作的 topic 名称 |
- 查看消费者命令参数(kafka-console-consumer.sh)
参数 | 描述 |
---|---|
–bootstrap-server <String: server toconnect to> | 连接的 Kafka Broker 主机名称和端口号 |
–topic <String: topic> | 操作的 topic 名称 |
–from-beginning | 从头开始消费 |
–group <String: consumer group id> | 指定消费者组名称 |
发送消息与消费消息
使用hadoop102生产消息,hadoop103消费消息。
-
生产消息
一次只能发送一条,多发送会报错,等消费者消费完可以发送之后的消息。[root@hadoop102 kafka]# bin/kafka-console-producer.sh --bootstrap-server hadoop102:9092 --topic first
开启后需要等待,等待消费者开启
- 消费消息
消费 first 主题中的数据
注意:新创建消费者无法消费之前的历史记录,只能消费生产者新产生的记录
[root@hadoop103 kafka]# bin/kafka-console-consumer.sh --bootstrap-server hadoop102:9092 --topic first
-
测试
生产者一条消息
消费一条消息
-
查询消费者消费记录
把主题中所有的数据都读取出来
[root@hadoop102 kafka]# bin/kafka-console-consumer.sh --bootstrap-server hadoop102:9092 --from-beginning --topic first
- 查询脚本
下面提供自己写的一个脚本,方便管理维护相关配置,读者可以根据自己需要进行修改
#!/bin/bash
CMD=$1
TOPIC=$2
if [ $CMD == "start" ]; then
/root/services/kafka_2.11/bin/kafka-server-start.sh -daemon /root/services/kafka_2.11/config/server.properties
elif [ $CMD == "stop" ]; then
/root/services/kafka_2.11/bin/kafka-server-stop.sh
elif [ $CMD == "init" ]; then
/root/services/kafka_2.11/bin/init_kafka.sh
elif [ $CMD == "topic" ]; then
/root/services/kafka_2.11/bin/kafka-topics.sh --bootstrap-server 172.17.0.3:9092 --list
elif [ $CMD == "producer" ]; then
/root/services/kafka_2.11/bin/kafka-console-producer.sh --broker-list 172.17.0.3:9092 --topic $2
elif [ $CMD == "consumer" ]; then
/root/services/kafka_2.11/bin/kafka-console-consumer.sh --bootstrap-server 172.17.0.3:9092 --from-beginning --topic $2
else
echo "Unknown command"
exit
fi