kafka偏移量offset--java

maven

<dependency>
   <groupId>org.apache.kafka</groupId>
   <artifactId>kafka_2.10</artifactId>
   <version>0.9.0-kafka-2.0.2</version>
</dependency>
private SparkKafka kafka      = null ;

private static final String TOPIC_SOURCE = "TP_LABEL";

public SparkStoredKuduApp(String[] args){

    kafka_conf = KafkaPool.getInstance().getConfig();

    kafka_conf.setProperty("zookeeper_connect", "personas1:2181,personas2:2181,personas4:2181");
    kafka_conf.setProperty("groupid_tdx", "tpsc01"); //tpsc01
    kafka_conf.setProperty("bootstrap.servers", "personas1:9092,personas2:9092,personas4:9092");

    kafka = new SparkKafka(kafkaParams());
    kafka.setTopics(new HashSet<>(Arrays.asList(TOPIC_SOURCE)));
}

private Map<String, String> kafkaParams() {
    Map<String, String> kafkaParams = new HashMap<String, String>();
    kafkaParams.put(ConsumerConfig.GROUP_ID_CONFIG, kafka_conf.getProperty("groupid_tdx"));
    kafkaParams.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, kafka_conf.getProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG));
    kafkaParams.put("zookeeper.connect", kafka_conf.getProperty("zookeeper_connect"));
    return kafkaParams;
}
// 获取kafka开始读取偏移量
Map<TopicAndPartition, Long> fromOffsets = kafka.getOffset();
public class SparkKafka implements Serializable {
   private static final long serialVersionUID = -7633373735487600970L;
   private Map<String, String> kafkaParams = null;
   private Set<String> topics = null;
   private KafkaCluster kafkaCluster = null;

   public SparkKafka(Map<String, String> kafkaParams) {
      this.kafkaParams = kafkaParams;
      init();
   }

   private void init() {
      scala.collection.mutable.Map<String, String> mutableKafkaParam = JavaConversions.mapAsScalaMap(kafkaParams);
      scala.collection.immutable.Map<String, String> immutableKafkaParam = mutableKafkaParam
            .toMap(new Predef.$less$colon$less<Tuple2<String, String>, Tuple2<String, String>>() {
               @Override
               public Tuple2<String, String> apply(Tuple2<String, String> v1) {
                  return v1;
               }
            });
      kafkaCluster = new KafkaCluster(immutableKafkaParam);
   }

   /**
    * 获取kafka offset
    * 
    * @return
    */
   public Map<TopicAndPartition, Long> getOffset() {
      Map<TopicAndPartition, Long> fromOffsets = new HashMap<TopicAndPartition, Long>();

      scala.collection.mutable.Set<String> mutableTopics = JavaConversions.asScalaSet(this.topics);
      scala.collection.immutable.Set<String> immutableTopics = mutableTopics.toSet();
      scala.collection.immutable.Set<TopicAndPartition> scalaTopicAndPartitionSet = kafkaCluster
            .getPartitions(immutableTopics).right().get();

      // 首次消费
      if (kafkaCluster.getConsumerOffsets(kafkaParams.get(ConsumerConfig.GROUP_ID_CONFIG), scalaTopicAndPartitionSet)
            .isLeft()) {
         scala.collection.immutable.Map<TopicAndPartition, LeaderOffset> earliestOffsetsTemp = kafkaCluster
               .getEarliestLeaderOffsets(scalaTopicAndPartitionSet).right().get();
         Set<TopicAndPartition> javaTopicAndPartitionSet = JavaConversions.setAsJavaSet(scalaTopicAndPartitionSet);
         Map<TopicAndPartition, LeaderOffset> earliestOffsets = JavaConversions.mapAsJavaMap(earliestOffsetsTemp);
         for (TopicAndPartition topicAndPartition : javaTopicAndPartitionSet) {
            LeaderOffset latestOffset = earliestOffsets.get(topicAndPartition);
            fromOffsets.put(topicAndPartition, latestOffset.offset());
         }
      } else {
         scala.collection.immutable.Map<TopicAndPartition, LeaderOffset> earliestOffsetsTemp = kafkaCluster
               .getEarliestLeaderOffsets(scalaTopicAndPartitionSet).right().get();
         scala.collection.immutable.Map<TopicAndPartition, Object> consumerOffsetsTemp = kafkaCluster
               .getConsumerOffsets(kafkaParams.get(ConsumerConfig.GROUP_ID_CONFIG), scalaTopicAndPartitionSet)
               .right().get();
         Map<TopicAndPartition, LeaderOffset> earliestOffsets = JavaConversions.mapAsJavaMap(earliestOffsetsTemp);
         Map<TopicAndPartition, Object> consumerOffsets = JavaConversions.mapAsJavaMap(consumerOffsetsTemp);
         Set<TopicAndPartition> javaTopicAndPartitionSet = JavaConversions.setAsJavaSet(scalaTopicAndPartitionSet);
         for (TopicAndPartition topicAndPartition : javaTopicAndPartitionSet) {
            LeaderOffset earliestOffset = earliestOffsets.get(topicAndPartition);
            Long offset = (Long) consumerOffsets.get(topicAndPartition);
            // 如果消费的offset小于leaderearlistOffset,有可能是kafka定时清理已删除该offset文件
            // 这时将过期的offset更新为leaderearlistOffset开始消费,避免offsetOutOfRang异常
            if (offset < earliestOffset.offset()) {
               offset = earliestOffset.offset();
            }
            fromOffsets.put(topicAndPartition, offset);
         }
      }
      return fromOffsets;
   }

