Kafka to query the offset of a specified time data

Source: Internet
Author: User

Package Com.unimas.test;import Java.util.arraylist;import java.util.collections;import java.util.Date;import Java.util.hashmap;import java.util.list;import java.util.map;import Java.util.map.entry;import java.util.TreeMap; Import Kafka.api.partitionoffsetrequestinfo;import Kafka.common.topicandpartition;import Kafka.javaapi.offsetresponse;import Kafka.javaapi.partitionmetadata;import Kafka.javaapi.topicmetadata;import Kafka.javaapi.topicmetadatarequest;import Kafka.javaapi.consumer.SimpleConsumer;  Public class Kafkaoffsetsearch {

 Public Map<string,string> Getpartitonandoffset () {
int port = 6667; //Port
The time to query
String topic = "TOPIC1"; //Assign a theme
List<string> seeds = new arraylist<string> (); //kafka broke address
Seeds.add ("11.11.184.172");
Seeds.add ("11.11.184.174");
Seeds.add ("11.11.184.183");
Seeds.add ("11.11.184.167");
Seeds.add ("11.11.184.177");
Kafkaoffsetsearch Kot=NewKafkaoffsetsearch (); TreeMap<Integer,PartitionMetadata> Metadatas =Kot.findleader (seeds, port, topic); Map<String,String> map =NewHashmap<string,string>(); List<Long> offsetlist =NewArraylist<>();  for(entry<integer,partitionmetadata>Entry:metadatas.entrySet ()) {            intPartition =Entry.getkey (); String Leadbroker=Entry.getvalue (). Leader (). Host (); String ClientName="Client_"+ Topic +"_"+partition; Simpleconsumer Consumer=NewSimpleconsumer (Leadbroker, Port,100000, -*1024x768, ClientName); LongReadoffset =Getlastoffset (consumer, topic, partition, timestamp, clientName);            Offsetlist.add (Readoffset); Map.put (Partition+"", readoffset+""); System. out. println (partition+":"+readoffset); if(consumer!=NULL) Consumer.close (); }                returnmap; }     Public Static LongGetlastoffset (Simpleconsumer Consumer, String topic,intPartitionLongwhichtime, String clientName) {topicandpartition topicandpartition=Newtopicandpartition (topic, partition); Map<topicandpartition, partitionoffsetrequestinfo> requestinfo =NewHashmap<topicandpartition, partitionoffsetrequestinfo>(); Requestinfo.put (Topicandpartition,NewPartitionoffsetrequestinfo (Whichtime,1)); Kafka.javaapi.OffsetRequest Request=Newkafka.javaapi.OffsetRequest (Requestinfo, Kafka.api.OffsetRequest.CurrentVersion (), CLI        Entname); Offsetresponse Response=Consumer.getoffsetsbefore (Request); if(Response.haserror ()) {System. out. println ("Error fetching Data Offset data the Broker. Reason:"+response.errorcode (topic, partition)); return 0; }        Long[] offsets =response.offsets (topic, partition); returnoffsets[0]; }    PrivateTreemap<integer,partitionmetadata> Findleader (list<string>A_seedbrokers,intA_port, String a_topic) {TreeMap<integer, partitionmetadata> map =NewTreemap<integer, partitionmetadata>(); Loop: for(String seed:a_seedbrokers) {Simpleconsumer consumer=NULL; Try{Consumer=NewSimpleconsumer (seed, A_port,100000, -*1024x768,                        "Leaderlookup"+NewDate (). GetTime ()); List<String> topics =collections.singletonlist (a_topic); Topicmetadatarequest req=Newtopicmetadatarequest (topics); Kafka.javaapi.TopicMetadataResponse resp=Consumer.send (req); List<TopicMetadata> MetaData =Resp.topicsmetadata ();  for(Topicmetadata item:metadata) { for(Partitionmetadata part:item.partitionsMetadata ()) {Map.put (Part.partitionid (), part); }                }            } Catch(Exception e) {System. out. println ("Error communicating with Broker ["+seed+"] to find Leader for ["+ A_topic +", ] Reason:"+e); } finally {                if(Consumer! =NULL) Consumer.close (); }        }        returnmap; }}

Kafka to query the offset of a specified time data

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.