org.apache.kafka.clients.admin.DescribeTopicsOptions Java Examples

The following examples show how to use org.apache.kafka.clients.admin.DescribeTopicsOptions. 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: KafkaAdmin.java    From feeyo-redisproxy with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public Map<String, TopicDescription> getTopicAndDescriptions() throws Exception {

		try {
			// 查询topic
			ListTopicsOptions lto = new ListTopicsOptions();
			lto.timeoutMs(10 * 1000);
			ListTopicsResult ltr = adminClient.listTopics(lto);
			
			// 查询topic配置信息
			DescribeTopicsOptions dto = new DescribeTopicsOptions();
			dto.timeoutMs(15 * 1000);
			DescribeTopicsResult dtr = adminClient.describeTopics(ltr.names().get(), dto);
			return dtr.all().get();
			
		} catch (Exception e) {
			throw e;
		}
	}
 
Example #2
Source File: PartitionMonitor.java    From MirrorTool-for-Kafka-Connect with Apache License 2.0 5 votes vote down vote up
private synchronized Set<LeaderTopicPartition> retrieveLeaderTopicPartitions(int requestTimeoutMs)
    throws InterruptedException, ExecutionException, TimeoutException {
  long startWait = System.currentTimeMillis();

  ListTopicsOptions listTopicsOptions = new ListTopicsOptions().listInternal(false)
      .timeoutMs((int) (requestTimeoutMs - (System.currentTimeMillis() - startWait)));
  Set<String> retrievedTopicSet = partitionMonitorClient.listTopics(listTopicsOptions).names()
      .get(requestTimeoutMs - (System.currentTimeMillis() - startWait), TimeUnit.MILLISECONDS);
  logger.debug("Server topic list: {}", retrievedTopicSet);
  Set<String> matchedTopicSet = retrievedTopicSet.stream().filter(topic -> matchedTopicFilter(topic))
      .collect(Collectors.toSet());
  if (matchedTopicSet.size() > 0) {
    logger.debug("Matched topic list: {}", matchedTopicSet);
  } else {
    logger.warn("Provided pattern {} does currently not match any topic." +
                 " Thus connector won't spawn any task until partition monitor recognizes matching topic.", this.topicWhitelistPattern.toString());

  }

  DescribeTopicsOptions describeTopicsOptions = new DescribeTopicsOptions()
      .timeoutMs((int) (requestTimeoutMs - (System.currentTimeMillis() - startWait)));
  Map<String, TopicDescription> retrievedTopicDescriptions = partitionMonitorClient
      .describeTopics(matchedTopicSet, describeTopicsOptions).all()
      .get(requestTimeoutMs - (System.currentTimeMillis() - startWait), TimeUnit.MILLISECONDS);
  return retrievedTopicDescriptions.values().stream()
      .map(topicDescription -> topicDescription.partitions().stream()
          .map(partitionInfo -> new LeaderTopicPartition(partitionInfo.leader().id(), topicDescription.name(),
              partitionInfo.partition())))
      .flatMap(Function.identity()).collect(Collectors.toSet());
}
 
Example #3
Source File: TopicEnsure.java    From common-docker with Apache License 2.0 5 votes vote down vote up
public boolean validateTopic(TopicSpec spec, int timeOut) throws Exception {
  // Describe topic.
  DescribeTopicsResult topicDescribeResult = adminClient.describeTopics(
      Collections.singletonList(spec.name()), new DescribeTopicsOptions().timeoutMs(timeOut)
  );
  TopicDescription topic = topicDescribeResult.all().get().get(spec.name());

  // Get topic config.
  ConfigResource configResource = new ConfigResource(ConfigResource.Type.TOPIC, spec.name());
  DescribeConfigsResult configResult = adminClient.describeConfigs(
      Collections.singletonList(configResource)
  );
  Map<ConfigResource, Config> resultMap = configResult.all().get();
  Config config = resultMap.get(configResource);

  // Create actual TopicSpec.
  Map<String, String> actualConfig = new HashMap<>();
  for (Map.Entry<String, String> entry : spec.config().entrySet()) {
    ConfigEntry actualConfigEntry = config.get(entry.getKey());
    if (actualConfigEntry != null) {
      actualConfig.put(entry.getKey(), actualConfigEntry.value());
    }
  }

  TopicSpec actualSpec = new TopicSpec(
      topic.name(), topic.partitions().size(),
      topic.partitions().get(0).replicas().size(), actualConfig
  );

  boolean isTopicValid = actualSpec.equals(spec);
  if (!isTopicValid) {
    System.err.printf(
        "Invalid topic [ %s ] ! Expected %s but got %s\n", spec.name(), spec, actualSpec
    );
  }

  return isTopicValid;
}
 
Example #4
Source File: TopicEnsure.java    From common-docker with Apache License 2.0 5 votes vote down vote up
public boolean topicExists(TopicSpec spec, Integer timeOut) throws Exception {
  try {
    DescribeTopicsResult topicDescribeResult = adminClient.describeTopics(
        Collections.singletonList(spec.name()), new DescribeTopicsOptions().timeoutMs(timeOut)
    );
    topicDescribeResult.all().get().get(spec.name());
  } catch (ExecutionException e) {
    if (e.getCause() instanceof UnknownTopicOrPartitionException) {
      return false;
    } else {
      throw e;
    }
  }
  return true;
}
 
Example #5
Source File: KafkaAdmin.java    From feeyo-redisproxy with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * 获取指定topic的配置信息
 */
public TopicDescription getDescriptionByTopicName(String topic) throws Exception {
	
	List<String> topics = new ArrayList<String>();
	topics.add(topic);

	DescribeTopicsOptions dto = new DescribeTopicsOptions();
	dto.timeoutMs(5 * 1000);
	DescribeTopicsResult dtr = adminClient.describeTopics(topics, dto);
	return dtr.all().get().get(topic);

}