Kafka的安装和使用

2 篇文章 0 订阅

kafka下载地址:https://kafka.apache.org/downloads

选择Binary downloads:

解压进入目录:

admindeMacBook-Pro:Tools yyc$ cd kafka_2.11-1.1.0/
admindeMacBook-Pro:kafka_2.11-1.1.0 yyc$ ls
LICENSE		bin		libs
NOTICE		config		site-docs

Step1.启动zookeeper

admindeMacBook-Pro:kafka_2.11-1.1.0 yyc$ bin/zookeeper-server-start.sh config/zookeeper.properties 
[2018-07-19 12:24:42,461] INFO Reading configuration from: config/zookeeper.properties (org.apache.zookeeper.server.quorum.QuorumPeerConfig)
[2018-07-19 12:24:42,464] INFO autopurge.snapRetainCount set to 3 (org.apache.zookeeper.server.DatadirCleanupManager)
[2018-07-19 12:24:42,464] INFO autopurge.purgeInterval set to 0 (org.apache.zookeeper.server.DatadirCleanupManager)

启动Kafka:

admindeMacBook-Pro:Tools yyc$ cd kafka_2.11-1.1.0/
admindeMacBook-Pro:kafka_2.11-1.1.0 yyc$ bin/kafka-server-start.sh config/server.properties &
[1] 1604
admindeMacBook-Pro:kafka_2.11-1.1.0 yyc$ [2018-07-19 12:27:27,288] INFO Registered kafka:type=kafka.Log4jController MBean (kafka.utils.Log4jControllerRegistration$)

Step2.创建Topic

admindeMacBook-Pro:kafka_2.11-1.1.0 yyc$ bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
Created topic "test".
[2018-07-19 12:32:17,512] INFO [ReplicaFetcherManager on broker 0] Removed fetcher for partitions test-0 (kafka.server.ReplicaFetcherManager)
[2018-07-19 12:32:17,543] INFO [Log partition=test-0, dir=/tmp/kafka-logs] Loading producer state from offset 0 with message format version 2 (kafka.log.Log)

查看创建的Topic:

admindeMacBook-Pro:kafka_2.11-1.1.0 yyc$ bin/kafka-topics.sh --list --zookeeper localhost:2181
test

Step3.发送消息

admindeMacBook-Pro:kafka_2.11-1.1.0 yyc$ bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test 
>this is a me[2018-07-19 12:37:28,352] INFO [GroupMetadataManager brokerId=0] Removed 0 expired offsets in 0 milliseconds. (kafka.coordinator.group.GroupMetadataManager)
^R
this is a message
>th[2018-07-19 12:37:36,269] INFO Updated PartitionLeaderEpoch. New: {epoch:0, offset:0}, Current: {epoch:-1, offset:-1} for Partition: test-0. Cache now contains 0 entries. (kafka.server.epoch.LeaderEpochFileCache)
^R
    
>
>this is a new message

接收消息

admindeMacBook-Pro:kafka_2.11-1.1.0 yyc$ bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic test --from-beginning
Using the ConsoleConsumer with old consumer is deprecated and will be removed in a future major release. Consider using the new consumer by passing [bootstrap-server] instead of [zookeeper].
this is a message


this is a new message

step4.设置多个集群

admindeMacBook-Pro:kafka_2.11-1.1.0 yyc$ cp config/server.properties config/server-1.properties 
admindeMacBook-Pro:kafka_2.11-1.1.0 yyc$ cp config/server.properties config/server-2.properties 
admindeMacBook-Pro:kafka_2.11-1.1.0 yyc$ 

配置文件修改:

config/server-1.properties: 
    broker.id=1 
    listeners=PLAINTEXT://:9093 
    log.dir=/tmp/kafka-logs-1

config/server-2.properties: 
    broker.id=2 
    listeners=PLAINTEXT://:9094 
    log.dir=/tmp/kafka-logs-2

运行:

已经运行了zookeeper和刚才的一个kafka节点,所有我们只需要在启动2个新的kafka节点。

> bin/kafka-server-start.sh config/server-1.properties &
... 
> bin/kafka-server-start.sh config/server-2.properties &
...

设置新topic,备份为3

admindeMacBook-Pro:kafka_2.11-1.1.0 yyc$ bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 3 --partitions 1 --topic my-replicated-topic
[2018-07-19 14:06:43,529] INFO Accepted socket connection from /0:0:0:0:0:0:0:1:56030 (org.apache.zookeeper.server.NIOServerCnxnFactory)

查看每个集群干嘛

admindeMacBook-Pro:kafka_2.11-1.1.0 yyc$ bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic my-replicated-topic
[2018-07-19 14:09:56,044] INFO Accepted socket connection from /127.0.0.1:56137 (org.apache.zookeeper.server.NIOServerCnxnFactory)
[2018-07-19 14:09:56,051] INFO Client attempting to establish new session at /127.0.0.1:56137 (org.apache.zookeeper.server.ZooKeeperServer)
[2018-07-19 14:09:56,052] INFO Established session 0x164b12214f90005 with negotiated timeout 30000 for client /127.0.0.1:56137 (org.apache.zookeeper.server.ZooKeeperServer)
Topic:my-replicated-topic	PartitionCount:1	ReplicationFactor:3	Configs:
Topic: my-replicated-topic	Partition: 0	Leader: 0	Replicas: 0,2,1	Isr: 0,2,1
[2018-07-19 14:09:56,262] INFO Processed session termination for sessionid: 0x164b12214f90005 (org.apache.zookeeper.server.PrepRequestProcessor)
[2018-07-19 14:09:56,264] INFO Closed socket connection for client /127.0.0.1:56137 which had sessionid 0x164b12214f90005 (org.apache.zookeeper.server.NIOServerCnxn)
admindeMacBook-Pro:kafka_2.11-1.1.0 yyc$ 

