微信号 yogoup
功能介绍 网站性能提升与架构设计
主要内容:1. kafka 安装、启动2. 消息的 生产、消费3. 配置启动集群4. 集群下的容错测试5. 从文件中导入数据,并导出到文件
打开一个新的终端窗口
bin/kafka-topics.sh --create \--zookeeper localhost:2181 \--replication-factor 1 \--partitions 1 \--topic test打开一个新的终端窗口
bin/kafka-console-producer.sh \--broker-list localhost:9092 \--topic test进入输入模式,随意输入信息,例如:
hello world hi打开一个新的终端窗口
bin/kafka-console-consumer.sh \--bootstrap-server localhost:9092 \--topic test \--from-beginning便会显示出刚才发送的两条消息:
hello world hi这时可以打开发送消息的终端窗口,输入新的信息,再返回来就可以看到自动接收到了新消息
修改 config/server-1.properties 的以下几项配置:
broker.id=1 listeners=PLAINTEXT://:9093 log.dir=logs/kafka-logs-1修改 config/server-2.properties 的以下几项配置:
broker.id=2 listeners=PLAINTEXT://:9094 log.dir=logs/kafka-logs-2输入消息:
my test message 1 my test message 2可以正常取得消息
读取消息
bin/kafka-console-consumer.sh \--bootstrap-server localhost:9092 \--from-beginning \--topic my-replicated-topic返回信息:
my test message 1 my test message 2仍然可以正常取得消息
Kafka 中的 connecter 可以与外部系统进行连接,例如文件系统、数据库
下面实验一个简单文件系统交互,从一个文件中导入数据,然后导出到另一个文件中
命令执行后,会输出一系列的日志信息,等待执行完毕
返回结果:
foo bar成功导出了 test.txt 中的数据
执行第2步的命令后,为什么是去读test.txt?为什么写入了test.sink.txt?中间的过程是什么样的?
原因是在于两个配置文件
config/connect-file-source.properties (导入配置)
name=local-file-source connector.class=FileStreamSource tasks.max=1 file=test.txt topic=connect-testfile指定了是从test.txt中导入数据
topic指定了把数据发送到connect-test这个topic
connect-file-sink.properties(导出配置)
name=local-file-sink connector.class=FileStreamSink tasks.max=1 file=test.sink.txt topics=connect-testfile指定了把数据导出到test.txt中导入数据
topic指定从connect-test这个topic中读取数据
查看一下connect-test这个topic
bin/kafka-console-consumer.sh \--bootstrap-server localhost:9092 \--topic connect-test \--from-beginning结果为:
{"schema":{"type":"string","optional":false},"payload":"foo"} {"schema":{"type":"string","optional":false},"payload":"bar"}现在向test.txt中添加一条新数据:
echo "Another line" >> test.txt再次执行 cat test.sink.txt 就会看到刚刚添加的数据:
foo bar Another line更多介绍: http://www.cnblogs.com/ximengchj/p/6423704.html
相关文章:
分布式消息队列 Kafka
Kafka 消息存储及检索
Kafka 高可用设计
Kafka是如何实现高吞吐率的
点击 “阅读原文” 查看 文章列表
赞赏
人赞赏
阅读原文 阅读 投诉 精选留言该文章作者已设置需关注才可以留言
写留言
该文章作者已设置需关注才可以留言写留言
加载中 以上留言由公众号筛选后显示了解留言功能详情
转载于:https://www.cnblogs.com/wangdaijun/p/6498041.html
相关资源:数据结构—成绩单生成器