Sending using KafkaTemplate returns a exception for using String as a key

Multi tool use
When using Integer as a key this is not a problem, kafka should be able to handle Strings as a key.
ProducerFactory<String, String> pf =
new DefaultKafkaProducerFactory<String, String>(senderProps);
KafkaTemplate<String, String> template = new KafkaTemplate<>(pf);
ProducerRecord<String,String> pr = new ProducerRecord<>("my-topic", "key1","test");
template.send(pr);`
It throws below exception:
Org.apache.kafka.common.errors.SerializationException: Can't convert
key of class java.lang.String to class
org.apache.kafka.common.serialization.IntegerSerializer specified in
key.serializer
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
at org.apache.kafka.common.serialization.IntegerSerializer.serialize(IntegerSerializer.java:21)
at org.apache.kafka.common.serialization.ExtendedSerializer$Wrapper.serialize(ExtendedSerializer.java:65)
at org.apache.kafka.common.serialization.ExtendedSerializer$Wrapper.serialize(ExtendedSerializer.java:55)
at org.apache.kafka.clients.producer.KafkaProducer.doSend(KafkaProducer.java:799)
at org.apache.kafka.clients.producer.KafkaProducer.send(KafkaProducer.java:784)
at org.springframework.kafka.core.DefaultKafkaProducerFactory$CloseSafeProducer.send(DefaultKafkaProducerFactory.java:285)
at org.springframework.kafka.core.KafkaTemplate.doSend(KafkaTemplate.java:357)
at org.springframework.kafka.core.KafkaTemplate.send(KafkaTemplate.java:206)
spring apache-kafka spring-kafka spring-kafka-test
add a comment |
When using Integer as a key this is not a problem, kafka should be able to handle Strings as a key.
ProducerFactory<String, String> pf =
new DefaultKafkaProducerFactory<String, String>(senderProps);
KafkaTemplate<String, String> template = new KafkaTemplate<>(pf);
ProducerRecord<String,String> pr = new ProducerRecord<>("my-topic", "key1","test");
template.send(pr);`
It throws below exception:
Org.apache.kafka.common.errors.SerializationException: Can't convert
key of class java.lang.String to class
org.apache.kafka.common.serialization.IntegerSerializer specified in
key.serializer
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
at org.apache.kafka.common.serialization.IntegerSerializer.serialize(IntegerSerializer.java:21)
at org.apache.kafka.common.serialization.ExtendedSerializer$Wrapper.serialize(ExtendedSerializer.java:65)
at org.apache.kafka.common.serialization.ExtendedSerializer$Wrapper.serialize(ExtendedSerializer.java:55)
at org.apache.kafka.clients.producer.KafkaProducer.doSend(KafkaProducer.java:799)
at org.apache.kafka.clients.producer.KafkaProducer.send(KafkaProducer.java:784)
at org.springframework.kafka.core.DefaultKafkaProducerFactory$CloseSafeProducer.send(DefaultKafkaProducerFactory.java:285)
at org.springframework.kafka.core.KafkaTemplate.doSend(KafkaTemplate.java:357)
at org.springframework.kafka.core.KafkaTemplate.send(KafkaTemplate.java:206)
spring apache-kafka spring-kafka spring-kafka-test
add a comment |
When using Integer as a key this is not a problem, kafka should be able to handle Strings as a key.
ProducerFactory<String, String> pf =
new DefaultKafkaProducerFactory<String, String>(senderProps);
KafkaTemplate<String, String> template = new KafkaTemplate<>(pf);
ProducerRecord<String,String> pr = new ProducerRecord<>("my-topic", "key1","test");
template.send(pr);`
It throws below exception:
Org.apache.kafka.common.errors.SerializationException: Can't convert
key of class java.lang.String to class
org.apache.kafka.common.serialization.IntegerSerializer specified in
key.serializer
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
at org.apache.kafka.common.serialization.IntegerSerializer.serialize(IntegerSerializer.java:21)
at org.apache.kafka.common.serialization.ExtendedSerializer$Wrapper.serialize(ExtendedSerializer.java:65)
at org.apache.kafka.common.serialization.ExtendedSerializer$Wrapper.serialize(ExtendedSerializer.java:55)
at org.apache.kafka.clients.producer.KafkaProducer.doSend(KafkaProducer.java:799)
at org.apache.kafka.clients.producer.KafkaProducer.send(KafkaProducer.java:784)
at org.springframework.kafka.core.DefaultKafkaProducerFactory$CloseSafeProducer.send(DefaultKafkaProducerFactory.java:285)
at org.springframework.kafka.core.KafkaTemplate.doSend(KafkaTemplate.java:357)
at org.springframework.kafka.core.KafkaTemplate.send(KafkaTemplate.java:206)
spring apache-kafka spring-kafka spring-kafka-test
When using Integer as a key this is not a problem, kafka should be able to handle Strings as a key.
ProducerFactory<String, String> pf =
new DefaultKafkaProducerFactory<String, String>(senderProps);
KafkaTemplate<String, String> template = new KafkaTemplate<>(pf);
ProducerRecord<String,String> pr = new ProducerRecord<>("my-topic", "key1","test");
template.send(pr);`
It throws below exception:
Org.apache.kafka.common.errors.SerializationException: Can't convert
key of class java.lang.String to class
org.apache.kafka.common.serialization.IntegerSerializer specified in
key.serializer
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
at org.apache.kafka.common.serialization.IntegerSerializer.serialize(IntegerSerializer.java:21)
at org.apache.kafka.common.serialization.ExtendedSerializer$Wrapper.serialize(ExtendedSerializer.java:65)
at org.apache.kafka.common.serialization.ExtendedSerializer$Wrapper.serialize(ExtendedSerializer.java:55)
at org.apache.kafka.clients.producer.KafkaProducer.doSend(KafkaProducer.java:799)
at org.apache.kafka.clients.producer.KafkaProducer.send(KafkaProducer.java:784)
at org.springframework.kafka.core.DefaultKafkaProducerFactory$CloseSafeProducer.send(DefaultKafkaProducerFactory.java:285)
at org.springframework.kafka.core.KafkaTemplate.doSend(KafkaTemplate.java:357)
at org.springframework.kafka.core.KafkaTemplate.send(KafkaTemplate.java:206)
spring apache-kafka spring-kafka spring-kafka-test
spring apache-kafka spring-kafka spring-kafka-test
edited Jan 3 at 9:20
Nishu Tayal
12.9k73584
12.9k73584
asked Jan 3 at 9:12
kambokambo
104
104
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Seems, your Key serializer is set as Integer in ProducerConfig. You need to set the KEY_SERIALIZER_CLASS_CONFIG
as String :
@Bean
public Map<String, Object> producerConfigs() {
Map<String, Object> props = new HashMap<>();
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
return props;
}
Thank you :) had to add StringDeserializer to the DefaultKafkaConsumerFactory aswell. Now it works well !
– kambo
Jan 3 at 9:43
1
Yeah, You need to add in Producer and consumer both whatever key and value type you are using :) If it helps you, please feel free to accept/upvote the answer :)
– Nishu Tayal
Jan 3 at 9:49
1
Please don't forget to accept the answer which solves your problem. It will help others :)
– Nishu Tayal
Jan 3 at 12:18
+1 for accepting this answer, please. That's the way how SO works: stackoverflow.com/help/someone-answers
– Artem Bilan
Jan 3 at 14:40
thank you, didn't know about that :)
– kambo
Jan 4 at 21:45
add a comment |
It looks like you have configured as keySerializer: IntegerSerializer, but you sent messages with key of type String. You should change serializer type or type of key, to match to each other
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54019235%2fsending-using-kafkatemplatestring-string-returns-a-exception-for-using-string%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Seems, your Key serializer is set as Integer in ProducerConfig. You need to set the KEY_SERIALIZER_CLASS_CONFIG
as String :
@Bean
public Map<String, Object> producerConfigs() {
Map<String, Object> props = new HashMap<>();
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
return props;
}
Thank you :) had to add StringDeserializer to the DefaultKafkaConsumerFactory aswell. Now it works well !
– kambo
Jan 3 at 9:43
1
Yeah, You need to add in Producer and consumer both whatever key and value type you are using :) If it helps you, please feel free to accept/upvote the answer :)
– Nishu Tayal
Jan 3 at 9:49
1
Please don't forget to accept the answer which solves your problem. It will help others :)
– Nishu Tayal
Jan 3 at 12:18
+1 for accepting this answer, please. That's the way how SO works: stackoverflow.com/help/someone-answers
– Artem Bilan
Jan 3 at 14:40
thank you, didn't know about that :)
– kambo
Jan 4 at 21:45
add a comment |
Seems, your Key serializer is set as Integer in ProducerConfig. You need to set the KEY_SERIALIZER_CLASS_CONFIG
as String :
@Bean
public Map<String, Object> producerConfigs() {
Map<String, Object> props = new HashMap<>();
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
return props;
}
Thank you :) had to add StringDeserializer to the DefaultKafkaConsumerFactory aswell. Now it works well !
– kambo
Jan 3 at 9:43
1
Yeah, You need to add in Producer and consumer both whatever key and value type you are using :) If it helps you, please feel free to accept/upvote the answer :)
– Nishu Tayal
Jan 3 at 9:49
1
Please don't forget to accept the answer which solves your problem. It will help others :)
– Nishu Tayal
Jan 3 at 12:18
+1 for accepting this answer, please. That's the way how SO works: stackoverflow.com/help/someone-answers
– Artem Bilan
Jan 3 at 14:40
thank you, didn't know about that :)
– kambo
Jan 4 at 21:45
add a comment |
Seems, your Key serializer is set as Integer in ProducerConfig. You need to set the KEY_SERIALIZER_CLASS_CONFIG
as String :
@Bean
public Map<String, Object> producerConfigs() {
Map<String, Object> props = new HashMap<>();
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
return props;
}
Seems, your Key serializer is set as Integer in ProducerConfig. You need to set the KEY_SERIALIZER_CLASS_CONFIG
as String :
@Bean
public Map<String, Object> producerConfigs() {
Map<String, Object> props = new HashMap<>();
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
return props;
}
answered Jan 3 at 9:23
Nishu TayalNishu Tayal
12.9k73584
12.9k73584
Thank you :) had to add StringDeserializer to the DefaultKafkaConsumerFactory aswell. Now it works well !
– kambo
Jan 3 at 9:43
1
Yeah, You need to add in Producer and consumer both whatever key and value type you are using :) If it helps you, please feel free to accept/upvote the answer :)
– Nishu Tayal
Jan 3 at 9:49
1
Please don't forget to accept the answer which solves your problem. It will help others :)
– Nishu Tayal
Jan 3 at 12:18
+1 for accepting this answer, please. That's the way how SO works: stackoverflow.com/help/someone-answers
– Artem Bilan
Jan 3 at 14:40
thank you, didn't know about that :)
– kambo
Jan 4 at 21:45
add a comment |
Thank you :) had to add StringDeserializer to the DefaultKafkaConsumerFactory aswell. Now it works well !
– kambo
Jan 3 at 9:43
1
Yeah, You need to add in Producer and consumer both whatever key and value type you are using :) If it helps you, please feel free to accept/upvote the answer :)
– Nishu Tayal
Jan 3 at 9:49
1
Please don't forget to accept the answer which solves your problem. It will help others :)
– Nishu Tayal
Jan 3 at 12:18
+1 for accepting this answer, please. That's the way how SO works: stackoverflow.com/help/someone-answers
– Artem Bilan
Jan 3 at 14:40
thank you, didn't know about that :)
– kambo
Jan 4 at 21:45
Thank you :) had to add StringDeserializer to the DefaultKafkaConsumerFactory aswell. Now it works well !
– kambo
Jan 3 at 9:43
Thank you :) had to add StringDeserializer to the DefaultKafkaConsumerFactory aswell. Now it works well !
– kambo
Jan 3 at 9:43
1
1
Yeah, You need to add in Producer and consumer both whatever key and value type you are using :) If it helps you, please feel free to accept/upvote the answer :)
– Nishu Tayal
Jan 3 at 9:49
Yeah, You need to add in Producer and consumer both whatever key and value type you are using :) If it helps you, please feel free to accept/upvote the answer :)
– Nishu Tayal
Jan 3 at 9:49
1
1
Please don't forget to accept the answer which solves your problem. It will help others :)
– Nishu Tayal
Jan 3 at 12:18
Please don't forget to accept the answer which solves your problem. It will help others :)
– Nishu Tayal
Jan 3 at 12:18
+1 for accepting this answer, please. That's the way how SO works: stackoverflow.com/help/someone-answers
– Artem Bilan
Jan 3 at 14:40
+1 for accepting this answer, please. That's the way how SO works: stackoverflow.com/help/someone-answers
– Artem Bilan
Jan 3 at 14:40
thank you, didn't know about that :)
– kambo
Jan 4 at 21:45
thank you, didn't know about that :)
– kambo
Jan 4 at 21:45
add a comment |
It looks like you have configured as keySerializer: IntegerSerializer, but you sent messages with key of type String. You should change serializer type or type of key, to match to each other
add a comment |
It looks like you have configured as keySerializer: IntegerSerializer, but you sent messages with key of type String. You should change serializer type or type of key, to match to each other
add a comment |
It looks like you have configured as keySerializer: IntegerSerializer, but you sent messages with key of type String. You should change serializer type or type of key, to match to each other
It looks like you have configured as keySerializer: IntegerSerializer, but you sent messages with key of type String. You should change serializer type or type of key, to match to each other
answered Jan 3 at 9:22
wardziniakwardziniak
1,6021616
1,6021616
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54019235%2fsending-using-kafkatemplatestring-string-returns-a-exception-for-using-string%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
lx0p9i1TXvn,2qG1ZbOq,y0T5