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()));

更新计划

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

相关推荐
Francek Chen4 小时前
【大数据处理与分析】Spark:03 Spark运行架构
大数据·hadoop·分布式·架构·spark
ps酷教程4 小时前
分布式事务
分布式
真上帝的左手5 小时前
12. 消息队列-RabbitMQ-Spring Boot 集成 RabbitMQ/RocketMQ实战——消息可靠投递、幂等消费、顺序保证
分布式·rabbitmq·java-rocketmq·java-rabbitmq·架构设计·应用架构
kyrie_sakura17 小时前
大模型学习32 --大模型微调3 -- 分布式训练算法
分布式·学习·算法
看浪的路人1 天前
第10讲:运维 Agent——从「被动救火」到「自动驾驶」
c#·linq
qq_410960351 天前
大模型 Prompt 工程在文件上传与分布式存储中的高级用
分布式·prompt
霸道流氓气质1 天前
分布式序列号生成:基于 Redis 原子自增的编号方案
数据库·redis·分布式
大鹏说大话1 天前
iOS组件化方案对比_CocoaPods_SPM与源码拆分
分布式
AI你一生一世1 天前
集中式与分布式怎么选?谈谈金仓数据库的架构取舍
数据库·分布式·架构·分布式架构·数据库选型·金仓数据库·集中式架构
wyd200403031 天前
开源 AnyLink Agent:跨设备 Agent 协作协议与云端共享记忆实践
人工智能·分布式·postgresql·开源·智能体·openclaw·共享记忆