Kafka入门

Kafka 安装部署

Kafka 版本:kafka_2.11-0.11.0.0

vim config/server.properties

# The id of the broker. This must be set to a unique integer for each broker.
broker.id=0

# Switch to enable topic deletion or not, default value is false
delete.topic.enable=true

# A comma seperated list of directories under which to store log files
log.dirs=/opt/data/kafka0.11/kafka-logs

# Zookeeper connection string (see zookeeper docs for details).
# This is a comma separated host:port pairs, each corresponding to a zk
# server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002".
# You can also append an optional chroot string to the urls to specify the
# root directory for all kafka znodes.
zookeeper.connect=node01:2181,node02:2181,node03:2181/kafka

zookeeper.connect 配置说明

ZooKeeper连接字符串的格式为:hostname:port,此处hostname和port分别是ZooKeeper集群中某个节点的host和port;为了当某个host宕掉之后你能通过其他ZooKeeper节点进行连接,你可以按照一下方式制定多个hosts:
hostname1:port1, hostname2:port2, hostname3:port3.

ZooKeeper 允许你增加一个“chroot”路径,将集群中所有kafka数据存放在特定的路径下。当多个Kafka集群或者其他应用使用相同ZooKeeper集群时,可以使用这个方式设置数据存放路径。这种方式的实现可以通过这样设置连接字符串格式,如下所示:
hostname1:port1,hostname2:port2,hostname3:port3/chroot/path
这样设置就将所有kafka集群数据存放在/chroot/path路径下。注意,在你启动broker之前,你必须创建这个路径,并且consumers必须使用相同的连接格式。

更详细的配置说明参考Kafka配置说明

启动命令

bin/kafka-server-start.sh -daemon config/server.properties
命令行操作

查看当前服务器中的所有topic

bin/kafka-topics.sh --list --zookeeper node01:2181,node02:2181,node03:2181/kafka

创建topic

bin/kafka-topics.sh --create --zookeeper node01:2181,node02:2181,node03:2181/kafka --replication-factor 1 --partitions 1 --topic test

--topic 指定topic名称。
--replication-factor 指定副本数,副本数不能大于broker数量(副本数 = leader + follower 数量)。
--partitions 指定分区数。

删除topic

bin/kafka-topics.sh --delete --zookeeper node01:2181,node02:2181,node03:2181/kafka --topic test

查看某个topic详情

bin/kafka-topics.sh --describe --zookeeper node01:2181,node02:2181,node03:2181/kafka --topic test

修改分区数

bin/kafka-topics.sh --zookeeper node01:2181,node02:2181,node03:2181/kafka --alter --topic product --partitions 3

分区数只能增加不能减少。

发送消息

bin/kafka-console-producer.sh --broker-list node01:9092,node02:9092,node03:9092 --topic test
>This is a message
>This is another message

消费消息

bin/kafka-console-consumer.sh --bootstrap-server node01:9092,node02:9092,node03:9092 --topic test --from-beginning
This is a message
This is another message
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值