我尝试使用数据类型 blob
.这给了一些 Datastax 异常(exception)。我尝试了对象本身,bytearray。还是不行:
Caused by: com.datastax.driver.core.exceptions.InvalidQueryException: Invalid STRING constant ([B@547248ad) for user_object of type blob
这是失败的插入:
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("')"
+ ";");
最佳答案
从文档
blob | blobs | Arbitrary bytes (no validation), expressed as hexadecimal
所以你需要的是Bytes类(class)。以下是我用来序列化/反序列化需要保存在 Cassandra 中的 Java 对象的接口(interface)
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;
}
}
}
高温下,
卡罗
关于cassandra - 如何使用 blob 数据类型在 cassandra 中存储对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25197685/