2
votes

I am trying to send Integer value through Kafka producer using the kafka provided API IntegerSerializer,but the integer value is not getting parsed correctly and it displayed in form of random unknown symbol on the Kafka consumer console.

public static void main(String[] args) throws Exception{
    int i;
    // Check arguments length value
    if(args.length == 0){
       System.out.println("Enter topic name");
       return;
    }

    //Assign topicName to string variable
    String topicName = args[0].toString();

    // create instance for properties to access producer configs   
    Properties props = new Properties();

    //Assign localhost id
    props.put("bootstrap.servers", "localhost:9092");

    //Set acknowledgements for producer requests.      
    props.put("acks", "all");

    //If the request fails, the producer can automatically retry,
    props.put("retries", "0");

    //Specify buffer size in config
    props.put("batch.size"," 16384");

    //Reduce the no of requests less than 0   
    props.put("linger.ms", "1");

    //The buffer.memory controls the total amount of memory available to the producer for buffering.   
    props.put("buffer.memory", "33554432");

    props.put("key.serializer", 
       "org.apache.kafka.common.serialization.StringSerializer");

    props.put("value.serializer", 
       "org.apache.kafka.common.serialization.IntegerSerializer");

    KafkaProducer<String,Integer> producerRcrd = new KafkaProducer<String,Integer>(props);
    producerRcrd.send(new ProducerRecord<String,Integer>(topicName, "Key1",100));
    System.out.println("Message sent successfully");
    producerRcrd.flush();
    producerRcrd.close();

 }
}

Then it is not showing 100 on Kafka-consumer console.

1

1 Answers

2
votes

Appending

--property key.deserializer=org.apache.kafka.common.serialization.StringDeserializer --property value.deserializer=org.apache.kafka.common.serialization.IntegerDeserializer

to kafka-console-consumer.sh, having console message formatter know how to deserialize your message body.