Java Code Examples for org.apache.kafka.clients.producer.Producer#partitionsFor()

The following examples show how to use org.apache.kafka.clients.producer.Producer#partitionsFor() . 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: FlinkKafkaProducer.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
private static int[] getPartitionsByTopic(String topic, Producer<byte[], byte[]> producer) {
	// the fetched list is immutable, so we're creating a mutable copy in order to sort it
	List<PartitionInfo> partitionsList = new ArrayList<>(producer.partitionsFor(topic));

	// sort the partitions by partition id to make sure the fetched partition list is the same across subtasks
	Collections.sort(partitionsList, new Comparator<PartitionInfo>() {
		@Override
		public int compare(PartitionInfo o1, PartitionInfo o2) {
			return Integer.compare(o1.partition(), o2.partition());
		}
	});

	int[] partitions = new int[partitionsList.size()];
	for (int i = 0; i < partitions.length; i++) {
		partitions[i] = partitionsList.get(i).partition();
	}

	return partitions;
}
 
Example 2
Source File: FlinkKafkaProducer011.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
private static int[] getPartitionsByTopic(String topic, Producer<byte[], byte[]> producer) {
	// the fetched list is immutable, so we're creating a mutable copy in order to sort it
	List<PartitionInfo> partitionsList = new ArrayList<>(producer.partitionsFor(topic));

	// sort the partitions by partition id to make sure the fetched partition list is the same across subtasks
	Collections.sort(partitionsList, new Comparator<PartitionInfo>() {
		@Override
		public int compare(PartitionInfo o1, PartitionInfo o2) {
			return Integer.compare(o1.partition(), o2.partition());
		}
	});

	int[] partitions = new int[partitionsList.size()];
	for (int i = 0; i < partitions.length; i++) {
		partitions[i] = partitionsList.get(i).partition();
	}

	return partitions;
}
 
Example 3
Source File: FlinkKafkaProducer.java    From flink with Apache License 2.0 6 votes vote down vote up
private static int[] getPartitionsByTopic(String topic, Producer<byte[], byte[]> producer) {
	// the fetched list is immutable, so we're creating a mutable copy in order to sort it
	List<PartitionInfo> partitionsList = new ArrayList<>(producer.partitionsFor(topic));

	// sort the partitions by partition id to make sure the fetched partition list is the same across subtasks
	Collections.sort(partitionsList, new Comparator<PartitionInfo>() {
		@Override
		public int compare(PartitionInfo o1, PartitionInfo o2) {
			return Integer.compare(o1.partition(), o2.partition());
		}
	});

	int[] partitions = new int[partitionsList.size()];
	for (int i = 0; i < partitions.length; i++) {
		partitions[i] = partitionsList.get(i).partition();
	}

	return partitions;
}
 
Example 4
Source File: FlinkKafkaProducer011.java    From flink with Apache License 2.0 6 votes vote down vote up
private static int[] getPartitionsByTopic(String topic, Producer<byte[], byte[]> producer) {
	// the fetched list is immutable, so we're creating a mutable copy in order to sort it
	List<PartitionInfo> partitionsList = new ArrayList<>(producer.partitionsFor(topic));

	// sort the partitions by partition id to make sure the fetched partition list is the same across subtasks
	Collections.sort(partitionsList, new Comparator<PartitionInfo>() {
		@Override
		public int compare(PartitionInfo o1, PartitionInfo o2) {
			return Integer.compare(o1.partition(), o2.partition());
		}
	});

	int[] partitions = new int[partitionsList.size()];
	for (int i = 0; i < partitions.length; i++) {
		partitions[i] = partitionsList.get(i).partition();
	}

	return partitions;
}
 
Example 5
Source File: FlinkKafkaProducer.java    From flink with Apache License 2.0 6 votes vote down vote up
protected static int[] getPartitionsByTopic(String topic, Producer<byte[], byte[]> producer) {
	// the fetched list is immutable, so we're creating a mutable copy in order to sort it
	List<PartitionInfo> partitionsList = new ArrayList<>(producer.partitionsFor(topic));

	// sort the partitions by partition id to make sure the fetched partition list is the same across subtasks
	Collections.sort(partitionsList, new Comparator<PartitionInfo>() {
		@Override
		public int compare(PartitionInfo o1, PartitionInfo o2) {
			return Integer.compare(o1.partition(), o2.partition());
		}
	});

	int[] partitions = new int[partitionsList.size()];
	for (int i = 0; i < partitions.length; i++) {
		partitions[i] = partitionsList.get(i).partition();
	}

	return partitions;
}
 
