kafka消息键-byte[]和string

bwleehnv  于 2021-06-07  发布在  Kafka
关注(0)|答案(2)|浏览(400)

我对Kafka有一个非常困惑的问题-特别是试图获得一个信息的关键。
键似乎认为它既是字符串又是字节[]
以下代码生成以下异常:

Map<String, Integer> topicCount = new HashMap<>();
    topicCount.put(myConsumer.getTopic(), 1);

    Map<String, List<KafkaStream<byte[], byte[]>>> consumerStreams = myConsumer.getConsumer().createMessageStreams(topicCount);
    List<KafkaStream<byte[], byte[]>> streams = consumerStreams.get(myConsumer.getTopic());
    System.out.println("Listening to topic " + myConsumer.getTopic());
    for (final KafkaStream stream : streams) {
        ConsumerIterator<String, byte[]> it = stream.iterator();
        while (it.hasNext()) {

            System.out.println("Message received from topic");

            MessageAndMetadata<String, byte[]> o = it.next();

            Object messageKey = o.key();
            System.out.println("messageKey is type: " + messageKey.getClass().getName());
            System.out.println("messageKey is type: " + messageKey.getClass().getCanonicalName());
            System.out.println("o keyDecoder: " + o.keyDecoder());

            System.out.println("Key from message: " + o.key());  //This throws exception - [B cannot be cast to java.lang.String
            //System.out.println("Key as String: " + new String(o.key(), StandardCharsets.UTF_8));    //uncomment this compile Exception - no suitable constructor found for String(java.lang.String,java.nio.charset.Charset)

            byte[] bytesIn = o.message();       //getting the bytes is fine

            System.out.println("MessageAndMetadata: " + o);

            ///other code cut
        }
    }

例外情况:

Listening to topic MyKafkaTopic
Message received from topic
messageKey is type: [B
messageKey is type: byte[]
o decoder: kafka.serializer.DefaultDecoder@2e0d0acd
[WARNING] 
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:293)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassCastException: [B cannot be cast to java.lang.String
    at com.foo.bar.KafkaCFS.process(KafkaCFS.java:153)
    at com.foo.bar.KafkaCFS.run(KafkaCFS.java:63)
    at com.foo.bar.App.main(App.java:90)
    ... 6 more

Maven:

<dependency>
  <groupId>org.apache.kafka</groupId>
  <artifactId>kafka_2.10</artifactId>
  <version>0.9.0.1</version>
</dependency>

如果我取消对system.out行的注解,那么我甚至无法编译:

[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /C:/Dev/main/java/com/foo/bar/KafkaCFS.java:[152,56] no suitable constructor found for String(java.lang.String,java.nio.charset.Charset)
    constructor java.lang.String.String(byte[],int) is not applicable
    (argument mismatch; java.lang.String cannot be converted to byte[])

为什么编译器认为键是一个字符串(这是我所期望的),而运行时它是一个字节数组?
我该怎么做才能把钥匙变成字符串?
谢谢,
灵魂。

huus2vyu

huus2vyu1#

最好设置kafkastream泛型参数类型为(byte[],byte[])。尝试这样更改代码:

ConsumerIterator<byte[], byte[]> it = stream.iterator();
while (it.hasNext()) {
    String key = new String(it.next().key());
    ...
}
lymgl2op

lymgl2op2#

这不匹配!您正在声明流为 KafkaStream<byte[], byte[]> 然后你期望 ConsumerIterator<String, byte[]> it = stream.iterator(); 应该是的 ConsumerIterator<byte[], byte[]> it = stream.iterator(); 匹配泛型。然后你就可以 o.key() 并通过 new String(o.key());

相关问题