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