You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@kafka.apache.org by "kader81@yahoo.com.INVALID" <ka...@yahoo.com.INVALID> on 2014/11/18 07:43:55 UTC
Kafka consumer - want to print partition id along with message
Hi,
I am trying to learn more on Kafka consuming patterns if we have partitions on the topic and testing few scenarios. I would like to print the message when the consumer consumes along with the partition id. How to get this done ?
We have simple consumer code (high level)
public void run() {
ConsumerIterator<byte[], byte[]> it = m_stream.iterator();
while (it.hasNext())
System.out.println("Thread " + m_threadNumber + ": " + new String(it.next().message()));
System.out.println("Shutting down Thread: " + m_threadNumber);
}
How to print the message as well as partition id ?
Tried to assign it.next() to MessageandMetadata variable and printed partition id (which is declared as int). But while printing message() - it is declared as object in MessageandMetadata. Message is getting printed as junk data
Tried this:
MessageandMetadata msg;
msg = it.next()
System.out.println("Partition "+ msg.partition() + "Message data" + msg.message().tostring());
Here msg.message() is not printing string value.
Can anyone help on this.
Kader .
Sent from Yahoo Mail on Android
Re: Kafka consumer - want to print partition id along with message
Posted by Jun Rao <ju...@gmail.com>.
You need to define msg as MessageAndMetadata<byte[], byte[]>.
Thanks,
Jun
On Mon, Nov 17, 2014 at 10:43 PM, kader81@yahoo.com.INVALID <
kader81@yahoo.com.invalid> wrote:
> Hi,
>
> I am trying to learn more on Kafka consuming patterns if we have
> partitions on the topic and testing few scenarios. I would like to print
> the message when the consumer consumes along with the partition id. How to
> get this done ?
>
> We have simple consumer code (high level)
>
> public void run() {
> ConsumerIterator<byte[], byte[]> it = m_stream.iterator();
> while (it.hasNext())
> System.out.println("Thread " + m_threadNumber + ":
> " + new String(it.next().message()));
> System.out.println("Shutting down Thread: " + m_threadNumber);
> }
>
> How to print the message as well as partition id ?
>
> Tried to assign it.next() to MessageandMetadata variable and printed
> partition id (which is declared as int). But while printing message() - it
> is declared as object in MessageandMetadata. Message is getting printed as
> junk data
>
> Tried this:
>
> MessageandMetadata msg;
> msg = it.next()
> System.out.println("Partition "+ msg.partition() + "Message data" +
> msg.message().tostring());
>
> Here msg.message() is not printing string value.
> Can anyone help on this.
>
> Kader .
>
> Sent from Yahoo Mail on Android
>
>