Search in sources :

Example 96 with ProducerRecord

use of org.apache.kafka.clients.producer.ProducerRecord in project hazelcast by hazelcast.

the class WriteKafkaPTest method testWriteToSpecificPartitions.

@Test
public void testWriteToSpecificPartitions() {
    String localTopic = topic;
    Pipeline p = Pipeline.create();
    p.readFrom(Sources.map(sourceIMap)).writeTo(KafkaSinks.kafka(properties, e -> new ProducerRecord<>(localTopic, e.getKey(), e.getKey(), e.getValue())));
    instance().getJet().newJob(p).join();
    kafkaTestSupport.assertTopicContentsEventually(topic, sourceIMap, true);
}
Also used : AbstractProcessor(com.hazelcast.jet.core.AbstractProcessor) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) Traverser(com.hazelcast.jet.Traverser) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) BeforeClass(org.junit.BeforeClass) QuickTest(com.hazelcast.test.annotation.QuickTest) Processor(com.hazelcast.jet.core.Processor) TestOutbox(com.hazelcast.jet.core.test.TestOutbox) ConsumerRecords(org.apache.kafka.clients.consumer.ConsumerRecords) TestProcessorContext(com.hazelcast.jet.core.test.TestProcessorContext) ArrayList(java.util.ArrayList) KafkaProducer(org.apache.kafka.clients.producer.KafkaProducer) Util.entry(com.hazelcast.jet.Util.entry) Duration(java.time.Duration) IntegerSerializer(org.apache.kafka.common.serialization.IntegerSerializer) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) Collections.singletonMap(java.util.Collections.singletonMap) KafkaSinks(com.hazelcast.jet.kafka.KafkaSinks) Job(com.hazelcast.jet.Job) Before(org.junit.Before) SimpleTestInClusterSupport(com.hazelcast.jet.SimpleTestInClusterSupport) TestInbox(com.hazelcast.jet.core.test.TestInbox) AfterClass(org.junit.AfterClass) Properties(java.util.Properties) Pipeline(com.hazelcast.jet.pipeline.Pipeline) ProcessorMetaSupplier(com.hazelcast.jet.core.ProcessorMetaSupplier) JobConfig(com.hazelcast.jet.config.JobConfig) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) Traversers(com.hazelcast.jet.Traversers) Category(org.junit.experimental.categories.Category) Sources(com.hazelcast.jet.pipeline.Sources) List(java.util.List) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) Entry(java.util.Map.Entry) HOURS(java.util.concurrent.TimeUnit.HOURS) SinkStressTestUtil(com.hazelcast.jet.impl.connector.SinkStressTestUtil) ProcessingGuarantee(com.hazelcast.jet.config.ProcessingGuarantee) Sink(com.hazelcast.jet.pipeline.Sink) Assert.assertEquals(org.junit.Assert.assertEquals) IMap(com.hazelcast.map.IMap) KafkaConsumer(org.apache.kafka.clients.consumer.KafkaConsumer) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) Pipeline(com.hazelcast.jet.pipeline.Pipeline) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 97 with ProducerRecord

use of org.apache.kafka.clients.producer.ProducerRecord in project incubator-atlas by apache.

the class KafkaNotification method sendInternalToProducer.

@VisibleForTesting
void sendInternalToProducer(Producer p, NotificationType type, String[] messages) throws NotificationException {
    String topic = TOPIC_MAP.get(type);
    List<MessageContext> messageContexts = new ArrayList<>();
    for (String message : messages) {
        ProducerRecord record = new ProducerRecord(topic, message);
        LOG.debug("Sending message for topic {}: {}", topic, message);
        Future future = p.send(record);
        messageContexts.add(new MessageContext(future, message));
    }
    List<String> failedMessages = new ArrayList<>();
    Exception lastFailureException = null;
    for (MessageContext context : messageContexts) {
        try {
            RecordMetadata response = context.getFuture().get();
            LOG.debug("Sent message for topic - {}, partition - {}, offset - {}", response.topic(), response.partition(), response.offset());
        } catch (Exception e) {
            lastFailureException = e;
            failedMessages.add(context.getMessage());
        }
    }
    if (lastFailureException != null) {
        throw new NotificationException(lastFailureException, failedMessages);
    }
}
Also used : RecordMetadata(org.apache.kafka.clients.producer.RecordMetadata) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) ArrayList(java.util.ArrayList) NotificationException(org.apache.atlas.notification.NotificationException) Future(java.util.concurrent.Future) URISyntaxException(java.net.URISyntaxException) NotificationException(org.apache.atlas.notification.NotificationException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) AtlasException(org.apache.atlas.AtlasException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 98 with ProducerRecord

use of org.apache.kafka.clients.producer.ProducerRecord in project incubator-atlas by apache.

the class KafkaNotificationMockTest method shouldSendMessagesSuccessfully.

@Test
@SuppressWarnings("unchecked")
public void shouldSendMessagesSuccessfully() throws NotificationException, ExecutionException, InterruptedException {
    Properties configProperties = mock(Properties.class);
    KafkaNotification kafkaNotification = new KafkaNotification(configProperties);
    Producer producer = mock(Producer.class);
    String topicName = kafkaNotification.getTopicName(NotificationInterface.NotificationType.HOOK);
    String message = "This is a test message";
    Future returnValue = mock(Future.class);
    when(returnValue.get()).thenReturn(new RecordMetadata(new TopicPartition(topicName, 0), 0, 0));
    ProducerRecord expectedRecord = new ProducerRecord(topicName, message);
    when(producer.send(expectedRecord)).thenReturn(returnValue);
    kafkaNotification.sendInternalToProducer(producer, NotificationInterface.NotificationType.HOOK, new String[] { message });
    verify(producer).send(expectedRecord);
}
Also used : RecordMetadata(org.apache.kafka.clients.producer.RecordMetadata) Producer(org.apache.kafka.clients.producer.Producer) TopicPartition(org.apache.kafka.common.TopicPartition) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) Future(java.util.concurrent.Future) Properties(java.util.Properties) Test(org.testng.annotations.Test)

