我尝试使用数据类型blob
。这给了一些Datastax例外。我尝试了对象本身,bytearray。仍然没有好处:
Caused by: com.datastax.driver.core.exceptions.InvalidQueryException: Invalid STRING constant ([B@547248ad) for user_object of type blob
这是失败的INSERT:
executeSting.append("INSERT INTO htadb.objecttable (object_id, bucket_name, object_key, link, user_status, user_object) ")
.append("VALUES (")
.append(objectId).append(",'")
.append(bucketName).append("','")
.append(key).append("','")
.append(link).append("','")
.append("online").append("','")
.append(serializer(register)).append("')"
+ ";");
答案 0 :(得分:6)
来自文档
blob | blobs | Arbitrary bytes (no validation), expressed as hexadecimal
所以你需要的是Bytes课程。以下是我用来序列化/反序列化我需要在Cassandra中保存的Java对象的接口
public interface Bufferable extends Serializable {
static final Logger LOGGER = LoggerFactory.getLogger(Bufferable.class);
default ByteBuffer serialize() {
try (ByteArrayOutputStream bytes = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bytes);) {
oos.writeObject(this);
String hexString = Bytes.toHexString(bytes.toByteArray());
return Bytes.fromHexString(hexString);
} catch (IOException e) {
LOGGER.error("Serializing bufferable object error", e);
return null;
}
}
public static Bufferable deserialize(ByteBuffer bytes) {
String hx = Bytes.toHexString(bytes);
ByteBuffer ex = Bytes.fromHexString(hx);
try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(ex.array()));) {
return (Bufferable) ois.readObject();
} catch (ClassNotFoundException | IOException e) {
LOGGER.error("Deserializing bufferable object error", e);
return null;
}
}
}
HTH, 卡罗
答案 1 :(得分:0)
您可以使用 Array[Byte]
java.nio.ByteBuffer.wrap()
对象