本文整理汇总了Java中kafka.javaapi.TopicMetadata类的典型用法代码示例。如果您正苦于以下问题:Java TopicMetadata类的具体用法?Java TopicMetadata怎么用?Java TopicMetadata使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
TopicMetadata类属于kafka.javaapi包,在下文中一共展示了TopicMetadata类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: fetchTopicMetadataFromBroker
import kafka.javaapi.TopicMetadata; //导入依赖的package包/类
private List<TopicMetadata> fetchTopicMetadataFromBroker(String broker, String... selectedTopics) {
LOG.info(String.format("Fetching topic metadata from broker %s", broker));
SimpleConsumer consumer = null;
try {
consumer = getSimpleConsumer(broker);
for (int i = 0; i < NUM_TRIES_FETCH_TOPIC; i++) {
try {
return consumer.send(new TopicMetadataRequest(Arrays.asList(selectedTopics))).topicsMetadata();
} catch (Exception e) {
LOG.warn(String.format("Fetching topic metadata from broker %s has failed %d times.", broker, i + 1), e);
try {
Thread.sleep((long) ((i + Math.random()) * 1000));
} catch (InterruptedException e2) {
LOG.warn("Caught InterruptedException: " + e2);
}
}
}
} finally {
if (consumer != null) {
consumer.close();
}
}
return null;
}
示例2: getPartitionMetadata
import kafka.javaapi.TopicMetadata; //导入依赖的package包/类
public static PartitionMetadata getPartitionMetadata(final SimpleConsumer consumer, final List<String> topics, final int partitionId) {
try {
TopicMetadataRequest req = new TopicMetadataRequest(topics);
TopicMetadataResponse resp = consumer.send(req);
List<TopicMetadata> topicMetadataList = resp.topicsMetadata();
for (TopicMetadata metaData : topicMetadataList) {
for (PartitionMetadata part : metaData.partitionsMetadata()) {
if (part.partitionId() == partitionId) {
return part;
}
}
}
} catch (Exception e) {
LOG.warn("Unable to fetch partition meta data from host[{}:{}] [{}:{}]", consumer.host(), consumer.port(), topics, partitionId, e);
}
return null;
}
示例3: updateLeaderMap
import kafka.javaapi.TopicMetadata; //导入依赖的package包/类
private void updateLeaderMap() {
for (String broker : brokerList) {
try {
SimpleConsumer consumer = getSimpleConsumer(broker);
TopicMetadataRequest req = new TopicMetadataRequest(auditTopics);
kafka.javaapi.TopicMetadataResponse resp = consumer.send(req);
List<TopicMetadata> metaData = resp.topicsMetadata();
for (TopicMetadata tmd : metaData) {
for (PartitionMetadata pmd : tmd.partitionsMetadata()) {
TopicAndPartition topicAndPartition = new TopicAndPartition(tmd.topic(), pmd.partitionId());
partitionLeader.put(topicAndPartition, getHostPort(pmd.leader()));
}
}
} catch (Exception e) {
logger.warn("Got exception to get metadata from broker=" + broker, e);
}
}
}
示例4: refreshTopicMetadata
import kafka.javaapi.TopicMetadata; //导入依赖的package包/类
private void refreshTopicMetadata(KafkaPartition partition) {
for (String broker : KafkaWrapper.this.getBrokers()) {
List<TopicMetadata> topicMetadataList = fetchTopicMetadataFromBroker(broker, partition.getTopicName());
if (topicMetadataList != null && !topicMetadataList.isEmpty()) {
TopicMetadata topicMetadata = topicMetadataList.get(0);
for (PartitionMetadata partitionMetadata : topicMetadata.partitionsMetadata()) {
if (partitionMetadata.partitionId() == partition.getId()) {
partition.setLeader(partitionMetadata.leader().id(), partitionMetadata.leader().host(),
partitionMetadata.leader().port());
break;
}
}
break;
}
}
}
示例5: main1
import kafka.javaapi.TopicMetadata; //导入依赖的package包/类
public static void main1(String[] args) throws Exception{
List<String> topics = new ArrayList<String>();
topics.add("applog");
List<TopicMetadata> topicMetaData = getTopicMetaData(topics);
for (TopicMetadata meta : topicMetaData) {
System.out.println(meta.topic());
List<PartitionMetadata> list = meta.partitionsMetadata();
System.out.println(list.size());
for (PartitionMetadata pmeta : list) {
System.out.println("id: " + pmeta.partitionId());
System.out.println("isr: " + pmeta.isr());
System.out.println("leader: " + pmeta.leader());
System.out.println("replicas: " + pmeta.replicas());
}
}
}
示例6: doesTopicExist
import kafka.javaapi.TopicMetadata; //导入依赖的package包/类
public boolean doesTopicExist(String topic) {
log.debug("Does Topic {} exist?", topic);
SimpleConsumer consumer = new SimpleConsumer(host, port, soTimeout, bufferSize, clientId);
List<String> topics = new ArrayList<>();
TopicMetadataRequest request = new TopicMetadataRequest(topics);
TopicMetadataResponse response = consumer.send(request);
List<TopicMetadata> metadata = response.topicsMetadata();
for (TopicMetadata item : metadata) {
if (item.topic().equals(topic)) {
log.debug("Found Topic {}.", topic);
return true;
}
}
log.debug("Did not find Topic {}.", topic);
return false;
}
示例7: getPartitionsForTopic
import kafka.javaapi.TopicMetadata; //导入依赖的package包/类
private List<KafkaPartition> getPartitionsForTopic(TopicMetadata topicMetadata) {
List<KafkaPartition> partitions = Lists.newArrayList();
for (PartitionMetadata partitionMetadata : topicMetadata.partitionsMetadata()) {
if (null == partitionMetadata) {
LOG.error("Ignoring topic with null partition metadata " + topicMetadata.topic());
return Collections.emptyList();
}
if (null == partitionMetadata.leader()) {
LOG.error(
"Ignoring topic with null partition leader " + topicMetadata.topic() + " metatada=" + partitionMetadata);
return Collections.emptyList();
}
partitions.add(new KafkaPartition.Builder().withId(partitionMetadata.partitionId())
.withTopicName(topicMetadata.topic()).withLeaderId(partitionMetadata.leader().id())
.withLeaderHostAndPort(partitionMetadata.leader().host(), partitionMetadata.leader().port()).build());
}
return partitions;
}
示例8: fetchTopicMetadataFromBroker
import kafka.javaapi.TopicMetadata; //导入依赖的package包/类
private List<TopicMetadata> fetchTopicMetadataFromBroker(String broker, List<Pattern> blacklist,
List<Pattern> whitelist) {
List<TopicMetadata> topicMetadataList = fetchTopicMetadataFromBroker(broker);
if (topicMetadataList == null) {
return null;
}
List<TopicMetadata> filteredTopicMetadataList = Lists.newArrayList();
for (TopicMetadata topicMetadata : topicMetadataList) {
if (DatasetFilterUtils.survived(topicMetadata.topic(), blacklist, whitelist)) {
filteredTopicMetadataList.add(topicMetadata);
}
}
return filteredTopicMetadataList;
}
示例9: getPartitionsForTopic
import kafka.javaapi.TopicMetadata; //导入依赖的package包/类
private List<KafkaPartition> getPartitionsForTopic(TopicMetadata topicMetadata) {
List<KafkaPartition> partitions = Lists.newArrayList();
for (PartitionMetadata partitionMetadata : topicMetadata.partitionsMetadata()) {
if (null == partitionMetadata) {
log.error("Ignoring topic with null partition metadata " + topicMetadata.topic());
return Collections.emptyList();
}
if (null == partitionMetadata.leader()) {
log.error("Ignoring topic with null partition leader " + topicMetadata.topic() + " metatada="
+ partitionMetadata);
return Collections.emptyList();
}
partitions.add(new KafkaPartition.Builder().withId(partitionMetadata.partitionId())
.withTopicName(topicMetadata.topic()).withLeaderId(partitionMetadata.leader().id())
.withLeaderHostAndPort(partitionMetadata.leader().host(), partitionMetadata.leader().port()).build());
}
return partitions;
}
示例10: fetchTopicMetadataFromBroker
import kafka.javaapi.TopicMetadata; //导入依赖的package包/类
private List<TopicMetadata> fetchTopicMetadataFromBroker(String broker, String... selectedTopics) {
log.info(String.format("Fetching topic metadata from broker %s", broker));
SimpleConsumer consumer = null;
try {
consumer = getSimpleConsumer(broker);
for (int i = 0; i < this.fetchTopicRetries; i++) {
try {
return consumer.send(new TopicMetadataRequest(Arrays.asList(selectedTopics))).topicsMetadata();
} catch (Exception e) {
log.warn(String.format("Fetching topic metadata from broker %s has failed %d times.", broker, i + 1), e);
try {
Thread.sleep((long) ((i + Math.random()) * 1000));
} catch (InterruptedException e2) {
log.warn("Caught InterruptedException: " + e2);
}
}
}
} finally {
if (consumer != null) {
consumer.close();
}
}
return null;
}
示例11: refreshTopicMetadata
import kafka.javaapi.TopicMetadata; //导入依赖的package包/类
private void refreshTopicMetadata(KafkaPartition partition) {
for (String broker : this.brokers) {
List<TopicMetadata> topicMetadataList = fetchTopicMetadataFromBroker(broker, partition.getTopicName());
if (topicMetadataList != null && !topicMetadataList.isEmpty()) {
TopicMetadata topicMetadata = topicMetadataList.get(0);
for (PartitionMetadata partitionMetadata : topicMetadata.partitionsMetadata()) {
if (partitionMetadata.partitionId() == partition.getId()) {
partition.setLeader(partitionMetadata.leader().id(), partitionMetadata.leader().host(), partitionMetadata
.leader().port());
break;
}
}
break;
}
}
}
示例12: getNumPartitions
import kafka.javaapi.TopicMetadata; //导入依赖的package包/类
public int getNumPartitions(String topic) {
SimpleConsumer consumer = null;
try {
consumer = createConsumer(
mConfig.getKafkaSeedBrokerHost(),
mConfig.getKafkaSeedBrokerPort(),
"partitionLookup");
List<String> topics = new ArrayList<String>();
topics.add(topic);
TopicMetadataRequest request = new TopicMetadataRequest(topics);
TopicMetadataResponse response = consumer.send(request);
if (response.topicsMetadata().size() != 1) {
throw new RuntimeException("Expected one metadata for topic " + topic + " found " +
response.topicsMetadata().size());
}
TopicMetadata topicMetadata = response.topicsMetadata().get(0);
return topicMetadata.partitionsMetadata().size();
} finally {
if (consumer != null) {
consumer.close();
}
}
}
示例13: findTopicMetadata
import kafka.javaapi.TopicMetadata; //导入依赖的package包/类
public TopicMetadata findTopicMetadata(final String topic, int retries) throws Exception {
Operation<TopicMetadata> findTopicOperation = new Operation<TopicMetadata>() {
@Override
public TopicMetadata execute() throws Exception {
List<String> topics = Collections.singletonList(topic);
TopicMetadataRequest req = new TopicMetadataRequest(topics);
TopicMetadataResponse resp = consumer.send(req);
List<TopicMetadata> topicMetadatas = resp.topicsMetadata();
if (topicMetadatas.size() != 1) {
throw new Exception("Expect to find 1 topic " + topic + ", but found " + topicMetadatas.size());
}
return topicMetadatas.get(0);
}
};
return execute(findTopicOperation, retries);
}
示例14: run
import kafka.javaapi.TopicMetadata; //导入依赖的package包/类
public void run() {
try {
while (!exit) {
KafkaTool kafkaTool = new KafkaTool(topic, cluster.getZKConnect());
kafkaTool.connect();
TopicMetadata topicMeta = kafkaTool.findTopicMetadata(topic);
PartitionMetadata partitionMeta = findPartition(topicMeta, partition);
Broker partitionLeader = partitionMeta.leader();
Server kafkaServer = cluster.findKafkaServerByPort(partitionLeader.port());
System.out.println("Shutdown kafka server " + kafkaServer.getPort());
kafkaServer.shutdown();
failureCount++;
Thread.sleep(sleepBeforeRestart);
kafkaServer.start();
kafkaTool.close();
Thread.sleep(10000); //wait to make sure that the kafka server start
}
} catch (Exception e) {
e.printStackTrace();
}
synchronized (this) {
notify();
}
}
示例15: readFromPartition
import kafka.javaapi.TopicMetadata; //导入依赖的package包/类
private void readFromPartition(String consumerName, int partition, int maxRead) throws Exception {
KafkaTool kafkaTool = new KafkaTool(consumerName, cluster.getZKConnect());
kafkaTool.connect();
TopicMetadata topicMetadata = kafkaTool.findTopicMetadata("hello");
PartitionMetadata partitionMetadata = findPartition(topicMetadata.partitionsMetadata(), partition);
KafkaPartitionReader partitionReader =
new KafkaPartitionReader(consumerName, cluster.getZKConnect(), "hello", partitionMetadata);
List<byte[]> messages = partitionReader.fetch(10000, maxRead);
for(int i = 0; i < messages.size(); i++) {
byte[] message = messages.get(i) ;
System.out.println((i + 1) + ". " + new String(message));
}
partitionReader.commit();
partitionReader.close();
kafkaTool.close();
}