0

I am using Kafka and following along with this tutorial (https://cwiki.apache.org/confluence/display/KAFKA/Consumer+Group+Example)

After some tweaks, the code compiles and everything runs like it should. my problem is that I am trying to utilize an array of bytes that the Kafka server sends me to do some processing. If I use the default code, everything works fine and the byte array gets converted to a String and displayed on my screen. If I try to read the byte array and assign it to a String so that I can display it to the screen and then parse it nothing happens.

it.next().message() returns a byte array

Default code:

ConsumerIterator<byte[], byte[]> it = m_stream.iterator();
while (it.hasNext())
    System.out.println("Thread " + m_threadNumber + ": " + new String(it.next().message()));

My Code that breaks:

 ConsumerIterator<byte[], byte[]> it = m_stream.iterator();
 String msg= "";
 while (it.hasNext())
    msg = new String(it.next().message());
    System.out.println("Thread " + m_threadNumber + ": " + msg);

Can anybody tell me why I cannot assign the byte array to my String? And of course how to fix my glitch?

I've taken a look at:

Java byte to string

Convert byte to string in Java

converting byte[] to string

but none of them seem to apply, they all try to assign the byte array to the String as the String is initialized and I cannot do that here.

6
  • What error message are you getting? Commented Jul 24, 2015 at 16:22
  • Also, why don't you have braces around your while-loop? Commented Jul 24, 2015 at 16:22
  • no error message. nothing at all is displayed to the screen Commented Jul 24, 2015 at 16:22
  • Please describe the problem more precisely than "it breaks" Commented Jul 24, 2015 at 16:23
  • 1
    OK well there it is I have been sitting at my desk too long. The default code only executes that one line so I didn't need the brackets. When I added the extra line I violated that assumption of a while one liner. Credit to @Juan as having the first answer when I checked in (and honestly I owe you an apology because I scoffed at it. Sorry for the ill thoughts about your reply) I'll accept it as soon as SO will let me Commented Jul 24, 2015 at 16:25

3 Answers 3

4

You are missing the curly brackets so your println is only being executed the last time. Try this:

ConsumerIterator<byte[], byte[]> it = m_stream.iterator();
String msg= "";
while (it.hasNext())
{
   msg = new String(it.next().message());
   System.out.println("Thread " + m_threadNumber + ": " + msg);
}
Sign up to request clarification or add additional context in comments.

Comments

3

Your code is failing because you did not enclose your code block in braces. Adjusting your code reveals:

ConsumerIterator<byte[], byte[]> it = m_stream.iterator();
while (it.hasNext()) { // <- here
    String msg = new String(it.next().message());
    System.out.println("Thread " + m_threadNumber + ": " + msg);
} // <- and here

In fact, with these braces this code snippet is equivalent to your first code snippet.

By the way: No need to declare the variable msg outside the loop.

Comments

0

Just put curly braces while loop block.

while (it.hasNext()){msg = new String(it.next().message());
System.out.println("Thread " + m_threadNumber + ": " + msg);}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.