naj*_*eeb 2 cassandra datastax-java-driver
我尝试了数据类型blob.这给了一些Datastax例外.我尝试了对象本身,bytearray.仍然没有好处:
Caused by: com.datastax.driver.core.exceptions.InvalidQueryException: Invalid STRING constant ([B@547248ad) for user_object of type blob
Run Code Online (Sandbox Code Playgroud)
这是失败的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("')"
+ ";");
Run Code Online (Sandbox Code Playgroud)
从文档
blob | blobs | Arbitrary bytes (no validation), expressed as hexadecimal
Run Code Online (Sandbox Code Playgroud)
所以你需要的是由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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
HTH,卡罗
| 归档时间: |
|
| 查看次数: |
6006 次 |
| 最近记录: |