String 卡夫卡消息键-字节[]和字符串同时出现

String 卡夫卡消息键-字节[]和字符串同时出现,string,casting,byte,apache-kafka,String,Casting,Byte,Apache Kafka,我对卡夫卡有一个非常困惑的问题——特别是试图获得一条信息的关键 键似乎认为它既是字符串又是字节[] 以下代码生成以下异常: Map<String, Integer> topicCount = new HashMap<>(); topicCount.put(myConsumer.getTopic(), 1); Map<String, List<KafkaStream<byte[], byte[]>>> consu

我对卡夫卡有一个非常困惑的问题——特别是试图获得一条信息的关键

键似乎认为它既是字符串又是字节[]

以下代码生成以下异常:

    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
        }
    }
马文:

<dependency>
  <groupId>org.apache.kafka</groupId>
  <artifactId>kafka_2.10</artifactId>
  <version>0.9.0.1</version>
</dependency>
为什么编译器认为键是一个字符串(这是我所期望的),而运行时它是一个字节数组

如何才能将密钥作为字符串获取

谢谢


KA.

这不匹配!将流声明为
KafkaStream
,然后期望
ConsumerIterator it=stream.iterator()它应该是
ConsumerIterator it=stream.iterator()以匹配泛型。然后您可以获取
o.key()
并通过
newstring(o.key())从中创建一个字符串

最好设置KafkaStream通用参数类型为(字节[],字节[])。尝试如下更改代码:

ConsumerIterator<byte[], byte[]> it = stream.iterator();
while (it.hasNext()) {
    String key = new String(it.next().key());
    ...
}
ConsumerIterator it=stream.iterator();
while(it.hasNext()){
字符串键=新字符串(it.next().key());
...
}
[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[])
ConsumerIterator<byte[], byte[]> it = stream.iterator();
while (it.hasNext()) {
    String key = new String(it.next().key());
    ...
}