Search in sources :

Example 6 with KafkaProducer

use of org.apache.kafka.clients.producer.KafkaProducer in project storm by apache.

the class TridentKafkaState method prepare.

public void prepare(Properties options) {
    Validate.notNull(mapper, "mapper can not be null");
    Validate.notNull(topicSelector, "topicSelector can not be null");
    producer = new KafkaProducer(options);
}
Also used : KafkaProducer(org.apache.kafka.clients.producer.KafkaProducer)

Example 7 with KafkaProducer

use of org.apache.kafka.clients.producer.KafkaProducer in project beam by apache.

the class ResumeFromCheckpointStreamingTest method produce.

private static void produce(Map<String, Instant> messages) {
    Properties producerProps = new Properties();
    producerProps.putAll(EMBEDDED_KAFKA_CLUSTER.getProps());
    producerProps.put("request.required.acks", 1);
    producerProps.put("bootstrap.servers", EMBEDDED_KAFKA_CLUSTER.getBrokerList());
    Serializer<String> stringSerializer = new StringSerializer();
    Serializer<Instant> instantSerializer = new InstantSerializer();
    try (@SuppressWarnings("unchecked") KafkaProducer<String, Instant> kafkaProducer = new KafkaProducer(producerProps, stringSerializer, instantSerializer)) {
        for (Map.Entry<String, Instant> en : messages.entrySet()) {
            kafkaProducer.send(new ProducerRecord<>(TOPIC, en.getKey(), en.getValue()));
        }
        kafkaProducer.close();
    }
}
Also used : KafkaProducer(org.apache.kafka.clients.producer.KafkaProducer) InstantSerializer(org.apache.beam.sdk.io.kafka.serialization.InstantSerializer) Instant(org.joda.time.Instant) Properties(java.util.Properties) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 8 with KafkaProducer

use of org.apache.kafka.clients.producer.KafkaProducer in project samza by apache.

the class TestKafkaSystemProducerJava method testInstantiateProducer.

@Test
public void testInstantiateProducer() {
    KafkaSystemProducer ksp = new KafkaSystemProducer("SysName", new ExponentialSleepStrategy(2.0, 200, 10000), new AbstractFunction0<Producer<byte[], byte[]>>() {

        @Override
        public Producer<byte[], byte[]> apply() {
            return new KafkaProducer<>(new HashMap<String, Object>());
        }
    }, new KafkaSystemProducerMetrics("SysName", new MetricsRegistryMap()), new AbstractFunction0<Object>() {

        @Override
        public Object apply() {
            return System.currentTimeMillis();
        }
    });
    long now = System.currentTimeMillis();
    assertTrue((Long) ksp.clock().apply() >= now);
}
Also used : HashMap(java.util.HashMap) ExponentialSleepStrategy(org.apache.samza.util.ExponentialSleepStrategy) KafkaProducer(org.apache.kafka.clients.producer.KafkaProducer) Producer(org.apache.kafka.clients.producer.Producer) MetricsRegistryMap(org.apache.samza.metrics.MetricsRegistryMap) Test(org.junit.Test)

Example 9 with KafkaProducer

use of org.apache.kafka.clients.producer.KafkaProducer in project samza by apache.

the class TestZkStreamProcessorBase method produceMessages.

/**
   * Produces the provided number of messages to the topic.
   */
protected void produceMessages(final int start, String topic, int numMessages) {
    KafkaProducer producer = getKafkaProducer();
    for (int i = start; i < numMessages + start; i++) {
        try {
            LOG.info("producing " + i);
            producer.send(new ProducerRecord(topic, i % 2, String.valueOf(i), String.valueOf(i).getBytes())).get();
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
    }
}
Also used : KafkaProducer(org.apache.kafka.clients.producer.KafkaProducer) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) ExecutionException(java.util.concurrent.ExecutionException)

Example 10 with KafkaProducer

use of org.apache.kafka.clients.producer.KafkaProducer in project spark-dataflow by cloudera.

the class KafkaStreamingTest method init.

@BeforeClass
public static void init() throws IOException {
    EMBEDDED_ZOOKEEPER.startup();
    EMBEDDED_KAFKA_CLUSTER.startup();
    // write to Kafka
    Properties producerProps = new Properties();
    producerProps.putAll(EMBEDDED_KAFKA_CLUSTER.getProps());
    producerProps.put("request.required.acks", 1);
    producerProps.put("bootstrap.servers", EMBEDDED_KAFKA_CLUSTER.getBrokerList());
    Serializer<String> stringSerializer = new StringSerializer();
    try (@SuppressWarnings("unchecked") KafkaProducer<String, String> kafkaProducer = new KafkaProducer(producerProps, stringSerializer, stringSerializer)) {
        for (Map.Entry<String, String> en : KAFKA_MESSAGES.entrySet()) {
            kafkaProducer.send(new ProducerRecord<>(TOPIC, en.getKey(), en.getValue()));
        }
    }
}
Also used : KafkaProducer(org.apache.kafka.clients.producer.KafkaProducer) Properties(java.util.Properties) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) ImmutableMap(com.google.cloud.dataflow.sdk.repackaged.com.google.common.collect.ImmutableMap) Map(java.util.Map) BeforeClass(org.junit.BeforeClass)

Aggregations

KafkaProducer (org.apache.kafka.clients.producer.KafkaProducer)19 Properties (java.util.Properties)12 ProducerRecord (org.apache.kafka.clients.producer.ProducerRecord)8 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)4 ArrayList (java.util.ArrayList)3 Random (java.util.Random)3 Test (org.junit.Test)3 ISE (io.druid.java.util.common.ISE)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 TopicExistsException (kafka.common.TopicExistsException)2 ZkClient (org.I0Itec.zkclient.ZkClient)2 Callback (org.apache.kafka.clients.producer.Callback)2 RecordMetadata (org.apache.kafka.clients.producer.RecordMetadata)2 KafkaStreams (org.apache.kafka.streams.KafkaStreams)2 DateTime (org.joda.time.DateTime)2 DateTimeZone (org.joda.time.DateTimeZone)2 DateTimeFormatter (org.joda.time.format.DateTimeFormatter)2