我的代码要消耗卡夫卡,它不打印任何输出
我正在尝试通过kafka使用json消息,但未提供任何输入, 这是我的输入:{“ opp_id”:“ 2”,“ opp_tenant_id”:“ 3”,“ createdWhen”:“ 4”} 我可以接收字符串反序列化器,但不能使用Jsondeserializer
非常感谢您的帮助,
ObjectMapper mapper = new ObjectMapper();
KafkaConsumer<String, JsonNode> consumer = new KafkaConsumer<String, JsonNode>(props);
try {
while (true) {
ConsumerRecords<String, JsonNode> records = consumer.poll(100);
for (ConsumerRecord<String, JsonNode> record : records) {
JsonNode jsonNode = record.value();
System.out.println(mapper.treeToValue(jsonNode,Contact.class));
}
}
}
Pojo,
public class Contact {
private String opp_id;
private String opp_tenant_id;
private String createdWhen;
public Contact(String opp_id,String opp_tenant_id, String createdWhen)
{
this.opp_id =opp_id;
this.opp_tenant_id = opp_tenant_id;
this.createdWhen = createdWhen;
}
public void parseString(String csvStr){
StringTokenizer st = new StringTokenizer(csvStr,",");
opp_id = st.nextToken();
opp_tenant_id = st.nextToken();
createdWhen = st.nextToken();
}
public Contact() {}
public String getopp_id(){
return opp_id;
}
public Contact setopp_id(String opp_id) {
this.opp_id=opp_id;
return this;
}
public String getopp_tenant_id(){
return opp_tenant_id;
}
public Contact setopp_tenant_id(String opp_tenant_id) {
this.opp_tenant_id=opp_tenant_id;
return this;
}
public String getcreatedWhen(){
return createdWhen;
}
public Contact setcreatedWhen(String createdWhen) {
this.createdWhen=createdWhen;
return this;
}
答案 0 :(得分:0)
Apache kafka没有任何Error in `$<-.data.frame`(`*tmp*`, newvar, value = numeric(0)) : replacement
has 0 rows, data has 139
,并且apache kafka中可用的所有反序列化类均为JsonDeserializer
(有关更多信息here),因此您可以使用ByteArrayDeserializer, IntegerDeserializer, LongDeserializer, StringDeserializer
来读取JSON格式的消息,并使用StringDeserializer
将JSON字符串转换为POJO类
或者如果您想使用Object mapper
,请选择Spring kafka