安装单机kafka
Posted 从零开始的linux
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了安装单机kafka相关的知识,希望对你有一定的参考价值。
安装jdk
安装kafka
[root@localhost kafka_2.11-1.0.0]# wget http://mirrors.shuosc.org/apache/kafka/1.0.0/kafka_2.11-1.0.0.tgz
[root@localhost kafka_2.11-1.0.0]# cd kafka_2.11-1.0.0/
#启动zookeeper
[root@localhost kafka_2.11-1.0.0]# bin/zookeeper-server-start.sh -daemon config/zookeeper.properties
#启动kafka
[root@localhost kafka_2.11-1.0.0]# bin/kafka-server-start.sh -daemon config/server.properties
查看端口
root@localhost kafka_2.11-1.0.0]# netstat -nltp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 989/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1095/master
tcp6 0 0 :::38544 :::* LISTEN 1692/java
tcp6 0 0 :::22 :::* LISTEN 989/sshd
tcp6 0 0 :::38489 :::* LISTEN 2278/java
tcp6 0 0 ::1:25 :::* LISTEN 1095/master
tcp6 0 0 :::9092 :::* LISTEN 2278/java
tcp6 0 0 :::2181 :::* LISTEN 1692/java
创建一个单分区单副本的 topic: mytopic
[root@localhost kafka_2.11-1.0.0]# bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic mytopic
Created topic "mytopic".
查看已经存在的 topic
[root@localhost kafka_2.11-1.0.0]# bin/kafka-topics.sh --list --zookeeper localhost:2181
mytopic
[root@localhost kafka_2.11-1.0.0]# bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic mytopic
Topic:mytopic PartitionCount:1 ReplicationFactor:1 Configs:
Topic: mytopic Partition: 0 Leader: 0 Replicas: 0 Isr: 0
产生消息
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic mytopic
打开另一个终端消费消息
[root@localhost kafka_2.11-1.0.0]# bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic mytopic --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].
hello
使用 Kafka Connect 导入导出数据
#首先运行一个脚本每隔一段时间追加一个数字
[root@localhost kafka_2.11-1.0.0]# for i in {1..300};do echo $i >> test.txt; sleep 1; done
#根据配置文件connect-file-source.properties
里面的源文件填写file=test.txt,connect-file-sink.propertiest为file=test.sink.txt
#从一个文件中读取输入到另一个文件
[root@localhost kafka_2.11-1.0.0]# bin/connect-standalone.sh -daemon config/connect-standalone.properties config/connect-file-source.properties config/connect-file-sink.properties
#查看另一个文件
[root@localhost kafka_2.11-1.0.0]# tail -f test.sink.txt
1
2
3
4
5
6
7
8
9
[root@localhost kafka_2.11-1.0.0]# bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic connect-test
Topic:connect-test PartitionCount:1 ReplicationFactor:1 Configs:
Topic: connect-test Partition: 0 Leader: 0 Replicas: 0 Isr: 0
以上是关于安装单机kafka的主要内容,如果未能解决你的问题,请参考以下文章