Kafka Message Key - byte[] and String Simultaneously
<p>I'm having a very confusing issue with Kafka - specifically trying to obtain the Key of a message.</p>

<p>The key seems to think it's both a String and a byte[] </p>

<p>The following code produces the exception below:</p>

<pre><code>    Map&lt;String, Integer&gt; topicCount = new HashMap&lt;&gt;();
    topicCount.put(myConsumer.getTopic(), 1);

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

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

            MessageAndMetadata&lt;String, byte[]&gt; 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
        }
    }
</code></pre>

<p>Exception:      </p>

<pre><code>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      
</code></pre>

<p>Maven:</p>

<pre><code>&lt;dependency&gt;
  &lt;groupId&gt;org.apache.kafka&lt;/groupId&gt;
  &lt;artifactId&gt;kafka_2.10&lt;/artifactId&gt;
  &lt;version&gt;0.9.0.1&lt;/version&gt;
&lt;/dependency&gt;
</code></pre>

<p>If I uncomment the System.out line then I cannot even compile:</p>

<pre><code>[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[])
</code></pre>

<p>How is it that the compiler thinks the Key is a String (which is what I expected it to be) but that runtime it's a byte array?</p>

<p>What can I do to get the Key as a String?</p>

<p>Thanks,</p>

<p>KA.</p>