Kafka入门到实战-第二弹

Kafka入门到实战

Kafka快速开始

官网地址

声明: 由于操作系统, 版本更新等原因, 文章所列内容不一定100%复现, 还要以官方信息为准

bash 复制代码
https://kafka.apache.org/

Kafka概述

Apache Kafka 是一个开源的分布式事件流平台,提供高性能数据管道、流分析、 数据集成和任务关键型应用程序。

Kafka术语

  • Event 事件具有键、值、时间戳和可选的元数据头
  • Producers 生产者是向 Kafka 发布(写入)事件的客户端应用程序
  • Consumers 消费者是订阅(读取和处理)这些事件的客户端应用程序
  • Topics 主题类似于文件系统中的文件夹,事件是该文件夹中的文件
  • Partitioned 一个主题分布在位于不同 Kafka 代理上的多个"存储桶"上
  • Replicated 数据具有容错性和高度可用性, 可以有多个备份

Kafka初体验

  • 下载安装部分, 过于简单, 不宜演示, 请参考官网教程
  • 创建一个主题,名称叫做quickstart-events
bash 复制代码
bin/kafka-topics.sh --create --topic quickstart-events --bootstrap-server localhost:9092
  • 查看topic的详细信息
bash 复制代码
bin/kafka-topics.sh --describe --topic quickstart-events --bootstrap-server localhost:9092
  • 将事件写入到kafka
bash 复制代码
 bin/kafka-console-producer.sh --topic quickstart-events --bootstrap-server localhost:9092
hello
kafka
  • 读取事件
bash 复制代码
bin/kafka-console-consumer.sh --topic quickstart-events --from-beginning --bootstrap-server localhost:9092
  • Kafka Connect 将数据导入/导出为事件流
bash 复制代码
 echo "plugin.path=libs/connect-file-3.7.0.jar" >>config/connect-standalone.properties
 echo -e "foo\nbar" > test.txt
 bin/connect-standalone.sh config/connect-standalone.properties config/connect-file-source.properties config/connect-file-sink.properties

执行完会有一个test.sink.txt文件,

也可以通过下边的命令查看数据

bash 复制代码
bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic connect-test --from-beginning
echo Another line>> test.txt
  • 使用 KAFKA STREAMS 处理事件
    先贴出来代码, 等后边实战会贴出来运行效果
bash 复制代码
KStream<String, String> textLines = builder.stream("quickstart-events");

KTable<String, Long> wordCounts = textLines
            .flatMapValues(line -> Arrays.asList(line.toLowerCase().split(" ")))
            .groupBy((keyIgnored, word) -> word)
            .count();

wordCounts.toStream().to("output-topic", Produced.with(Serdes.String(), Serdes.Long()));

更新计划

欲知后事如何, 请听下回分解

相关推荐
茶杯梦轩3 天前
从零起步学习RabbitMQ || 第三章:RabbitMQ的生产者、Broker、消费者如何保证消息不丢失(可靠性)详解
分布式·后端·面试
IvanCodes3 天前
一、消息队列理论基础与Kafka架构价值解析
大数据·后端·kafka
回家路上绕了弯5 天前
深入解析Agent Subagent架构:原理、协同逻辑与实战落地指南
分布式·后端
初次攀爬者5 天前
Kafka的Rebalance基础介绍
后端·kafka
初次攀爬者6 天前
Kafka + KRaft模式架构基础介绍
后端·kafka
初次攀爬者6 天前
Kafka + ZooKeeper架构基础介绍
后端·zookeeper·kafka
初次攀爬者6 天前
Kafka 基础介绍
spring boot·kafka·消息队列
DemonAvenger10 天前
Kafka性能调优:从参数配置到硬件选择的全方位指南
性能优化·kafka·消息队列
初次攀爬者10 天前
ZooKeeper 实现分布式锁的两种方式
分布式·后端·zookeeper