public byte[] readLastMessage(String zookeeperConnect, String topic, int partition) { KafkaUtils ku = new KafkaUtils(); PartitionMetadata pm = ku.findLeader(zookeeperConnect, topic, partition); long logSize = ku.getLogSize(pm.leader().host(), pm.leader().port(), topic, partition); LOG.debug("Topic size [" + logSize + "]"); if (logSize == 0) return null; long start = System.currentTimeMillis(); LOG.debug("Read last message start time(ms) " + start); byte[] bb = ku.readAtOffSet(pm.leader().host(), pm.leader().port(), topic, logSize - 1, 0); LOG.debug("End read last message time(ms) " + (System.currentTimeMillis() - start)); return bb; }
public boolean checkTopicIsEmpty(String zookeeperConnect, String topic, int partition) throws CheckTopicFailureException { try { KafkaUtils ku = new KafkaUtils(); PartitionMetadata pm = ku.findLeader(zookeeperConnect, topic, partition); long logSize = ku.getLogSize(pm.leader().host(), pm.leader().port(), topic, 0); LOG.debug("Topic Size " + logSize); if (logSize == 0) return true; // to be sure there are not previous messages byte[] bb = ku.readAtOffSet(pm.leader().host(), pm.leader().port(), topic, logSize - 1, 0); if (bb == null) return true; } catch (Exception e) { throw new CheckTopicFailureException(e); } return false; }