Example 99 with ProducerRecord

use of org.apache.kafka.clients.producer.ProducerRecord in project druid by druid-io.

the class KafkaSupervisorTest method addSomeEvents.

private void addSomeEvents(int numEventsPerPartition) throws Exception {
    // create topic manually
    try (Admin admin = kafkaServer.newAdminClient()) {
        admin.createTopics(Collections.singletonList(new NewTopic(topic, NUM_PARTITIONS, (short) 1))).all().get();
    }
    try (final KafkaProducer<byte[], byte[]> kafkaProducer = kafkaServer.newProducer()) {
        kafkaProducer.initTransactions();
        kafkaProducer.beginTransaction();
        for (int i = 0; i < NUM_PARTITIONS; i++) {
            for (int j = 0; j < numEventsPerPartition; j++) {
                kafkaProducer.send(new ProducerRecord<>(topic, i, null, StringUtils.toUtf8(StringUtils.format("event-%d", j)))).get();
            }
        }
        kafkaProducer.commitTransaction();
    }
}
Also used : ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) NewTopic(org.apache.kafka.clients.admin.NewTopic) Admin(org.apache.kafka.clients.admin.Admin)

Example 100 with ProducerRecord

use of org.apache.kafka.clients.producer.ProducerRecord in project druid by druid-io.

the class KafkaSupervisorTest method addMoreEvents.

private void addMoreEvents(int numEventsPerPartition, int num_partitions) throws Exception {
    try (Admin admin = kafkaServer.newAdminClient()) {
        admin.createPartitions(Collections.singletonMap(topic, NewPartitions.increaseTo(num_partitions))).all().get();
    }
    try (final KafkaProducer<byte[], byte[]> kafkaProducer = kafkaServer.newProducer()) {
        kafkaProducer.initTransactions();
        kafkaProducer.beginTransaction();
        for (int i = NUM_PARTITIONS; i < num_partitions; i++) {
            for (int j = 0; j < numEventsPerPartition; j++) {
                kafkaProducer.send(new ProducerRecord<>(topic, i, null, StringUtils.toUtf8(StringUtils.format("event-%d", j)))).get();
            }
        }
        kafkaProducer.commitTransaction();
    }
}
Also used : ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) Admin(org.apache.kafka.clients.admin.Admin)

Aggregations

ProducerRecord (org.apache.kafka.clients.producer.ProducerRecord)193 Test (org.junit.Test)90 KafkaProducer (org.apache.kafka.clients.producer.KafkaProducer)57 Properties (java.util.Properties)50 RecordMetadata (org.apache.kafka.clients.producer.RecordMetadata)40 ArrayList (java.util.ArrayList)39 Callback (org.apache.kafka.clients.producer.Callback)30 Future (java.util.concurrent.Future)26 TopicPartition (org.apache.kafka.common.TopicPartition)24 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)21 HashMap (java.util.HashMap)20 Random (java.util.Random)19 IOException (java.io.IOException)16 ConsumerRecord (org.apache.kafka.clients.consumer.ConsumerRecord)16 KafkaConsumer (org.apache.kafka.clients.consumer.KafkaConsumer)16 KafkaException (org.apache.kafka.common.KafkaException)16 List (java.util.List)13 MockProducer (org.apache.kafka.clients.producer.MockProducer)13 DefaultPartitioner (org.apache.kafka.clients.producer.internals.DefaultPartitioner)12 StreamsException (org.apache.kafka.streams.errors.StreamsException)12