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

更新计划

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

相关推荐
Thomas.Sir4 天前
第21课:PyTorch|GPU多卡训练与分布式训练基础【让多卡并行成为你的加速引擎】
人工智能·pytorch·分布式
Cicada1284 天前
库存消息消费的正确性设计——从幂等窗口到批量流水线
分布式·系统架构
Nano叶落4 天前
Kafka 消费积压排查入门:Docker 搭环境亲手制造一次 ‘消息堵死‘,10 分钟看懂 Lag
kafka
Francek Chen5 天前
【大数据处理与分析】数据仓库Hive:04 数据仓库Hive概述
大数据·数据仓库·hive·hadoop·分布式
Gl�ria5 天前
Hadoop/YARN 集群缩容:下线DN节点
大数据·hadoop·分布式
吉甫作诵5 天前
Kafka 集群安装与运维实战:消费组排查、Offset 重置与副本重分配
大数据·运维·分布式·kafka·消息队列
imDwAaY5 天前
消息队列四大核心问题:顺序性、幂等性、可靠性与一致性
学习·kafka·rabbitmq
wno7046 天前
Spring Boot整合Kafka
spring boot·kafka
是Dream呀6 天前
Harness 工程:让 Agent 真正把任务做完
人工智能·分布式·缓存·agent