use of org.apache.kafka.clients.producer.internals.DefaultPartitioner in project apache-kafka-on-k8s by banzaicloud.
the class WindowedStreamPartitionerTest method testCopartitioning.
@Test
public void testCopartitioning() {
final Random rand = new Random();
final DefaultPartitioner defaultPartitioner = new DefaultPartitioner();
final WindowedSerializer<Integer> timeWindowedSerializer = new TimeWindowedSerializer<>(intSerializer);
final WindowedStreamPartitioner<Integer, String> streamPartitioner = new WindowedStreamPartitioner<>(topicName, timeWindowedSerializer);
for (int k = 0; k < 10; k++) {
Integer key = rand.nextInt();
byte[] keyBytes = intSerializer.serialize(topicName, key);
String value = key.toString();
byte[] valueBytes = stringSerializer.serialize(topicName, value);
Integer expected = defaultPartitioner.partition("topic", key, keyBytes, value, valueBytes, cluster);
for (int w = 1; w < 10; w++) {
TimeWindow window = new TimeWindow(10 * w, 20 * w);
Windowed<Integer> windowedKey = new Windowed<>(key, window);
Integer actual = streamPartitioner.partition(windowedKey, value, infos.size());
assertEquals(expected, actual);
}
}
defaultPartitioner.close();
}
use of org.apache.kafka.clients.producer.internals.DefaultPartitioner in project apache-kafka-on-k8s by banzaicloud.
the class MockProducerTest method testPartitioner.
@Test
public void testPartitioner() throws Exception {
PartitionInfo partitionInfo0 = new PartitionInfo(topic, 0, null, null, null);
PartitionInfo partitionInfo1 = new PartitionInfo(topic, 1, null, null, null);
Cluster cluster = new Cluster(null, new ArrayList<Node>(0), asList(partitionInfo0, partitionInfo1), Collections.<String>emptySet(), Collections.<String>emptySet());
MockProducer<String, String> producer = new MockProducer<>(cluster, true, new DefaultPartitioner(), new StringSerializer(), new StringSerializer());
ProducerRecord<String, String> record = new ProducerRecord<>(topic, "key", "value");
Future<RecordMetadata> metadata = producer.send(record);
assertEquals("Partition should be correct", 1, metadata.get().partition());
producer.clear();
assertEquals("Clear should erase our history", 0, producer.history().size());
producer.close();
}
use of org.apache.kafka.clients.producer.internals.DefaultPartitioner in project kafka by apache.
the class MockClientSupplier method getProducer.
@Override
public Producer<byte[], byte[]> getProducer(final Map<String, Object> config) {
if (applicationId != null) {
assertThat((String) config.get(ProducerConfig.TRANSACTIONAL_ID_CONFIG), startsWith(applicationId + "-"));
} else {
assertFalse(config.containsKey(ProducerConfig.TRANSACTIONAL_ID_CONFIG));
}
final MockProducer<byte[], byte[]> producer = new MockProducer<>(cluster, true, new DefaultPartitioner(), BYTE_ARRAY_SERIALIZER, BYTE_ARRAY_SERIALIZER);
producers.add(producer);
return producer;
}
Aggregations