之前笔者曾经写过通过scala的方式获取kafka最新的offset
但是大多数的情况我们需要使用java的方式进行获取最新offset
以下是通过java代码获取kafka最新offset
GetOffsetShellWrap
public class GetOffsetShellWrap { private static Logger log = LoggerFactory.getLogger(GetOffsetShellWrap.class); private String topic; private int port; private String host; private int time; public GetOffsetShellWrap(String topic,int port,String host,int time) { this.topic = topic; this.port = port; this.host = host; this.time = time; } public Map<String, String> getEveryPartitionMaxOffset() { //1.获取topic所有分区 以及每个分区的元数据 => 返回 Map<分区id,分区元数据> TreeMap<Integer, PartitionMetadata> partitionIdAndMeta = findTopicEveryPartition(); Map<String, String> map = new HashMap<String, String>(); for (Entry<Integer, PartitionMetadata> entry : partitionIdAndMeta.entrySet()) { int leaderPartitionId = entry.getKey(); //2.根据每个分区的元数据信息 ==> 获取leader分区的主机 S