Tuesday, 1 October 2013

How to write into Cassandra with Byte Array following Big Endian Byte Order?

How to write into Cassandra with Byte Array following Big Endian Byte Order?

I need to write Byte Array value into Cassandra using Java code. Then I
will be having my C++ program which will retrieve that Byte Array data
from Cassandra and then it will deserialize it.
That Byte Array which I will be writing into Cassandra is made up of three
Byte Arrays as described below-
short schemaId = 32767;
long lastModifiedDate = "1379811105109L";
byte[] avroBinaryValue = os.toByteArray();
Now, I will write schemaId , lastModifiedDate and avroBinaryValue together
into a single Byte Array and that resulting Byte Array I will write into
Cassandra and then I will be having my C++ program which will retrieve
that Byte Array data from Cassandra and then deserialize it to extract
schemaId , lastModifiedDate and avroBinaryValue from it.
I am not sure whether I should use Big Endian here in my Java code while
writing to Cassandra so that C++ code get simplified while reading it
back? I have given a try on the Java side to make sure it is following
certain format (Big Endian) while writing into Byte Array but not sure
whether this is right or not?
public static void main(String[] args) throws Exception {
String os = "Byte Array Test";
byte[] avroBinaryValue = os.getBytes();
long lastModifiedDate = 1379811105109L;
short schemaId = 32767;
ByteArrayOutputStream byteOsTest = new ByteArrayOutputStream();
DataOutputStream outTest = new DataOutputStream(byteOsTest);
outTest.writeShort(schemaId);
outTest.writeLong(lastModifiedDate);
outTest.writeInt(avroBinaryValue.length);
outTest.write(avroBinaryValue);
byte[] allWrittenBytesTest = byteOsTest.toByteArray();
ByteBuffer bb =
ByteBuffer.wrap(allWrittenBytesTest).order(ByteOrder.BIG_ENDIAN);
// now what value I should write into Cassandra?
// or does this even looks right?
// And now how to deserialize it?
}
Can anyone help me with this ByteBuffer thing here? Thanks..

No comments:

Post a Comment