amazon-kinesis-client icon indicating copy to clipboard operation
amazon-kinesis-client copied to clipboard

Kinesis Writing Objects to Data Stream

Open hanymorcos opened this issue 3 years ago • 1 comments

I'm trying to write object as bytes to Kinesis streams, but I'm getting errors. I wrote a class using Java Generic and sending objects as Array of Bytes.

java.io.StreamCorruptedException: invalid stream header: at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:1059)

Method to write to get bytes to ByteBuffer.wrap method.

public byte[] objectToBuffer(T record) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream out = null;

    out = new ObjectOutputStream(bos);
    out.writeObject(record);
    out.flush();
      return bos.toByteArray();

} To convert ByteBuffer back to object. It's erroring here:

public T bufferToObject(ByteBuffer recordByteBuffer) throws IOException, ClassNotFoundException {

  byte[] bytes = new byte[recordByteBuffer.limit()];
  recordByteBuffer.get(bytes);

  ObjectInputStream objectInputStream = new ObjectInputStream(
          new ByteArrayInputStream(bytes));
  return (T) objectInputStream.readObject();

}

What's really strange is that code works in Junit using Localstack? But it doesn't work on the real AWS kinesis.

hanymorcos avatar Apr 23 '21 10:04 hanymorcos

I changed the bytes returned to ascii. It's returning Json. Why is AWS returning JSON from Kinesis? I gave it bytes.

hanymorcos avatar Apr 26 '21 14:04 hanymorcos

Thank you for logging this issue, however this seems to be a KPL issue, please log your issue at https://github.com/awslabs/amazon-kinesis-producer if this is persisting. Closing this issue.

zeynepsu avatar Mar 16 '24 17:03 zeynepsu