Kafka如何手动维护偏移量?

本文介绍如何在 Spark Streaming 中手动管理 Kafka 消费者的偏移量,包括从 MySQL 获取偏移量和消费完成后更新偏移量的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

0:需求描述

-手动维护kafka偏移量

-做一个单词计数

1.环境准备

三台虚拟机:

node01,node02,node03三台机器已经安装kafka集群。

mysql用来存取kafka的偏移量

 CREATE TABLE `t_offset` (
      `topic` VARCHAR(255) NOT NULL,
      `partition` INT(11) NOT NULL,
      `groupid` VARCHAR(255) NOT NULL,
      `offset` BIGINT(20) DEFAULT NULL,
      PRIMARY KEY (`topic`,`partition`,`groupid`)
    ) ENGINE=INNODB DEFAULT CHARSET=utf8;

 

2.启动生产者:

/export/servers/kafka_2.11-1.0.0/bin/kafka-console-producer.sh --broker-list node01:9092,node01:9092,node01:9092 --topic spark_kafka

3.代码演示

pom依赖:

 <!-- 指定仓库位置,依次为aliyun、cloudera和jboss仓库 -->
    <repositories>
        <repository>
            <id>aliyun</id>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
        </repository>
        <repository>
            <id>cloudera</id>
            <url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
        </repository>
        <repository>
            <id>jboss</id>
            <url>http://repository.jboss.com/nexus/content/groups/public</url>
        </repository>
    </repositories>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <encoding>UTF-8</encoding>
        <scala.version>2.11.8</scala.version>
        <scala.compat.version>2.11</scala.compat.version>
        <hadoop.version>2.7.4</hadoop.version>
        <spark.version>2.2.0</spark.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-library</artifactId>
            <version>${scala.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-core_2.11</artifactId>
            <version>${spark.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-sql_2.11</artifactId>
            <version>${spark.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-hive_2.11</artifactId>
            <version>${spark.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-hive-thriftserver_2.11</artifactId>
            <version>${spark.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-streaming_2.11</artifactId>
            <version>${spark.version}</version>
        </dependency>
        <!-- <dependency>
             <groupId>org.apache.spark</groupId>
             <artifactId>spark-streaming-kafka-0-8_2.11</artifactId>
             <version>${spark.version}</version>
         </dependency>-->
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-streaming-kafka-0-10_2.11</artifactId>
            <version>${spark.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-sql-kafka-0-10_2.11</artifactId>
            <version>${spark.version}</version>
        </dependency>

        <!--<dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>2.6.0-mr1-cdh5.14.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>1.2.0-cdh5.14.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-server</artifactId>
            <version>1.2.0-cdh5.14.0</version>
        </dependency>-->
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-streaming-kafka-0-10_2.11</artifactId>
            <version>2.2.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>2.7.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-server</artifactId>
            <version>1.3.1</version>
        </dependency>
        <!--https://github.com/lightbend/config-->
        <dependency>
            <groupId>com.typesafe</groupId>
            <artifactId>config</artifactId>
            <version>1.3.3</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>
    </dependencies>

    <build>
        <sourceDirectory>src/main/scala</sourceDirectory>
        <plugins>
            <!-- 指定编译java的插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
            </plugin>
            <!-- 指定编译scala的插件 -->
            <plugin>
                <groupId>net.alchim31.maven</groupId>
                <artifactId>scala-maven-plugin</artifactId>
                <version>3.2.2</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>testCompile</goal>
                        </goals>
                        <configuration>
                            <args>
                                <arg>-dependencyfile</arg>
                                <arg>${project.build.directory}/.scala_dependencies</arg>
                            </args>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.18.1</version>
                <configuration>
                    <useFile>false</useFile>
                    <disableXmlReport>true</disableXmlReport>
                    <includes>
                        <include>**/*Test.*</include>
                        <include>**/*Suite.*</include>
                    </includes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.3</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <filters>
                                <filter>
                                    <artifact>*:*</artifact>
                                    <excludes>
                                        <exclude>META-INF/*.SF</exclude>
                                        <exclude>META-INF/*.DSA</exclude>
                                        <exclude>META-INF/*.RSA</exclude>
                                    </excludes>
                                </filter>
                            </filters>
                            <transformers>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass></mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

OffsetUtil工具类

package com.td.kafka

import java.sql.{DriverManager, ResultSet}

import org.apache.kafka.common.TopicPartition
import org.apache.spark.streaming.kafka010.OffsetRange

import scala.collection.mutable

object OffsetUtil {
  //从数据库读取偏移量
  def getOffsetMap(groupid: String, topic: String) = {
    val connection = DriverManager.getConnection("jdbc:mysql://node01:3306/bigdata?characterEncoding=UTF-8", "root", "123456")
    val pstmt = connection.prepareStatement("select * from t_offset where groupid=? and topic=?")
    pstmt.setString(1, groupid)
    pstmt.setString(2, topic)
    val rs: ResultSet = pstmt.executeQuery()
    val offsetMap = mutable.Map[TopicPartition, Long]()
    while (rs.next()) {
      offsetMap += new TopicPartition(rs.getString("topic"), rs.getInt("partition")) -> rs.getLong("offset")
    }
    rs.close()
    pstmt.close()
    connection.close()
    offsetMap
  }

  //将偏移量保存到数据库
  def saveOffsetRanges(groupid: String, offsetRange: Array[OffsetRange]) = {
    val connection = DriverManager.getConnection("jdbc:mysql://node01:3306/bigdata?characterEncoding=UTF-8", "root", "123456")
    //replace into表示之前有就替换,没有就插入
    val pstmt = connection.prepareStatement("replace into t_offset (`topic`, `partition`, `groupid`, `offset`) values(?,?,?,?)")
    for (o <- offsetRange) {
      pstmt.setString(1, o.topic)
      pstmt.setInt(2, o.partition)
      pstmt.setString(3, groupid)
      pstmt.setLong(4, o.untilOffset)
      pstmt.executeUpdate()
    }
    pstmt.close()
    connection.close()
  }
}

SparkKafka6测试类:

package com.td.kafka

import org.apache.kafka.clients.consumer.ConsumerRecord
import org.apache.kafka.common.TopicPartition
import org.apache.kafka.common.serialization.StringDeserializer
import org.apache.spark.streaming.dstream.{DStream, InputDStream}
import org.apache.spark.streaming.kafka010.{OffsetRange, _}
import org.apache.spark.streaming.{Seconds, StreamingContext}
import org.apache.spark.{SparkConf, SparkContext}

import scala.collection.mutable

object SparkKafka6 {
  def main(args: Array[String]): Unit = {
    //1.创建SrearmingContext
    val conf = new SparkConf().setAppName("wc").setMaster("local[*]")
    val sc = new SparkContext(conf)
    sc.setLogLevel("WARN")
    sc.setCheckpointDir("./kafka")

    val ssc = new StreamingContext(sc,Seconds(5))

    //准备连接Kafka的参数
    val kafkaParams = Map[String, Object](
      "bootstrap.servers" -> "node01:9092,node02:9092,node03:9092",
      "key.deserializer" -> classOf[StringDeserializer],
      "value.deserializer" -> classOf[StringDeserializer],
      "group.id" -> "SparkKafkaDemo",
      "auto.offset.reset" -> "latest",
      "enable.auto.commit" -> (false: java.lang.Boolean)
    )
    val topics = Array("spark_kafka")

    //2.使用KafkaUtil连接Kafak获取数据
    //注意:
    //如果MySQL中没有记录offset,则直接连接,从latest开始消费
    //如果MySQL中有记录offset,则应该从该offset处开始消费
    val offsetMap: mutable.Map[TopicPartition, Long] = OffsetUtil.getOffsetMap("SparkKafkaDemo","spark_kafka")

    val recordDStream: InputDStream[ConsumerRecord[String, String]] = {if(offsetMap.size > 0){//有记录offset
      println("MySQL中记录了offset,则从该offset处开始消费")
      KafkaUtils.createDirectStream[String, String](ssc,
        LocationStrategies.PreferConsistent,//位置策略,源码强烈推荐使用该策略,会让Spark的Executor和Kafka的Broker均匀对应
        ConsumerStrategies.Subscribe[String, String](topics, kafkaParams,offsetMap))//消费策略,源码强烈推荐使用该策略
    }else{//没有记录offset
      println("没有记录offset,则直接连接,从latest开始消费")
      // /export/servers/kafka/bin/kafka-console-producer.sh --broker-list node01:9092 --topic  spark_kafka
      KafkaUtils.createDirectStream[String, String](ssc,
        LocationStrategies.PreferConsistent,//位置策略,源码强烈推荐使用该策略,会让Spark的Executor和Kafka的Broker均匀对应
        ConsumerStrategies.Subscribe[String, String](topics, kafkaParams))//消费策略,源码强烈推荐使用该策略
    }}


    recordDStream.foreachRDD(rdd=>{
      if(rdd.count() > 0){//当前这一时间批次有数据
        rdd.foreach(record => println("接收到的Kafk发送过来的数据为:" + record))
        //接收到的Kafk发送过来的数据为:ConsumerRecord(topic = spark_kafka, partition = 2, offset = 18, CreateTime = 1601332815296, checksum = 2190377400, serialized key size = -1, serialized value size = 2, key = null, value = aa)
        //注意:通过打印接收到的消息可以看到,里面有我们需要维护的offset,和要处理的数据
        //接下来可以对数据进行处理....或者使用transform返回和之前一样处理
        //处理数据的代码写完了,就该维护offset了,那么为了方便我们对offset的维护/管理,spark提供了一个类,帮我们封装offset的数据
        val offsetRanges: Array[OffsetRange] = rdd.asInstanceOf[HasOffsetRanges].offsetRanges
        for (o <- offsetRanges){
          println(s"topic=${o.topic},partition=${o.partition},fromOffset=${o.fromOffset},untilOffset=${o.untilOffset}")
        }
        //手动提交offset,默认提交到Checkpoint中
        //recordDStream.asInstanceOf[CanCommitOffsets].commitAsync(offsetRanges)
        //实际中偏移量可以提交到MySQL/Redis中
        OffsetUtil.saveOffsetRanges("SparkKafkaDemo",offsetRanges)
      }
    })

    val lineDStream: DStream[String] = recordDStream.map(_.value())//_指的是ConsumerRecord
    val wrodDStream: DStream[String] = lineDStream.flatMap(_.split(" ")) //_指的是发过来的value,即一行数据
    val wordAndOneDStream: DStream[(String, Int)] = wrodDStream.map((_,1))
    val result: DStream[(String, Int)] = wordAndOneDStream.updateStateByKey(updateFunc)
    result.print()
    ssc.start()//开启
    ssc.awaitTermination()//等待优雅停止
  }
  def updateFunc(currentValues:Seq[Int],historyValue:Option[Int]): Option[Int] ={
    val result = currentValues.sum+historyValue.getOrElse(0)
    Some(result)
  }
}

测试效果:

 

mysql:

控制台打印:

 

<think>好的,我现在需要帮助用户理解Kafka偏移量的作用及管理方式。首先,我得回忆一下Kafka的基本概念,特别是关于消费者偏移量的部分。用户提到的引用内容里提到了生产者偏移量(LEO)和消费者偏移量,还有手动提交偏移量的重要性。 首先,我得明确偏移量的定义。偏移量是消息在分区中的唯一标识,对吧?每个分区中的消息都有一个递增的偏移量。生产者写入消息时,LEO会递增,而消费者则记录自己消费到的位置。这样消费者可以知道自己处理到了哪里,下次从哪里继续。这点在引用[2]里提到了。 接下来,用户想知道偏移量的作用。我应该分点说明,比如消息定位、消费进度跟踪、容错恢复和并行处理。这些点能够帮助用户理解为什么偏移量重要。例如,如果没有偏移量,消费者重启后可能无法知道上次处理到哪里,导致重复或丢失消息。 然后是管理方式,分为自动提交和手动提交。自动提交可能方便,但会有延迟或重复消费的问题,引用[1]里提到手动管理更及时。手动提交又分为同步和异步,同步更可靠但性能稍差,异步则相反。需要解释它们的区别,以及适用的场景。 用户可能还想知道如何选择提交方式,这里可以结合应用场景。比如实时性要求高的用自动,需要精确控制的用手动。另外,存储位置方面,早期的ZooKeeper和现在的__consumer_offsets主题,这也是一个知识点,引用[2]提到消费者组+主题+分区作为键存储偏移量。 还要注意可能的异常情况,比如提交失败或重复提交,如何处理这些问题。比如在手动提交时,如果在处理完消息后提交,但提交失败,可能会导致重复消费,所以可能需要更细致的错误处理机制。 用户的问题可能还涉及如何查看或管理偏移量,比如使用kafka-consumer-groups工具,但用户没直接问,可能不需要深入。但如果有相关问题生成的话,可以提出来。 最后,确保回答结构清晰,分点明确,用中文,数学表达式正确。比如在提到偏移量是数字的时候,可以用$offset$这样的格式。同时检查是否有需要引用标记的地方,比如引用[1]和[2]在回答中的适当位置添加。 总结一下,回答的结构应该是:偏移量的定义和作用,然后详细说明管理方式,包括自动和手动,以及存储位置。最后生成相关问题,比如如何选择提交方式,如何保证不丢失消息等。</think>### Kafka偏移量的作用及管理方式 #### 一、偏移量的定义 在Kafka中,**偏移量(Offset)** 是消息在分区中的唯一标识符,以递增的整数形式表示,例如$offset=0,1,2,\dots$。每个分区的偏移量独立维护,包含两类关键角色: 1. **生产者偏移量(LEO)**:指向分区中下一条待写入消息的位置,写入后LEO+1[^2]。 2. **消费者偏移量**:记录消费者组当前消费到的位置,必须满足$消费者offset \leq LEO$[^2]。 #### 二、偏移量的核心作用 1. **消息定位** 消费者通过偏移量确定从分区中哪个位置开始读取数据,例如从$offset=100$恢复消费。 2. **消费进度跟踪** 消费者组可独立管理消费进度,不同组可按照各自偏移量并行处理同一主题。 3. **容错与恢复** 消费者重启后,通过加载偏移量恢复中断前的状态,避免数据丢失或重复。 4. **并行扩展** 结合分区设计,偏移量支持多消费者实例的负载均衡。 #### 三、偏移量管理方式 ##### 1. 自动提交(默认方式) - **机制**:消费者定期(如5秒)将偏移量提交到Kafka内部主题`__consumer_offsets`。 - **特点**: - 简单易用,但可能因提交延迟导致**重复消费**(例如提交间隔内消费者崩溃)。 - 适用场景:允许少量数据重复的实时流处理。 ##### 2. 手动提交 - **分类**: - **同步提交**:调用`commitSync()`阻塞等待提交完成,可靠性高但性能较低。 - **异步提交**:调用`commitAsync()`非阻塞提交,性能高但需自行处理失败重试。 - **代码示例**: ```java while (true) { ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100)); for (ConsumerRecord<String, String> record : records) { processRecord(record); // 处理消息 } consumer.commitAsync(); // 异步提交(或commitSync()) } ``` ##### 3. 存储位置 - **内部主题`__consumer_offsets`**: 以键值对形式存储,键为$消费者组ID+主题名+分区号$,值为当前偏移量[^2]。 - **外部系统(如数据库)**: 需开发者自行实现,适用于需要与业务逻辑绑定的强一致性场景。 #### 四、关键问题与建议 1. **重复消费风险** 自动提交时若消息处理完成但未提交,崩溃后会导致重复消费。建议在手动提交中**先处理消息再提交**。 2. **偏移量重置策略** 配置`auto.offset.reset=earliest/latest/none`,控制偏移量记录时的行为(从头/从最新/抛出异常)。 3. **监控工具** 使用`kafka-consumer-groups.sh`查看消费组偏移量状态: ```bash bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group my-group ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值