发布新消息到主题上:

admindeMacBook-Pro:kafka_2.11-1.1.0 yyc$ bin/kafka-console-producer.sh --broker-list localhost:9092 --topic my-replicated-topic
>message 1
>mes[2018-07-19 14:16:42,241] INFO Updated PartitionLeaderEpoch. New: {epoch:0, offset:0}, Current: {epoch:-1, offset:-1} for Partition: my-replicated-topic-0. Cache now contains 0 entries. (kafka.server.epoch.LeaderEpochFileCache)
[2018-07-19 14:16:42,254] INFO Updated PartitionLeaderEpoch. New: {epoch:0, offset:0}, Current: {epoch:-1, offset:-1} for Partition: my-replicated-topic-0. Cache now contains 0 entries. (kafka.server.epoch.LeaderEpochFileCache)
[2018-07-19 14:16:42,255] INFO Updated PartitionLeaderEpoch. New: {epoch:0, offset:0}, Current: {epoch:-1, offset:-1} for Partition: my-replicated-topic-0. Cache now contains 0 entries. (kafka.server.epoch.LeaderEpochFileCache)
^R  
me
>message2
>^CadmindeMacBook-Pro:kafka_2.11-1.1.0 yyc$ 

查看消息:

admindeMacBook-Pro:kafka_2.11-1.1.0 yyc$ bin/kafka-console-consumer.sh --zookeeper localhost:2181 --from-beginning --topic my-replicated-topic

测试容错:

admindeMacBook-Pro:kafka_2.11-1.1.0 yyc$ ps | grep server-1.properties
 4179 ttys001    0:16.61 /usr/bin/java 
 admindeMacBook-Pro:kafka_2.11-1.1.0 yyc$ kill -9 4179
admindeMacBook-Pro:kafka_2.11-1.1.0 yyc$ [2018-07-19 14:21:50,164] WARN caught end of stream exception (org.apache.zookeeper.server.NIOServerCnxn)
EndOfStreamException: Unable to read additional data from client sessionid 0x164b12214f90001, likely client has closed socket
	at org.apache.zookeeper.server.NIOServerCnxn.doIO(NIOServerCnxn.java:239)
	at org.apache.zookeeper.server.NIOServerCnxnFactory.run(NIOServerCnxnFactory.java:203)
	at java.lang.Thread.run(Thread.java:748)
[2018-07-19 14:21:50,165] INFO Closed socket connection for client /127.0.0.1:55971 which had sessionid 0x164b12214f90001 (org.apache.zookeeper.server.NIOServerCnxn)

[3]-  Killed: 9               bin/kafka-server-start.sh config/server-1.properties

Step5.使用 Kafka Connect 来 导入/导出 数据

Step6.使用Kafak处理流数据

 

http://orchome.com/6

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Kafka安装使用可以分为以下几个步骤: 1. 下载和解压缩Kafka 首先,你需要从Kafka官方网站下载Kafka的二进制文件。解压缩下载的文件到你选择的目录。 2. 配置Kafka 进入Kafka解压缩后的目录,找到`config`文件夹。在该文件夹中,你可以找到一些配置文件,其中最重要的是`server.properties`文件。你可以使用文本编辑器打开该文件,并根据你的需求进行配置。至少需要配置以下几个参数: - `listeners`:指定Kafka监听的地址和端口号。 - `log.dirs`:指定Kafka存储日志文件的目录。 - `zookeeper.connect`:指定Zookeeper的地址和端口号。 3. 启动Zookeeper 在Kafka安装目录中,找到`bin`文件夹,然后执行以下命令启动Zookeeper: ```shell ./zookeeper-server-start.sh ../config/zookeeper.properties ``` 4. 启动Kafka 在同样的`bin`文件夹中,执行以下命令启动Kafka服务器: ```shell ./kafka-server-start.sh ../config/server.properties ``` 5. 创建Topic 在Kafka安装目录中,找到`bin`文件夹,然后执行以下命令创建一个名为`mytopic`的Topic: ```shell ./kafka-topics.sh --create --topic mytopic --bootstrap-server localhost:9092 --partitions 1 --replication-factor 1 ``` 6. 发送和接收消息 使用以下命令发送消息到`mytopic` Topic: ```shell ./kafka-console-producer.sh --topic mytopic --bootstrap-server localhost:9092 ``` 使用以下命令从`mytopic` Topic接收消息: ```shell ./kafka-console-consumer.sh --topic mytopic --bootstrap-server localhost:9092 --from-beginning ``` 这样,你就完成了Kafka安装使用

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值