   /**
    * 设置kafka offset
    * 
    * @param range
    */
   public void setOffset(HasOffsetRanges range) {
      OffsetRange[] offsets = range.offsetRanges();
      for (OffsetRange o : offsets) {
         // 封装topic.partition  offset对应关系 java Map
         TopicAndPartition topicAndPartition = new TopicAndPartition(o.topic(), o.partition());
         Map<TopicAndPartition, Object> topicAndPartitionObjectMap = new HashMap<TopicAndPartition, Object>();
         topicAndPartitionObjectMap.put(topicAndPartition, o.untilOffset());

         // 转换java map to scala immutable.map
         scala.collection.mutable.Map<TopicAndPartition, Object> map = JavaConversions
               .mapAsScalaMap(topicAndPartitionObjectMap);
         scala.collection.immutable.Map<TopicAndPartition, Object> scalatopicAndPartitionObjectMap = map.toMap(
               new Predef.$less$colon$less<Tuple2<TopicAndPartition, Object>, Tuple2<TopicAndPartition, Object>>() {
                  private static final long serialVersionUID = 1L;

                  public Tuple2<TopicAndPartition, Object> apply(Tuple2<TopicAndPartition, Object> v1) {
                     return v1;
                  }
               });

         // 更新offsetkafkaCluster
         kafkaCluster.setConsumerOffsets(kafkaParams.get(ConsumerConfig.GROUP_ID_CONFIG),
               scalatopicAndPartitionObjectMap);
      }
   }

   @SuppressWarnings("unchecked")
   public static Class<MessageAndMetadata<String, byte[]>> getMsgClass() {
      return (Class<MessageAndMetadata<String, byte[]>>) (Class<?>) MessageAndMetadata.class;
   }

   public Map<String, String> getKafkaParams() {
      return kafkaParams;
   }

   public void setKafkaParams(Map<String, String> kafkaParams) {
      this.kafkaParams = kafkaParams;
   }

   public Set<String> getTopics() {
      return topics;
   }

   public void setTopics(Set<String> topics) {
      this.topics = topics;
   }

   public KafkaCluster getKafkaCluster() {
      return kafkaCluster;
   }

   public void setKafkaCluster(KafkaCluster kafkaCluster) {
      this.kafkaCluster = kafkaCluster;
   }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要获取Kafka偏移量,可以使用Kafka Consumer API提供的方法。以下是获取Kafka偏移量的示例代码: ```java import org.apache.kafka.clients.consumer.ConsumerConfig; import org.apache.kafka.clients.consumer.ConsumerRecord; import org.apache.kafka.clients.consumer.ConsumerRecords; import org.apache.kafka.clients.consumer.KafkaConsumer; import org.apache.kafka.common.TopicPartition; import java.util.Arrays; import java.util.Properties; public class KafkaOffsetExample { private static final String TOPIC_NAME = "my-topic"; private static final String BOOTSTRAP_SERVERS = "localhost:9092"; private static final String GROUP_ID = "my-group"; public static void main(String[] args) { Properties props = new Properties(); props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, BOOTSTRAP_SERVERS); props.put(ConsumerConfig.GROUP_ID_CONFIG, GROUP_ID); props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer"); props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer"); KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props); // 订阅主题 consumer.subscribe(Arrays.asList(TOPIC_NAME)); // 获取分区 TopicPartition partition = new TopicPartition(TOPIC_NAME, 0); // 获取偏移量 long offset = consumer.position(partition); System.out.println("Offset: " + offset); // 关闭消费者 consumer.close(); } } ``` 在这个例子中,我们使用KafkaConsumer类获取Kafka偏移量。首先,我们创建KafkaConsumer实例并订阅主题。然后,我们获取分区并使用position()方法获取当前偏移量。最后,我们打印偏移量并关闭消费者。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值