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 小时前
【设计模式】基于责任链模式的参数校验
java·spring boot·分布式
ZHOU_WUYI15 小时前
一个简单的分布式追踪系统
分布式
码不停蹄的玄黓19 小时前
MySQL分布式ID冲突详解:场景、原因与解决方案
数据库·分布式·mysql·id冲突
王小王-12319 小时前
基于Hadoop的公共自行车数据分布式存储和计算平台的设计与实现
大数据·hive·hadoop·分布式·hadoop公共自行车·共享单车大数据分析·hadoop共享单车
要开心吖ZSH21 小时前
《Spring 中上下文传递的那些事儿》Part 4:分布式链路追踪 —— Sleuth + Zipkin 实践
java·分布式·spring
幼稚园的山代王1 天前
RabbitMQ 4.1.1初体验
分布式·rabbitmq·ruby
百锦再1 天前
RabbitMQ用法的6种核心模式全面解析
分布式·rabbitmq·路由·消息·通道·交换机·代理
一路向北North1 天前
RabbitMQ简单消息监听和确认
分布式·rabbitmq·ruby
真实的菜1 天前
Kafka生态整合深度解析:构建现代化数据架构的核心枢纽
架构·kafka·linq
一路向北North1 天前
使用reactor-rabbitmq库监听Rabbitmq
分布式·rabbitmq·ruby