Example 6
Source File: FlinkKafkaProducer011.java    From flink with Apache License 2.0 6 votes vote down vote up
private static int[] getPartitionsByTopic(String topic, Producer<byte[], byte[]> producer) {
	// the fetched list is immutable, so we're creating a mutable copy in order to sort it
	List<PartitionInfo> partitionsList = new ArrayList<>(producer.partitionsFor(topic));

	// sort the partitions by partition id to make sure the fetched partition list is the same across subtasks
	Collections.sort(partitionsList, new Comparator<PartitionInfo>() {
		@Override
		public int compare(PartitionInfo o1, PartitionInfo o2) {
			return Integer.compare(o1.partition(), o2.partition());
		}
	});

	int[] partitions = new int[partitionsList.size()];
	for (int i = 0; i < partitions.length; i++) {
		partitions[i] = partitionsList.get(i).partition();
	}

	return partitions;
}
 
Example 7
Source File: KafkaValidationUtil09.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@Override
public void createTopicIfNotExists(String topic, Map<String, Object> kafkaClientConfigs, String metadataBrokerList) throws StageException {
  Producer<String, String> kafkaProducer = createProducerTopicMetadataClient(
      metadataBrokerList,
      kafkaClientConfigs
  );
  kafkaProducer.partitionsFor(topic);
}
 
Example 8
Source File: MapRStreamsValidationUtil09.java    From datacollector with Apache License 2.0 5 votes vote down vote up
/**
 * Should be called only by MapR Streams Producer. It creates a topic using Producer.
 * @param topic
 * @param kafkaClientConfigs
 * @param metadataBrokerList
 * @throws StageException
 */
@Override
public void createTopicIfNotExists(String topic, Map<String, Object> kafkaClientConfigs, String metadataBrokerList)
    throws StageException {
  // Stream topic can be created through Producer if Stream Path exists already
  Producer<String, String> kafkaProducer = createProducerTopicMetadataClient(kafkaClientConfigs);
  kafkaProducer.partitionsFor(topic);
}
 
Example 9
Source File: MapRStreamsValidationUtil09.java    From datacollector with Apache License 2.0 4 votes vote down vote up
@Override
public boolean validateTopicExistence(
  Stage.Context context,
  String groupName,
  String configName,
  List<HostAndPort> kafkaBrokers,
  String metadataBrokerList,
  String topic,
  Map<String, Object> kafkaClientConfigs,
  List<Stage.ConfigIssue> issues,
  boolean producer
) {
  boolean valid = true;
  if(topic == null || topic.isEmpty()) {
    issues.add(context.createConfigIssue(groupName, configName, KafkaErrors.KAFKA_05));
    valid = false;
  } else {
    List<PartitionInfo> partitionInfos;
    try {
      if (producer) {
        Producer<String, String> kafkaProducer = createProducerTopicMetadataClient(kafkaClientConfigs);
        partitionInfos = kafkaProducer.partitionsFor(topic);
      } else {
        Consumer<String, String> kafkaConsumer = createTopicMetadataClient();
        partitionInfos = kafkaConsumer.partitionsFor(topic);
      }
      if (null == partitionInfos || partitionInfos.isEmpty()) {
        issues.add(
            context.createConfigIssue(
                groupName,
                KAFKA_CONFIG_BEAN_PREFIX + "topic",
                MapRStreamsErrors.MAPRSTREAMS_02,
                topic
            )
        );
        valid = false;
      }
    } catch (KafkaException e) {
      LOG.error(MapRStreamsErrors.MAPRSTREAMS_01.getMessage(), topic, e.toString(), e);
      issues.add(
          context.createConfigIssue(
              groupName,
              configName,
              MapRStreamsErrors.MAPRSTREAMS_01,
              topic,
              e.getMessage()
          )
      );
      valid = false;
    }
  }
  return valid;
}