Search in sources :

Example 1 with StringDecoder

use of kafka.serializer.StringDecoder in project open-kilda by telstra.

the class SimpleKafkaTest method buildConsumer.

private ConsumerIterator<String, String> buildConsumer(String topic) {
    Properties props = consumerProperties();
    Map<String, Integer> topicCountMap = new HashMap<>();
    topicCountMap.put(topic, 1);
    ConsumerConfig consumerConfig = new ConsumerConfig(props);
    consumerConnector = Consumer.createJavaConsumerConnector(consumerConfig);
    Map<String, List<KafkaStream<String, String>>> consumers = consumerConnector.createMessageStreams(topicCountMap, new StringDecoder(null), new StringDecoder(null));
    KafkaStream<String, String> stream = consumers.get(topic).get(0);
    return stream.iterator();
}
Also used : HashMap(java.util.HashMap) StringDecoder(kafka.serializer.StringDecoder) ConsumerConfig(kafka.consumer.ConsumerConfig) List(java.util.List) Properties(java.util.Properties)

Example 2 with StringDecoder

use of kafka.serializer.StringDecoder in project microservices by pwillhan.

the class App method start.

private void start(String topic) {
    consumer = Consumer.createJavaConsumerConnector(config);
    /* We tell Kafka how many threads will read each topic. We have one topic and one thread */
    Map<String, Integer> topicCountMap = new HashMap<String, Integer>();
    topicCountMap.put(topic, new Integer(1));
    /* We will use a decoder to get Kafka to convert messages to Strings
        * valid property will be deserializer.encoding with the charset to use.
        * default is UTF8 which works for us */
    StringDecoder decoder = new StringDecoder(new VerifiableProperties());
    /* Kafka will give us a list of streams of messages for each topic.
        In this case, its just one topic with a list of a single stream */
    stream = consumer.createMessageStreams(topicCountMap, decoder, decoder).get(topic).get(0);
}
Also used : HashMap(java.util.HashMap) VerifiableProperties(kafka.utils.VerifiableProperties) StringDecoder(kafka.serializer.StringDecoder)

Example 3 with StringDecoder

use of kafka.serializer.StringDecoder in project jeesuite-libs by vakinge.

the class OldApiTopicConsumer method start.

@Override
public void start() {
    // 重置offset
    if (consumerContext.getOffsetLogHanlder() != null) {
        resetCorrectOffsets();
    }
    Map<String, Integer> topicCountMap = new HashMap<String, Integer>();
    for (String topicName : consumerContext.getMessageHandlers().keySet()) {
        int nThreads = 1;
        topicCountMap.put(topicName, nThreads);
        logger.info("topic[{}] assign fetch Threads {}", topicName, nThreads);
    }
    StringDecoder keyDecoder = new StringDecoder(new VerifiableProperties());
    MessageDecoder valueDecoder = new MessageDecoder(deserializer);
    Map<String, List<KafkaStream<String, Object>>> consumerMap = this.connector.createMessageStreams(topicCountMap, keyDecoder, valueDecoder);
    for (String topicName : consumerContext.getMessageHandlers().keySet()) {
        final List<KafkaStream<String, Object>> streams = consumerMap.get(topicName);
        for (final KafkaStream<String, Object> stream : streams) {
            MessageProcessor processer = new MessageProcessor(topicName, stream);
            this.fetchExecutor.execute(processer);
        }
    }
    // 
    runing.set(true);
}
Also used : HashMap(java.util.HashMap) VerifiableProperties(kafka.utils.VerifiableProperties) StringDecoder(kafka.serializer.StringDecoder) KafkaStream(kafka.consumer.KafkaStream) MessageDecoder(com.jeesuite.kafka.serializer.MessageDecoder) List(java.util.List)

Example 4 with StringDecoder

use of kafka.serializer.StringDecoder in project thingsboard by thingsboard.

the class KafkaDemoClient method buildConsumer.

private static ConsumerIterator<String, String> buildConsumer(String topic) {
    Map<String, Integer> topicCountMap = new HashMap<>();
    topicCountMap.put(topic, 1);
    ConsumerConfig consumerConfig = new ConsumerConfig(consumerProperties());
    ConsumerConnector consumerConnector = Consumer.createJavaConsumerConnector(consumerConfig);
    Map<String, List<KafkaStream<String, String>>> consumers = consumerConnector.createMessageStreams(topicCountMap, new StringDecoder(null), new StringDecoder(null));
    KafkaStream<String, String> stream = consumers.get(topic).get(0);
    return stream.iterator();
}
Also used : HashMap(java.util.HashMap) StringDecoder(kafka.serializer.StringDecoder) ConsumerConfig(kafka.consumer.ConsumerConfig) ConsumerConnector(kafka.javaapi.consumer.ConsumerConnector) List(java.util.List)

Example 5 with StringDecoder

use of kafka.serializer.StringDecoder in project open-kilda by telstra.

the class Original method buildConsumer.

private ConsumerIterator<String, String> buildConsumer(String topic) {
    Properties props = consumerProperties();
    Map<String, Integer> topicCountMap = new HashMap<>();
    topicCountMap.put(topic, 1);
    ConsumerConfig consumerConfig = new ConsumerConfig(props);
    consumerConnector = Consumer.createJavaConsumerConnector(consumerConfig);
    Map<String, List<KafkaStream<String, String>>> consumers = consumerConnector.createMessageStreams(topicCountMap, new StringDecoder(null), new StringDecoder(null));
    KafkaStream<String, String> stream = consumers.get(topic).get(0);
    return stream.iterator();
}
Also used : HashMap(java.util.HashMap) StringDecoder(kafka.serializer.StringDecoder) ConsumerConfig(kafka.consumer.ConsumerConfig) List(java.util.List) Properties(java.util.Properties)

Aggregations

HashMap (java.util.HashMap)5 StringDecoder (kafka.serializer.StringDecoder)5 List (java.util.List)4 ConsumerConfig (kafka.consumer.ConsumerConfig)3 Properties (java.util.Properties)2 VerifiableProperties (kafka.utils.VerifiableProperties)2 MessageDecoder (com.jeesuite.kafka.serializer.MessageDecoder)1 KafkaStream (kafka.consumer.KafkaStream)1 ConsumerConnector (kafka.javaapi.consumer.ConsumerConnector)1