我正在尝试将Apache camel与Kafka集成,并编写了一个示例程序来读取文件并写入Kafka Topic。但是这样做时我会遇到错误。我能够以相反的方式从Kafka主题读取并写入文件。
org.apache.kafka.common.errors.SerializationException:无法将类org.apache.camel.component.file.GenericFile的值转换为value中指定的类org.apache.kafka.common.serialization.StringSerializer。串行 [#0 - file:// C:%5Cshare%5Cinput] KafkaProducer WARN没有消息键或分区键集 [#0 - file:// C:%5Cshare%5Cinput] GenericFileOnCompletion WARN回滚文件策略:org.apache.camel.component.file.strategy.GenericFileRenameProcessStrategy@7127845b for file:GenericFile [C:\ share \ input \ file。文本] [#0 - file:// C:%5Cshare%5Cinput] DefaultErrorHandler ERROR(ExchangeId上的MessageId:ID-L8-CWBL462-49953-1480494317350-0-21的传递失败:ID-L8-CWBL462-49953-1480494317350-0 -22)。交付尝试后耗尽:1捕获:org.apache.kafka.common.errors.SerializationException:无法将类org.apache.camel.component.file.GenericFile的值转换为类org.apache.kafka.common.serialization。在value.serializer
中指定的StringSerializer代码
@ContextName( “myCdiCamelContext”) 公共类MyRoutes扩展了RouteBuilder {
@Inject
@Uri("file:C:\\share\\input?fileName=file.txt&noop=true")
private Endpoint inputEndpoint;
@Inject
@Uri("kafka:localhost:9092?topic=test&groupId=testing&autoOffsetReset=earliest&consumersCount=1")
private Endpoint resultEndpoint;
@Override
public void configure() throws Exception {
from(inputEndpoint)
.to(resultEndpoint);
}
}
答案 0 :(得分:1)
添加新处理器后,它对我有用
public void configure() throws Exception {
from(inputEndpoint).process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
exchange.getIn().setBody(exchange.getIn().getBody(),String.class);
exchange.getIn().setHeader(KafkaConstants.PARTITION_KEY, 0);
exchange.getIn().setHeader(KafkaConstants.KEY, "1");
}
})
.to(resultEndpoint);
}