简体   繁体   English

将 Redis ByteString 转换为 BasicDBObject

[英]Convert Redis ByteString to BasicDBObject

I need to convert serialized Java Object (get from Redis) to a Java model that extends com.mongodb.BasicDBObject (mongo-java-driver).我需要将序列化的 Java 对象(从 Redis 获取)转换为扩展 com.mongodb.BasicDBObject(mongo-java-driver)的 Java 模型。

class Member extends BasicDBObject {
    public Member(DBObject doc) {
        super(doc.toMap());
    }
    public Member(BasicDBObject doc) {
        super(doc);
    }
    ...
}

What I got from Redis is in ByteString form.我从 Redis 得到的是 ByteString 形式。

In summary, I need to convert ByteString -> BasicDBObject总之,我需要转换 ByteString -> BasicDBObject

You should use an intermediate model to persist your model.您应该使用中间模型来持久化您的模型。 MongoDB's BasicDBObject s base on LinkedHashMap , but there is a good chance, that you end up with internal data structures that are not Java-serializable. MongoDB 的BasicDBObject基于LinkedHashMap ,但很有可能您最终会得到无法 Java 序列化的内部数据结构。

Neither DBObject nor BSONObject extend Serializable . DBObjectBSONObject都没有扩展Serializable I propose that you store JSON within Redis.我建议您将 JSON 存储在 Redis 中。 A shortcut could be storing BSON instead of JSON within Redis (see BasicBSONEncoder#encode and BasicBSONDecoder#readObject ).快捷方式可能是在 Redis 中存储 BSON 而不是 JSON(请参阅BasicBSONEncoder#encodeBasicBSONDecoder#readObject )。

@mp911de You are right. @mp911de 你是对的。 The good thing is that DBObject and BSONObject do not extend Serializable.好消息是 DBObject 和 BSONObject 不扩展 Serializable。 In other words, there is no need to write a deserializer for DBObject/BSONObject.换句话说,不需要为 DBObject/BSONObject 编写反序列化器。 I found a solution:我找到了一个解决方案:

ByteString from Redis -> byte array -> Object -> cast to my class Member.来自 Redis 的 ByteString -> 字节数组 -> 对象 -> 转换为我的类成员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM