Java Code Examples for org.apache.kafka.common.PartitionInfo#topic()

The following examples show how to use org.apache.kafka.common.PartitionInfo#topic() . 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: KafkaClusterManager.java    From doctorkafka with Apache License 2.0 6 votes vote down vote up
public Map<Integer, List<TopicPartition>> getBrokerLeaderPartitions(
    Map<String, List<PartitionInfo>> topicPartitonInfoMap) {
  Map<Integer, List<TopicPartition>> result = new HashMap<>();

  for (String topic : topicPartitonInfoMap.keySet()) {
    List<PartitionInfo> partitionInfoList = topicPartitonInfoMap.get(topic);
    if (partitionInfoList == null) {
      LOG.error("Failed to get partition info for {}", topic);
      continue;
    }

    for (PartitionInfo info : partitionInfoList) {
      Node leaderNode = info.leader();
      if (leaderNode != null) {
        result.putIfAbsent(leaderNode.id(), new ArrayList<>());
        TopicPartition topicPartiton = new TopicPartition(info.topic(), info.partition());
        result.get(leaderNode.id()).add(topicPartiton);
      }
    }
  }
  return result;
}
 
Example 2
Source File: KafkaMessageChannelBinder.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 6 votes vote down vote up
private TopicPartitionOffset[] getTopicPartitionOffsets(
		Collection<PartitionInfo> listenedPartitions,
		ExtendedConsumerProperties<KafkaConsumerProperties> extendedConsumerProperties,
		ConsumerFactory<?, ?> consumerFactory) {

	final TopicPartitionOffset[] TopicPartitionOffsets =
			new TopicPartitionOffset[listenedPartitions.size()];
	int i = 0;
	SeekPosition seekPosition = null;
	Object resetTo = checkReset(extendedConsumerProperties.getExtension().isResetOffsets(),
			consumerFactory.getConfigurationProperties().get(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG));
	if (resetTo != null) {
		seekPosition = "earliest".equals(resetTo) ? SeekPosition.BEGINNING : SeekPosition.END;
	}
	for (PartitionInfo partition : listenedPartitions) {

		TopicPartitionOffsets[i++] = new TopicPartitionOffset(
				partition.topic(), partition.partition(), seekPosition);
	}
	return TopicPartitionOffsets;
}
 
Example 3
Source File: PartitionState.java    From cruise-control with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public PartitionState(PartitionInfo partitionInfo) {
  _topic = partitionInfo.topic();
  _partition = partitionInfo.partition();
  _leader = partitionInfo.leader() == null ? -1 : partitionInfo.leader().id();
  _replicas = Arrays.stream(partitionInfo.replicas()).map(Node::id).collect(Collectors.toList());
  _inSyncReplicas = Arrays.stream(partitionInfo.inSyncReplicas()).map(Node::id).collect(Collectors.toList());
  _outOfSyncReplicas = new HashSet<>(_replicas);
  _outOfSyncReplicas.removeAll(_inSyncReplicas);
  _offlineReplicas = Arrays.stream(partitionInfo.offlineReplicas()).map(Node::id).collect(Collectors.toSet());
}
 
Example 4
Source File: KafkaConsumerEvent.java    From DBus with Apache License 2.0 5 votes vote down vote up
public KafkaConsumerEvent(String topic) {
    super(0l);
    this.topic = topic;
    Properties props = HeartBeatConfigContainer.getInstance().getKafkaConsumerConfig();
    Properties producerProps = HeartBeatConfigContainer.getInstance().getKafkaProducerConfig();
    try {
        if (KafkaUtil.checkSecurity()) {
            props.put(CommonClientConfigs.SECURITY_PROTOCOL_CONFIG, "SASL_PLAINTEXT");
            producerProps.put(CommonClientConfigs.SECURITY_PROTOCOL_CONFIG, "SASL_PLAINTEXT");
        }

        dataConsumer = new KafkaConsumer<>(props);
        assignTopics = new ArrayList<>();
        for (PartitionInfo pif : dataConsumer.partitionsFor(this.topic)) {
            TopicPartition tp = new TopicPartition(pif.topic(), pif.partition());
            assignTopics.add(tp);
        }

        dataConsumer.assign(assignTopics);
        KafkaConsumerContainer.getInstances().putConsumer(this.topic, dataConsumer);
        statProducer = new KafkaProducer<>(producerProps);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    startTime = System.currentTimeMillis();
}
 
Example 5
Source File: KafkaOffsetGetter.java    From Kafka-Insight with Apache License 2.0 5 votes vote down vote up
/**
 * When an object implementing interface <code>Runnable</code> is used
 * to create a thread, starting the thread causes the object's
 * <code>run</code> method to be called in that separately executing
 * thread.
 * <p>
 * The general contract of the method <code>run</code> is that it may
 * take any action whatsoever.
 *
 * @see Thread#run()
 */
@Override
public void run() {
    String group = "kafka-insight-logOffsetListener";
    int sleepTime = 60000;
    KafkaConsumer<Array<Byte>, Array<Byte>> kafkaConsumer = null;

    while (true) {

        try {
            if (null == kafkaConsumer) {
                kafkaConsumer = KafkaUtils.createNewKafkaConsumer(brokersInfo, group);
            }

            Map<String, List<PartitionInfo>> topicPartitionsMap = kafkaConsumer.listTopics();
            for (List<PartitionInfo> partitionInfoList : topicPartitionsMap.values()) {
                for (PartitionInfo partitionInfo : partitionInfoList) {
                    TopicPartition topicPartition = new TopicPartition(partitionInfo.topic(), partitionInfo.partition());
                    Collection<TopicPartition> topicPartitions = Arrays.asList(topicPartition);
                    kafkaConsumer.assign(topicPartitions);
                    kafkaConsumer.seekToEnd(topicPartitions);
                    Long logEndOffset = kafkaConsumer.position(topicPartition);
                    logEndOffsetMap.put(topicPartition, logEndOffset);
                }
            }

            Thread.sleep(sleepTime);

        } catch (Exception e) {
            e.printStackTrace();
            if (null != kafkaConsumer) {
                kafkaConsumer.close();
                kafkaConsumer = null;
            }
        }
    }

}
 
Example 6
Source File: NewApiTopicConsumer.java    From jeesuite-libs with Apache License 2.0 5 votes vote down vote up
/**
 * 按上次记录重置offsets
 */
private void resetCorrectOffsets(ConsumerWorker worker) {	
	
	KafkaConsumer<String, Serializable> consumer = worker.consumer;
	Map<String, List<PartitionInfo>> topicInfos = consumer.listTopics();
	Set<String> topics = topicInfos.keySet();
	
	List<String> expectTopics = new ArrayList<>(topicHandlers.keySet());
	
	List<PartitionInfo> patitions = null;
	
	consumer.poll(200);
	
	for (String topic : topics) {
		if(!expectTopics.contains(topic))continue;
		
		patitions = topicInfos.get(topic);
		for (PartitionInfo partition : patitions) {
			try {						
				//期望的偏移
				long expectOffsets = consumerContext.getLatestProcessedOffsets(topic, partition.partition());
				//
				TopicPartition topicPartition = new TopicPartition(partition.topic(), partition.partition());
				OffsetAndMetadata metadata = consumer.committed(topicPartition);
				
				Set<TopicPartition> assignment = consumer.assignment();
				if(assignment.contains(topicPartition)){
					if(expectOffsets > 0 && expectOffsets < metadata.offset()){								
						consumer.seek(topicPartition, expectOffsets);
						//consumer.seekToBeginning(assignment);
				        logger.info(">>>>>>> seek Topic[{}] partition[{}] from {} to {}",topic, partition.partition(),metadata.offset(),expectOffsets);
					}
				}
			} catch (Exception e) {
				logger.warn("try seek topic["+topic+"] partition["+partition.partition()+"] offsets error");
			}
		}
	}
	consumer.resume(consumer.assignment());
}
 
Example 7
Source File: KafkaSplitManager.java    From presto with Apache License 2.0 4 votes vote down vote up
private static TopicPartition toTopicPartition(PartitionInfo partitionInfo)
{
    return new TopicPartition(partitionInfo.topic(), partitionInfo.partition());
}
 
Example 8
Source File: OutOfSyncReplica.java    From doctorkafka with Apache License 2.0 4 votes vote down vote up
public OutOfSyncReplica(PartitionInfo partitionInfo) {
  this.topicPartition = new TopicPartition(partitionInfo.topic(), partitionInfo.partition());
  this.inSyncBrokers = getInSyncReplicas(partitionInfo);
  this.outOfSyncBrokers = getOutOfSyncReplicas(partitionInfo);
  this.leader = partitionInfo.leader();
}