Redis– Jedis存储Java对象 - (Java序列化为byte数组方式) Java 序列化和反序列化



import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Created by Lovell on 16/7/2.
 */
public class SerializeUtil {

    /**
     * serialize Object
     * @param object
     * @return byte[]
     */
    public static byte[] serialize(final Object object) {
        ObjectOutputStream oos = null;
        ByteArrayOutputStream baos = null;
        try {
            baos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(baos);
            oos.writeObject(object);
            return baos.toByteArray();
        } catch (Exception e) {

        }
        return null;
    }

    /**
     * unserialize byte[]
     * @param bytes
     * @return Object
     */
    public static Object unserialize(final byte[] bytes) {
        ByteArrayInputStream bais = null;
        try {
            bais = new ByteArrayInputStream(bytes);
            ObjectInputStream ois = new ObjectInputStream(bais);
            return ois.readObject();
        } catch (Exception e) {
        
        } 
        return null;
    }

    /**
     * serialize hash Map<Object, Object>
     * @param hash
     * @return Map<byte[], byte[]>
     */
    public static Map<byte[], byte[]> serializehmoo2mbb(Map<Object, Object> hash) {
        Map<byte[], byte[]> result = new HashMap<byte[], byte[]>();
        try {
            Set<Object> keys = hash.keySet();
            if (keys != null && keys.size() > 0) {
                for (Object key : keys) {
                    result.put(serialize(key), serialize(hash.get(key)));
                }
            }
        } catch (Exception e) {
        }
        return result;
    }

    /**
     * unserialize hash Map<byte[], byte[]>
     * @param hash
     * @return Map<Object, Object>
     */
    public static Map<Object, Object> unserializehmbb2moo(final Map<byte[], byte[]> hash) {
        Map<Object, Object> result = new HashMap<Object, Object>();
        try {
            Set<byte[]> keys = hash.keySet();
            if (keys != null && keys.size() > 0) {
                for (byte[] key : keys) {
                    result.put(unserialize(key), unserialize(hash.get(key)));
                }
            }
        } catch (Exception e) {
        }
        return result;
    }

    /**
     * serialize hash Map<String, Object>
     * @param hash
     * @return Map<byte[], byte[]>
     */
    public static Map<byte[], byte[]> serializehmso2mbb(final Map<String, Object> hash) {
        Map<byte[], byte[]> result = new HashMap<byte[], byte[]>();
        try {
            Set<String> keys = hash.keySet();
            if (keys != null && keys.size() > 0) {
                for (String key : keys) {
                    result.put(serialize(key), serialize(hash.get(key)));
                }
            }
        } catch (Exception e) {
        }
        return result;
    }

    /**
     * unserialize hash Map<byte[], byte[]>
     * @param hash
     * @return Map<String, Object>
     */
    public static Map<String, Object> unserializehmbb2mso(final Map<byte[], byte[]> hash) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            Set<byte[]> keys = hash.keySet();
            if (keys != null && keys.size() > 0) {
                for (byte[] key : keys) {
                    result.put(unserialize(key).toString(), unserialize(hash.get(key)));
                }
            }
        } catch (Exception e) {
        }
        return result;
    }
}

写对象

Person对象写入Redis中:

publicvoid setObject() {

Person person = newPerson(100, "alan");

jedis.set("person:100".getBytes(),SerializeUtil.serialize(person));

person = new Person(101,"bruce");

jedis.set("person:101".getBytes(),SerializeUtil.serialize(person));

}

 

运行上面代码之后,我们到命令行窗口中读取该对象,看看有没有写入成功:

redis127.0.0.1:6379> get person:100

"\xac\xed\x00\x05sr\x00\x15alanland.redis.Person\x05\xf4\x8d9A\xf4`\xb0\x02\x00\x02I\x00\x02idL\x00\x04namet\x00\x12Ljava/lang/String;xp\x00\x00\x00dt\x00\x04alan"

 

可以取到序列化之后的值。

 

取对象

Jedis获取对象:

publicPerson getObject(int id) {

byte[] person =jedis.get(("person:" + id).getBytes());

return (Person)SerializeUtil.unserialize(person);

}

 

测试一下上一步存入的两个对象:

Personperson = test.getObject(100);

System.out.println(person.getId());

System.out.println(person.getName());

person= test.getObject(101);

System.out.println(person.getId());

System.out.println(person.getName());

 

Java控制台输入:

100

alan

101

bruce


你可能感兴趣的:(Redis– Jedis存储Java对象 - (Java序列化为byte数组方式) Java 序列化和反序列化)