Kafka Consumer和ConsumerBuild在编码方面的差异 [英] Kafka consumer and consumerbuild difference in encoding
问题描述
我使用的是旧的kafka,我在下面使用的一段代码中,mEncoding可能是utf-7,utf-8,unicode等.
I was using old kafka where i was using piece of code below where mEncoding could be utf-7, utf-8, unicode etc.
new Consumer<Ignore, string>(mConfig, null, new StringDeserializer(mEncoding)))
我正在将我的kafka升级到1.4.0版本.
I am upgrading my kafka to 1.4.0 version.
我发现,ConsumerBuilder替换了Consumer,其中的消费者可以使用方法 SetValueDeserializer
,但它仅接受utf-8(Deserializers.Utf8).有什么办法可以发送其他编码吗?
I found that Consumer is replaced by ConsumerBuilder where method SetValueDeserializer
is available, but its accepting only utf-8 (Deserializers.Utf8). Is there any way i can send other encoding also?
推荐答案
您应该只实现自己的解串器.看起来可能像这样:
You should just implement your own deserializer. It could look something like this:
public class MyValueDeserializer : IDeserializer<string>
{
private readonly Encoding _encoding;
public MyValueDeserializer(Encoding encoding)
{
_encoding = encoding;
}
public string Deserialize(ReadOnlySpan<byte> data, bool isNull, SerializationContext context)
{
if (isNull)
{
return null;
}
var array = data.ToArray();
using var stream = new MemoryStream(array);
using var sr = new StreamReader(stream, _encoding);
return sr.ReadToEnd();
}
}
然后您可以使用 SetValueDeserializer(new MyValueDeserializer(Encoding.UTF7))
这篇关于Kafka Consumer和ConsumerBuild在编码方面的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!