org.apache.kafka.common.utils.Utils.sorted()方法的使用及代码示例

x33g5p2x  于2022-02-01 转载在 其他  
字(3.7k)|赞(0)|评价(0)|浏览(135)

本文整理了Java中org.apache.kafka.common.utils.Utils.sorted()方法的一些代码示例,展示了Utils.sorted()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Utils.sorted()方法的具体详情如下:
包路径:org.apache.kafka.common.utils.Utils
类名称:Utils
方法名:sorted

Utils.sorted介绍

[英]Get a sorted list representation of a collection.
[中]获取集合的排序列表表示形式。

代码示例

代码示例来源:origin: apache/kafka

@Override
public Map<String, List<TopicPartition>> assign(Map<String, Integer> partitionsPerTopic,
                        Map<String, Subscription> subscriptions) {
  Map<String, List<TopicPartition>> assignment = new HashMap<>();
  for (String memberId : subscriptions.keySet())
    assignment.put(memberId, new ArrayList<>());
  CircularIterator<String> assigner = new CircularIterator<>(Utils.sorted(subscriptions.keySet()));
  for (TopicPartition partition : allPartitionsSorted(partitionsPerTopic, subscriptions)) {
    final String topic = partition.topic();
    while (!subscriptions.get(assigner.peek()).topics().contains(topic))
      assigner.next();
    assignment.get(assigner.next()).add(partition);
  }
  return assignment;
}

代码示例来源:origin: apache/kafka

assert size == assignments.size();
List<String> consumers = Utils.sorted(assignments.keySet());

代码示例来源:origin: apache/kafka

List<String> sub = Utils.sorted(getRandomSublist(topics));
subscriptions.put(getConsumerName(i, maxNumConsumers), new Subscription(sub));
List<String> sub = Utils.sorted(getRandomSublist(topics));
String consumer = getConsumerName(i, maxNumConsumers);
subscriptions.put(consumer,

代码示例来源:origin: org.apache.kafka/kafka-streams

private Map<Integer, Set<String>> makeNodeGroups() {
  final Map<Integer, Set<String>> nodeGroups = new LinkedHashMap<>();
  final Map<String, Set<String>> rootToNodeGroup = new HashMap<>();
  int nodeGroupId = 0;
  // Go through source nodes first. This makes the group id assignment easy to predict in tests
  final Set<String> allSourceNodes = new HashSet<>(nodeToSourceTopics.keySet());
  allSourceNodes.addAll(nodeToSourcePatterns.keySet());
  for (final String nodeName : Utils.sorted(allSourceNodes)) {
    nodeGroupId = putNodeGroupName(nodeName, nodeGroupId, nodeGroups, rootToNodeGroup);
  }
  // Go through non-source nodes
  for (final String nodeName : Utils.sorted(nodeFactories.keySet())) {
    if (!nodeToSourceTopics.containsKey(nodeName)) {
      nodeGroupId = putNodeGroupName(nodeName, nodeGroupId, nodeGroups, rootToNodeGroup);
    }
  }
  return nodeGroups;
}

代码示例来源:origin: com.cerner.common.kafka/common-kafka

@Override
public Map<String, List<TopicPartition>> assign(Map<String, Integer> partitionsPerTopic,
                        Map<String, List<String>> subscriptions) {
  List<String> consumers = Utils.sorted(subscriptions.keySet());
  // Invert topics-per-consumer map to consumers-per-topic.
  Map<String, List<String>> consumersPerTopic = consumersPerTopic(subscriptions);
  // Map for tracking the total number of partitions assigned to each consumer
  Map<String, Integer> consumerAssignmentCounts = new HashMap<>();
  for (String consumer : consumers) {
    consumerAssignmentCounts.put(consumer, 0);
  }
  Map<String, List<TopicPartition>> assignment = new HashMap<>();
  for (String memberId : subscriptions.keySet()) {
    assignment.put(memberId, new ArrayList<>());
  }
  Comparator<String> consumerComparator = new ConsumerFairness(consumerAssignmentCounts);
  for (TopicPartition partition : allPartitionsSorted(partitionsPerTopic, subscriptions, consumersPerTopic)) {
    // Find the most appropriate consumer for the partition.
    String assignedConsumer = null;
    for (String consumer : consumersPerTopic.get(partition.topic())) {
      if (assignedConsumer == null || consumerComparator.compare(consumer, assignedConsumer) < 0) {
        assignedConsumer = consumer;
      }
    }
    consumerAssignmentCounts.put(assignedConsumer, consumerAssignmentCounts.get(assignedConsumer) + 1);
    assignment.get(assignedConsumer).add(partition);
  }
  return assignment;
}

相关文章