amazon-kinesis-client
amazon-kinesis-client copied to clipboard
Kinesis Writing Objects to Data Stream
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.
I changed the bytes returned to ascii. It's returning Json. Why is AWS returning JSON from Kinesis? I gave it bytes.
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.