Java Code Examples for kafka.javaapi.TopicMetadata#partitionsMetadata()
The following examples show how to use
kafka.javaapi.TopicMetadata#partitionsMetadata() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: KafkaWrapper.java From incubator-gobblin with Apache License 2.0 | 6 votes |
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; }
Example 2
Source File: KafkaWrapper.java From incubator-gobblin with Apache License 2.0 | 6 votes |
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; } } }
Example 3
Source File: Kafka08ConsumerClient.java From incubator-gobblin with Apache License 2.0 | 6 votes |
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; }
Example 4
Source File: Kafka08ConsumerClient.java From incubator-gobblin with Apache License 2.0 | 6 votes |
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; } } }
Example 5
Source File: KafkaMetadataUtil.java From attic-apex-malhar with Apache License 2.0 | 5 votes |
/** * @param brokerList brokers in same cluster * @param topic * @return Get the partition metadata list for the specific topic via the brokerList <br> * null if topic is not found */ public static List<PartitionMetadata> getPartitionsForTopic(Set<String> brokerList, String topic) { TopicMetadata tmd = getTopicMetadata(brokerList, topic); if (tmd == null) { return null; } return tmd.partitionsMetadata(); }
Example 6
Source File: AbstractExactlyOnceKafkaOutputOperator.java From attic-apex-malhar with Apache License 2.0 | 5 votes |
private void initializeLastProcessingOffset() { // read last received kafka message TopicMetadata tm = KafkaMetadataUtil.getTopicMetadata(Sets.newHashSet((String)getConfigProperties().get(KafkaMetadataUtil.PRODUCER_PROP_BROKERLIST)), this.getTopic()); if (tm == null) { throw new RuntimeException("Failed to retrieve topic metadata"); } partitionNum = tm.partitionsMetadata().size(); lastMsgs = new HashMap<Integer, Pair<byte[],byte[]>>(partitionNum); for (PartitionMetadata pm : tm.partitionsMetadata()) { String leadBroker = pm.leader().host(); int port = pm.leader().port(); String clientName = this.getClass().getName().replace('$', '.') + "_Client_" + tm.topic() + "_" + pm.partitionId(); SimpleConsumer consumer = new SimpleConsumer(leadBroker, port, 100000, 64 * 1024, clientName); long readOffset = KafkaMetadataUtil.getLastOffset(consumer, tm.topic(), pm.partitionId(), kafka.api.OffsetRequest.LatestTime(), clientName); FetchRequest req = new FetchRequestBuilder().clientId(clientName).addFetch(tm.topic(), pm.partitionId(), readOffset - 1, 100000).build(); FetchResponse fetchResponse = consumer.fetch(req); for (MessageAndOffset messageAndOffset : fetchResponse.messageSet(tm.topic(), pm.partitionId())) { Message m = messageAndOffset.message(); ByteBuffer payload = m.payload(); ByteBuffer key = m.key(); byte[] valueBytes = new byte[payload.limit()]; byte[] keyBytes = new byte[key.limit()]; payload.get(valueBytes); key.get(keyBytes); lastMsgs.put(pm.partitionId(), new Pair<byte[], byte[]>(keyBytes, valueBytes)); } } }
Example 7
Source File: LegacyKafkaClient.java From secor with Apache License 2.0 | 5 votes |
private HostAndPort findLeader(TopicPartition topicPartition) { SimpleConsumer consumer = null; try { LOG.debug("looking up leader for topic {} partition {}", topicPartition.getTopic(), topicPartition.getPartition()); consumer = createConsumer( mConfig.getKafkaSeedBrokerHost(), mConfig.getKafkaSeedBrokerPort(), "leaderLookup"); List<String> topics = new ArrayList<String>(); topics.add(topicPartition.getTopic()); TopicMetadataRequest request = new TopicMetadataRequest(topics); TopicMetadataResponse response = consumer.send(request); List<TopicMetadata> metaData = response.topicsMetadata(); for (TopicMetadata item : metaData) { for (PartitionMetadata part : item.partitionsMetadata()) { if (part.partitionId() == topicPartition.getPartition()) { return HostAndPort.fromParts(part.leader().host(), part.leader().port()); } } } } finally { if (consumer != null) { consumer.close(); } } return null; }
Example 8
Source File: ScribenginAM.java From Scribengin with GNU Affero General Public License v3.0 | 5 votes |
private void getMetaData(String topic) { LOG.info("inside getMetaData"); //xxx LOG.info("seedBrokerList" + this.brokerList); //xxx for (HostPort seed: brokerList) { SimpleConsumer consumer = new SimpleConsumer( seed.getHost(), seed.getPort(), 10000, // timeout 64*1024, // bufferSize "metaLookup" // clientId ); List <String> topicList = Collections.singletonList(topic); TopicMetadataRequest req = new TopicMetadataRequest(topicList); kafka.javaapi.TopicMetadataResponse resp = consumer.send(req); List<TopicMetadata> metaDataList = resp.topicsMetadata(); LOG.info("metaDataList: " + metaDataList); //xxxx for (TopicMetadata m: metaDataList) { LOG.info("inside the metadatalist loop"); //xxx LOG.info("m partitionsMetadata: " + m.partitionsMetadata()); //xxx for (PartitionMetadata part : m.partitionsMetadata()) { LOG.info("inside the partitionmetadata loop"); //xxx storeMetadata(topic, part); } } } }
Example 9
Source File: KafkaSource.java From Scribengin with GNU Affero General Public License v3.0 | 5 votes |
void init(StorageDescriptor descriptor) throws Exception { this.descriptor = descriptor; KafkaTool kafkaTool = new KafkaTool(descriptor.attribute("name"), descriptor.attribute("zk.connect")); kafkaTool.connect(); TopicMetadata topicMetdadata = kafkaTool.findTopicMetadata(descriptor.attribute("topic")); List<PartitionMetadata> partitionMetadatas = topicMetdadata.partitionsMetadata(); for(int i = 0; i < partitionMetadatas.size(); i++) { PartitionMetadata partitionMetadata = partitionMetadatas.get(i); KafkaSourceStream sourceStream = new KafkaSourceStream(descriptor, partitionMetadata); sourceStreams.put(sourceStream.getId(), sourceStream); } kafkaTool.close(); }
Example 10
Source File: KafkaTool.java From Scribengin with GNU Affero General Public License v3.0 | 5 votes |
public PartitionMetadata findPartitionMetadata(String topic, int partition) throws Exception { TopicMetadata topicMetadata = findTopicMetadata(topic); for (PartitionMetadata sel : topicMetadata.partitionsMetadata()) { if (sel.partitionId() == partition) return sel; } return null; }
Example 11
Source File: KafkaClusterTool.java From Scribengin with GNU Affero General Public License v3.0 | 5 votes |
PartitionMetadata findPartition(TopicMetadata topicMetadata, int partition) { for (PartitionMetadata sel : topicMetadata.partitionsMetadata()) { if (sel.partitionId() == partition) return sel; } throw new RuntimeException("Cannot find the partition " + partition); }
Example 12
Source File: KafkaMessageSendTool.java From Scribengin with GNU Affero General Public License v3.0 | 5 votes |
public void doSend() throws Exception { System.out.println("KafkaMessageSendTool: Start sending the message to kafka"); runDuration.start(); ExecutorService writerService = Executors.newFixedThreadPool(topicConfig.numberOfPartition); KafkaTool kafkaTool = new KafkaTool("KafkaTool", topicConfig.zkConnect); kafkaTool.connect(); String kafkaConnects = kafkaTool.getKafkaBrokerList(); //TODO: add option to delete topic if it exists //kafkaTool.deleteTopic(topicConfig.topic); if(!kafkaTool.topicExits(topicConfig.topic)) { kafkaTool.createTopic(topicConfig.topic, topicConfig.replication, topicConfig.numberOfPartition); } TopicMetadata topicMetadata = kafkaTool.findTopicMetadata(topicConfig.topic); List<PartitionMetadata> partitionMetadataHolder = topicMetadata.partitionsMetadata(); for (PartitionMetadata sel : partitionMetadataHolder) { PartitionMessageWriter writer = new PartitionMessageWriter(sel, kafkaConnects); writers.put(sel.partitionId(), writer); writerService.submit(writer); } writerService.shutdown(); writerService.awaitTermination(topicConfig.producerConfig.maxDuration, TimeUnit.MILLISECONDS); if (!writerService.isTerminated()) { writerService.shutdownNow(); } kafkaTool.close(); runDuration.stop(); }
Example 13
Source File: AckKafkaWriterTestRunner.java From Scribengin with GNU Affero General Public License v3.0 | 5 votes |
PartitionMetadata findPartition(TopicMetadata topicMetadata, int partion) { for (PartitionMetadata sel : topicMetadata.partitionsMetadata()) { if (sel.partitionId() == partition) return sel; } throw new RuntimeException("Cannot find the partition " + partition); }
Example 14
Source File: Kafka08PartitionDiscoverer.java From Flink-CEPplus with Apache License 2.0 | 4 votes |
/** * Send request to Kafka to get partitions for topics. * * @param topics The name of the topics. */ public List<KafkaTopicPartitionLeader> getPartitionLeadersForTopics(List<String> topics) { List<KafkaTopicPartitionLeader> partitions = new LinkedList<>(); retryLoop: for (int retry = 0; retry < numRetries; retry++) { brokersLoop: for (int arrIdx = 0; arrIdx < seedBrokerAddresses.length; arrIdx++) { LOG.info("Trying to get topic metadata from broker {} in try {}/{}", seedBrokerAddresses[currentContactSeedBrokerIndex], retry, numRetries); try { // clear in case we have an incomplete list from previous tries partitions.clear(); for (TopicMetadata item : consumer.send(new TopicMetadataRequest(topics)).topicsMetadata()) { if (item.errorCode() != ErrorMapping.NoError()) { // warn and try more brokers LOG.warn("Error while getting metadata from broker {} to find partitions for {}. Error: {}.", seedBrokerAddresses[currentContactSeedBrokerIndex], topics.toString(), ErrorMapping.exceptionFor(item.errorCode()).getMessage()); useNextAddressAsNewContactSeedBroker(); continue brokersLoop; } if (!topics.contains(item.topic())) { LOG.warn("Received metadata from topic " + item.topic() + " even though it was not requested. Skipping ..."); useNextAddressAsNewContactSeedBroker(); continue brokersLoop; } for (PartitionMetadata part : item.partitionsMetadata()) { Node leader = brokerToNode(part.leader()); KafkaTopicPartition ktp = new KafkaTopicPartition(item.topic(), part.partitionId()); KafkaTopicPartitionLeader pInfo = new KafkaTopicPartitionLeader(ktp, leader); partitions.add(pInfo); } } break retryLoop; // leave the loop through the brokers } catch (Exception e) { //validates seed brokers in case of a ClosedChannelException validateSeedBrokers(seedBrokerAddresses, e); LOG.warn("Error communicating with broker {} to find partitions for {}. {} Message: {}", seedBrokerAddresses[currentContactSeedBrokerIndex], topics, e.getClass().getName(), e.getMessage()); LOG.debug("Detailed trace", e); // we sleep a bit. Retrying immediately doesn't make sense in cases where Kafka is reorganizing the leader metadata try { Thread.sleep(500); } catch (InterruptedException e1) { // sleep shorter. } useNextAddressAsNewContactSeedBroker(); } } // brokers loop } // retries loop return partitions; }
Example 15
Source File: Kafka08PartitionDiscoverer.java From flink with Apache License 2.0 | 4 votes |
/** * Send request to Kafka to get partitions for topics. * * @param topics The name of the topics. */ public List<KafkaTopicPartitionLeader> getPartitionLeadersForTopics(List<String> topics) { List<KafkaTopicPartitionLeader> partitions = new LinkedList<>(); retryLoop: for (int retry = 0; retry < numRetries; retry++) { brokersLoop: for (int arrIdx = 0; arrIdx < seedBrokerAddresses.length; arrIdx++) { LOG.info("Trying to get topic metadata from broker {} in try {}/{}", seedBrokerAddresses[currentContactSeedBrokerIndex], retry, numRetries); try { // clear in case we have an incomplete list from previous tries partitions.clear(); for (TopicMetadata item : consumer.send(new TopicMetadataRequest(topics)).topicsMetadata()) { if (item.errorCode() != ErrorMapping.NoError()) { // warn and try more brokers LOG.warn("Error while getting metadata from broker {} to find partitions for {}. Error: {}.", seedBrokerAddresses[currentContactSeedBrokerIndex], topics.toString(), ErrorMapping.exceptionFor(item.errorCode()).getMessage()); useNextAddressAsNewContactSeedBroker(); continue brokersLoop; } if (!topics.contains(item.topic())) { LOG.warn("Received metadata from topic " + item.topic() + " even though it was not requested. Skipping ..."); useNextAddressAsNewContactSeedBroker(); continue brokersLoop; } for (PartitionMetadata part : item.partitionsMetadata()) { Node leader = brokerToNode(part.leader()); KafkaTopicPartition ktp = new KafkaTopicPartition(item.topic(), part.partitionId()); KafkaTopicPartitionLeader pInfo = new KafkaTopicPartitionLeader(ktp, leader); partitions.add(pInfo); } } break retryLoop; // leave the loop through the brokers } catch (Exception e) { //validates seed brokers in case of a ClosedChannelException validateSeedBrokers(seedBrokerAddresses, e); LOG.warn("Error communicating with broker {} to find partitions for {}. {} Message: {}", seedBrokerAddresses[currentContactSeedBrokerIndex], topics, e.getClass().getName(), e.getMessage()); LOG.debug("Detailed trace", e); // we sleep a bit. Retrying immediately doesn't make sense in cases where Kafka is reorganizing the leader metadata try { Thread.sleep(500); } catch (InterruptedException e1) { // sleep shorter. } useNextAddressAsNewContactSeedBroker(); } } // brokers loop } // retries loop return partitions; }
Example 16
Source File: OffsetMonitor.java From uReplicator with Apache License 2.0 | 4 votes |
private void updateTopicList() { logger.info("Update topicList"); topicList.clear(); partitionLeader.clear(); // update topicList topicList = helixMirrorMakerManager.getTopicLists(); logger.debug("TopicList: {}", topicList); Set<String> topicSet = new HashSet<>(topicList); // update partitionLeader for (String broker : srcBrokerList) { try { SimpleConsumer consumer = getSimpleConsumer(broker); TopicMetadataRequest req = new TopicMetadataRequest(topicList); 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()); if (topicSet.contains(tmd.topic())) { partitionLeader.put(topicAndPartition, pmd.leader()); } } } Iterator<Entry<TopicAndPartition, TopicPartitionLag>> iter = noProgressMap.entrySet() .iterator(); while (iter.hasNext()) { TopicAndPartition tp = iter.next().getKey(); if (!topicSet.contains(tp.topic())) { iter.remove(); logger.info("Remove non exist topic {} from noProgressMap", tp); } } break; } catch (Exception e) { logger.warn("Got exception to get metadata from broker=" + broker, e); } } logger.debug("partitionLeader: {}", partitionLeader); }