kafka的安装和使用

  • 安装:
  1. kafka_2.11-0.10.0.1.tgz 解压到/app/kafka_2.11-0.10.0.1 目录

tar -zxvf kafka_2.11-0.10.0.1.tgz

  1. 修改配置 /app/kafka_2.11-0.10.0.1/config/server.properties :(主要是broker.id、log.dir、zookeeper.connect)

zookeeper.connect=192.168.56.101:2181,192.168.126.102:2181,192.168.126.103:2181

broker.id=1

log.dirs=/app/kafka_2.11-0.10.0.1/logs

zookeeper.connect=192.168.56.101:2181,192.168.56.102:2181,192.168.56.103:2181

delete.topic.enable=true

listeners=PLAINTEXT://192.168.56.101:9092

advertised.listeners=PLAINTEXT://192.168.56.101:9092

  1. 拷贝完整目录到其他两台机器,修改broker.id,listeners,advertised.listeners即可

  • 配置环境变量

(1)vi /etc/profile

#set kafka environment

export KAFKA_HOME=/app/kafka_2.11-0.10.0.1

export PATH=$PATH:$KAFKA_HOME/bin

(2)生效环境变量

source /etc/profile

  • 主要命令脚本

启动(需先启动zk):

kafka-server-start.sh /app/kafka_2.11-0.10.0.1/config/server.properties

后台方式启动:

kafka-server-start.sh -daemon /app/kafka_2.11-0.10.0.1/config/server.properties

停止命令:

kafka-server-stop.sh

--创建topic

kafka-topics.sh --zookeeper 192.168.56.101:2181 --create --topic testTopic --partitions 4 --replication-factor 2

注: partitions指定topic分区数,replication-factor指定topic每个分区的副本数

--查看所有topic列表

kafka-topics.sh --zookeeper 192.168.56.101:2181 --list

--查看指定topic信息

kafka-topics.sh --zookeeper 192.168.56.101:2181 --describe --topic testTopic

--生产数据

kafka-console-producer.sh --broker-list 192.168.56.101:9092 --topic testTopic

--消费数据

--老版本

kafka-console-consumer.sh  --zookeeper 192.168.56.101:2181  --topic testTopic --from-beginning

(不加from-beginning为从最新消息开始消费)

--新版本

kafka-console-consumer.sh --bootstrap-server 192.168.56.101:9092 --topic testTopic --from-beginning

--删除topic主题或log  begin (通过调整清理策略删除数据)

需有这两个配置

# 启用删除主题

delete.topic.enable=true

# 检查日志段文件的间隔时间,以确定是否文件属性是否到达删除要求。

log.retention.check.interval.ms=10000

--查看topic策略

kafka-configs.sh --zookeeper 192.168.56.101:2181 --describe --entity-type topics --entity-name testTopic

--如果想保留主题,只删除主题现有数据(log)。可以通过修改数据保留时间实现

kafka-configs.sh --zookeeper 192.168.56.101:2181 --entity-type topics --entity-name testTopic --alter --add-config retention.ms=10000

--删除config策略

kafka-configs.sh --zookeeper 192.168.56.101:2181 --entity-type topics --entity-name testTopic --alter --delete-config retention.ms

#慎用

#kafka-topics.sh --delete --zookeeper 192.168.56.101:2181 --topic testTopic

--删除topic主题或log  end

--从文件导入topic,使用connector

connect-standalone.sh config/connect-standalone.properties config/connect-file-source.properties config/connect-file-sink.properties

查看topic某分区偏移量最大(小)值

kafka-run-class.sh kafka.tools.GetOffsetShell --topic testTopic --time -1 --broker-list 192.168.56.101:9092 --partitions 0
注: time为-1时表示最大值,time为-2时表示最小值

  • 问题
  1. sparkStreaming 连接报错:org.apache.spark.SparkException: Couldn't find leader offsets for Set([testTopic2,0], [testTopic2,1], [testTopic2,2], [testTopic2,3])

listeners=PLAINTEXT://192.168.56.101:9092

advertised.listeners=PLAINTEXT://192.168.56.101:9092

这两个配置项没有配置,导致外部无法通过正确的地址连接kafka,配置后重启kafka解决

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值