cassandra - 如何使用 blob 数据类型在 cassandra 中存储对象

标签 cassandra datastax-java-driver

我尝试使用数据类型 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/

相关文章:

cassandra - Cassandra 集群中的 IP 交换

c++ - 如何将 time-uuid(存储在 boost uuid 中)转换为自纪元以来的时间戳/时间?

cassandra - Hibernate OGM 和 Kundera 有什么区别

spring-boot - 如何查看在 Spring boot 中使用 Cassandra 时生成的 CQL

Hadoop/Cassandra - 如何存储和分析来自数千个传感器的数据?

cassandra - Apache Cassandra - 以 LIMIT 作为参数的访问器不起作用

apache-spark - Cassandra datastax 驱动程序连接突然终止

asynchronous - 异步 cassandra 查询

Cassandra NoClassDefFoundError : com/google/common/util/concurrent/AsyncFunction

cassandra - 如何使用 cassandra 更新处理竞争条件?