package com.DemandCube.KafkaSphere.kafka;


import kafka.javaapi.TopicMetadataRequest;
import kafka.javaapi.consumer.SimpleConsumer;

import java.util.ArrayList;
import java.util.List;



public class BrokerMetaData {

  public static void main(String[] args){
    List<kafka.javaapi.TopicMetadata> data = MetaDataDump();
    for (kafka.javaapi.TopicMetadata item : data){
      System.out.println("topic: " + item.topic());
      for (kafka.javaapi.PartitionMetadata part: item.partitionsMetadata()){
        String replicas = "";
        String isr = "";
        for (kafka.cluster.Broker replica: part.replicas()){
          replicas += " " + replica.host();
        }
        for (kafka.cluster.Broker replica: part.isr()){
          isr += " " + replica.host();
        }
        System.out.println("    Partition: " + part.partitionId());
        System.out.println("    Leader   : " + part.leader().host());
        System.out.println("    Replicas : [" + replicas + "]");
        System.out.println("    ISR      : [" + isr + "]");
      }
    }
  }

  public static List<kafka.javaapi.TopicMetadata> MetaDataDump(){
    kafka.javaapi.consumer.SimpleConsumer consumer = new SimpleConsumer("0.0.0.0", 9092, 100000, 64 * 1024, "metadata");
    List<String> topics = new ArrayList<String>();
    TopicMetadataRequest req = new TopicMetadataRequest(topics);
    kafka.javaapi.TopicMetadataResponse res = consumer.send(req);
    return res.topicsMetadata();
  };
}

@@ -178,16 +178,41 @@
 l.List;%0A
+import java.util.HashMap;
 %0A%0A%0Apubli
@@ -235,16 +235,106 @@
 taData %7B
+%0A  public static HashMap%3CString, HashMap%3CString, String%3E%3E brokerDetails = new HashMap%3C%3E();
 %0A%0A  publ
@@ -426,24 +426,118 @@
 DataDump();%0A
+    HashMap%3CString, String%3E topicDetails = new HashMap%3C%3E();%0A    String topic = new String();%0A%0A
     for (kaf
@@ -585,38 +585,15 @@
     
-System.out.println(%22topic: %22 +
+topic =
  ite
@@ -601,17 +601,16 @@
 .topic()
-)
 ;%0A      
@@ -967,47 +967,53 @@
     
-System.out.println(%22    Partition: %22 + 
+topicDetails.put(%22partition%22, String.valueOf(
 part
@@ -1027,16 +1027,18 @@
 ionId())
+ )
 ;%0A      
@@ -1043,46 +1043,34 @@
     
-System.out.println(%22    Leader   : %22 +
+topicDetails.put(%22leader%22,
  par
@@ -1086,16 +1086,17 @@
 ).host()
+ 
 );%0A     
@@ -1102,43 +1102,38 @@
     
-System.out.println(%22    R
+topicDetails.put(%22r
 eplicas
- : 
+%22, %22
 %5B%22 +
@@ -1162,67 +1162,145 @@
     
-System.out.println(%22    ISR      : %5B%22 + isr + %22%5D%22);%0A      %7D
+topicDetails.put(%22isr%22, %22%5B%22 + isr + %22%5D%22);%0A      %7D%0A%0A      brokerDetails.put(topic, topicDetails);%0A      System.out.println(brokerDetails);
 %0A   

add - all metadata to a hashmap for reuse