高级应用
命令行工具
消费组管理
- 查看消费组
bin/kafka-consumer-groups.sh --bootstrap-server localhost:9892 --list
- 查看消费组详情
bin/kafka-consumer-groups.sh --bootstrap-server localhost:9890 --describe --group groupName
- 查看消费组状态
bin/kafka-consumer-groups.sh --bootstrap-server localhost:9890 --describe --group groupName --state
- 消费组内成员信息
bin/kafka-consumer-groups.sh --bootstrap-server localhost:9890 --describe --group groupName --members
- 删除消费组,如果有消费者在使用则会失败
bin/kafka-consumer-groups.sh --bootstrap-server localhost:9890 --delete --group groupName
消费位移管理
- 重置消费位移,前提是没消费者在消费
bin/kafak-consumer-groups.sh --bootstrap-server localhost:9098 --group groupName --all-topics --reset-offsets --to-earliest --execute
- –all-topics指定了所有主题,修改为–topics,指定单个主题
数据管道connect![在这里插入图片描述](https://img-blog.csdnimg.cn/20200104155349905.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjY3Mjc3Nw==,size_16,color_FFFFFF,t_70)
-
kafka Connect运用用户快速定义并实现各种Connector,让大批量数据导入/导出kafka很方便
-
概念
- 连接器,实现了Connect API,决定需要运行多少个任务,按照任务进行数据分支,从work进行获取任务配置并传递下去
- 任务:负责将数据一如或移除kafka
- work进程:相当和connector和任务的容器,用于负责管理连接器的配置,启动连接器和连接器任务,提供REST API
- 转换器:kafka connect和其他存储系统直接发送或者接受数据之间转换数据
-
独立模式-文件系统
- 使用两个connector,将文件source.txt的内容通过source连接器写入kafka主题中,然后写入source.sink.txt
- FileStreamSource:从source.txt读取并发布到Broker中
- FileStreamSink:从Broker中读取数据并写入source.sink.txt文件中
- 使用两个connector,将文件source.txt的内容通过source连接器写入kafka主题中,然后写入source.sink.txt
-
步骤
- 在worker进程中用到的配置文件,在kafka的bin目录下${KAFKA_HOME}/config/connect-standalone.properties
//kafka集群连接的地址
bootstrap.servers = localhost:9092
//格式转换类
key.converter=org.apache.kafka.connect.json.JsonConverter
value.converter=org.apache.kafka.connect.json.JsonConverter
//json消息中是否包含schema
key.converter.schemas.enable=true
value.converter.schemas.enable=true
//保存偏移量的文件路径
offset.storage.file.filename=/tmp/connect.offsets
//设定提交偏移量的频率
offset.flush.interval.ms=10000- Source使用的配置文件在${KAFKA_HOME}/config/connect-file-souce.properties
//配置连接器的名称
name= local-file-source
//连接器的全限定名称,设置类名称也是可以
connector.class=FileStreamSource
//task数量
tasks.max=1
//数据源的文件路径
file=/temp/source.txt
//主题名称
topic=topic0701- sink使用的配置文件是${KAFKA_HOME}/config/connect-file-sink.properties
//配置连接器的名称
name= local-file-source
//连接器的全限定名称,设置类名称也是可以
connector.class=FileStreamSink
//task数量
tasks.max=1
//数据源的文件路径
file=/temp/sink.txt
//主题名称
topic=topic0701- 启动source连接器
bin/connect-standalone.sh config/connect-standalone.properties config/connect-file-source.properties
- 启动sink连接器
bin/connect-standalone.sh config/connect-standalone.properties config/connect-file-sink.properties
- source写入文本信息(打开sink.txt,就能看到这句话)
echo “Hello kafka,I coming;” >> /temp/source.txt
- 查看sink文件
cat /temp/source.sink.txt
SpringBoot Kafka
- 添加依赖
<!-- https://mvnrepository.com/artifact/org.springframework.kafka/spring-kafka -->
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
<version>2.4.0.RELEASE</version>
</dependency>
- 添加配置(application.properties)
# kafka config
spring.kafka.producer.bootstrap-servers=127.0.0.1:9092
- 消息的发送
@ Autowired
privete KafkaTemplate template;
private static final String topic = "topicName";
/** 发送消息
*/
@GetMapping("/send/{input}")
public String sendToKafka(@PathVariable String input){
this.template.send(topic,input);
return "send success";
}
- 消息的接收
@KafkaListener(id="",topic = "topicName",groupId = "groupName")
public void listener(String input){
System.out.println(input);
}
事务
- 配置文件(application.properties)
# 事务id要唯一
spring.kafka.producer.transaction-id-prefix = kafka_tx
- 第一种方式
@ Autowired privete KafkaTemplate template; private static final String topic = "topicName"; /** 发送消息 */ @GetMapping("/send/{input}") @Transactional(rollbackFor = RuntimeException.class) public String sendToKafka(@PathVariable String input){ this.template.send(topic,input); return "send success"; }
- 第二种方式
@ Autowired
privete KafkaTemplate template;
private static final String topic = "topicName";
/** 发送消息
*/
@GetMapping("/send/{input}")
public String sendToKafka(@PathVariable String input){
this.template.send(topic,input);
template.executeInTransaction(t->
t.send(topic,input);
if("error".queals(input)){
throw new RuntimeException("is error");
}
t.send(topic,input+"anthor");
return true;
);
return "send success";
}
```