如何使用blob数据类型在cassandra中存储对象 [英] How can I store Objects in cassandra using the blob datatype

查看:2017
本文介绍了如何使用blob数据类型在cassandra中存储对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用数据类型 blob 。这是一些Datastax异常。我试过对象本身,bytearray。仍然没有好:

 原因:com.datastax.driver.core.exceptions.InvalidQueryException:STRING常量无效([B @ 547248ad)b类型的user_object 

这是失败的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 ',')
.append(link).append(',')
.append(online)。append(',')
.append (serializer(register))。append(')
+;);


解决方案

来自文档

  blob | blob |任意字节(无验证),以十六进制表示

所以你需要的是字节类。以下是我用来序列化/反序列化Java对象的接口我需要保存在Cassandra中

  public interface Bufferable extends Serializable {

static final Logger LOGGER = LoggerFactory.getLogger(Bufferable.class);

默认ByteBuffer serialize(){
try(ByteArrayOutputStream bytes = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bytes);){
oos.writeObject (这个);
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(反序列化可缓冲对象错误,e);
return null;
}
}
}

HTH,
Carlo


I tried with the data type blob. That's giving some Datastax exception. I tried the object itself, bytearray. Still no good:

 Caused by: com.datastax.driver.core.exceptions.InvalidQueryException: Invalid STRING constant ([B@547248ad) for user_object of type blob

This is the failing 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("')"
                    + ";");

解决方案

From documentation

blob  | blobs  |  Arbitrary bytes (no validation), expressed as hexadecimal

so what you need is provided by the Bytes class. The following is an interface I use to serialize/deserialize Java objects I need to save in Cassandra

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, Carlo

这篇关于如何使用blob数据类型在cassandra中存储对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