kafka之十 高级应用

高级应用

命令行工具

消费组管理

  • 查看消费组

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在这里插入图片描述

  • 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文件中
  • 步骤

    • 在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";
    }  
  ```

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值