我需要使用 Java 代码将 Byte Array
值写入 Cassandra。然后我将让我的 C++ 程序从 Cassandra 读取相同的 Byte Array
数据。
该字节数组由三个字节数组组成,如下所述 -
short schemaId = 32767;
long lastModifiedDate = "1379811105109L";
byte[] avroBinaryValue = os.toByteArray();
现在,我将把 schemaId
、lastModifiedDate
和 avroBinaryValue
一起写入一个 Byte Array
中,然后得到字节数组,我将写回 Cassandra,然后我将使用我的 C++ 程序从 Cassandra 检索字节数组数据,然后反序列化以提取 schemaId
、lastModifiedDate
和 avroBinaryValue
来自它。
所以现在我很困惑在写 Cassandra 时是否应该在我的 Java 代码中使用 Big Endian?还是在将数据存储到 Cassandra 时使用小端字节顺序?
下面是代码,到目前为止,我已经掌握了 Java,它将所有内容序列化为一个单字节数组...
public static void main(String[] args) throws Exception {
String os = "whatever os is";
byte[] avroBinaryValue = os.getBytes();
long lastModifiedDate = 1379811105109L;
short schemaId = 32767;
ByteArrayOutputStream byteOsTest = new ByteArrayOutputStream();
DataOutputStream outTest = new DataOutputStream(byteOsTest);
outTest.writeShort(schemaId); // first write schemaId
outTest.writeLong(lastModifiedDate); // second lastModifiedDate
outTest.writeInt(avroBinaryValue.length); // then attributeLength
outTest.write(avroBinaryValue); // then its value
byte[] allWrittenBytesTest = byteOsTest.toByteArray();
// write this allWrittenBytesTest into Cassandra
// now deserialize it and extract everything from it
DataInputStream inTest = new DataInputStream(new ByteArrayInputStream(allWrittenBytesTest));
short schemaIdTest = inTest.readShort();
long lastModifiedDateTest = inTest.readLong();
int sizeAvroTest = inTest.readInt();
byte[] avroBinaryValue1 = new byte[sizeAvroTest];
inTest.read(avroBinaryValue1, 0, sizeAvroTest);
System.out.println(schemaIdTest);
System.out.println(lastModifiedDateTest);
System.out.println(new String(avroBinaryValue1));
}
而且我也在尝试查看在 Java 中是否有任何有效或正确的方法来执行此操作,因为我需要使用 C++ 程序从 Cassandra 检索这些数据,所以我不想在 C++ 方面也有任何问题.. 所以我试图确保当我从 Java 端将这些数据写入 Cassandra 时,一切看起来都很好..
现在,为了测试我正在做的事情是——我将这个字节数组写入 Java 程序的一个文件中,我正在使用 C++ 程序读取同一个文件,然后相应地反序列化该字节数组。
我希望我的问题足够清楚。有人可以帮我解决这个问题吗?
最佳答案
为什么不使用像 google protobuf (http://code.google.com/p/protobuf/) 这样的序列化框架,这样您就不必担心底层细节,也不必从任何语言和工具读取和写入
关于java - 将数据存储到 Cassandra 时是大端还是小端?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19108826/