Redis重新组合序列化字符串失败 - UTF-8

时间:2014-04-30 02:50:52

标签: serialization encoding utf-8 character-encoding redis

我正在与Redis合作。

我有2个项目,一个插入数据,另一个是选择。

我通过org.springframework.data.redis.RedisTemplate放了一些键/字段/值。项目编码是UTF-8。 密钥和字段为String类型,值为Object

我想要做的是从远程数据库(插入项目)中选择数据并插入自己的redis。获取数据时,使用Jedis并使用模板。

问题。

  • 当我插入到插入项目时,我已经序列化了String和Key。
  • 然而,当我在另一个中选择时,它会中断。
  • 使用控制台(redis-cli)做出正确答案。

这是我的源代码。有什么需要考虑的吗?谢谢:

@Repository
public class RedisDuplicator implements RedisDecalcomanie {

    @Inject
    RedisManagerImpl serviceRedis;

    @Override
    public void duplicateHash(String key) throws Exception {
        RedisHepler helper = RedisHepler.getInstance();
        Jedis managerRedis = helper.getConnection();

        Map<byte[], byte[]> hashAll = managerRedis.hgetAll(key.getBytes());

        Iterator<byte[]> iter = hashAll.keySet().iterator();
        while(iter.hasNext()){
            byte[] field = iter.next();
            byte[] value = hashAll.get(field);

            System.out.println("REDIS DUPLICATOR - ORIGIN : " + field);
            System.out.println("REDIS DUPLICATOR : " + new String(field, "ISO-8859-1"));
//          System.out.println("\t[ val ] : " + new String(value));

            System.out.println("utf-8 -> euc-kr        : " + new String(field, "euc-kr"));
            System.out.println("utf-8 -> ksc5601       : " + new String(field, "ksc5601"));
            System.out.println("utf-8 -> x-windows-949 : " + new String(field, "x-windows-949"));
            System.out.println("utf-8 -> iso-8859-1    : " + new String(field, "iso-8859-1"));
            System.out.println("iso-8859-1 -> euc-kr        : " + new String(field, "euc-kr"));
            System.out.println("iso-8859-1 -> ksc5601       : " + new String(field, "ksc5601"));
            System.out.println("iso-8859-1 -> x-windows-949 : " + new String(field, "x-windows-949"));
            System.out.println("iso-8859-1 -> utf-8         : " + new String(field, "utf-8"));
            System.out.println("euc-kr -> utf-8         : " + new String(field, "utf-8"));
            System.out.println("euc-kr -> ksc5601       : " + new String(field, "ksc5601"));
            System.out.println("euc-kr -> x-windows-949 : " + new String(field, "x-windows-949"));
            System.out.println("euc-kr -> iso-8859-1    : " + new String(field, "iso-8859-1"));
            System.out.println("ksc5601 -> euc-kr        : " + new String(field, "euc-kr"));
            System.out.println("ksc5601 -> utf-8         : " + new String(field, "utf-8"));
            System.out.println("ksc5601 -> x-windows-949 : " + new String(field, "x-windows-949"));
            System.out.println("ksc5601 -> iso-8859-1    : " + new String(field, "iso-8859-1"));
            System.out.println("x-windows-949 -> euc-kr     : " + new String(field, "euc-kr"));
            System.out.println("x-windows-949 -> utf-8      : " + new String(field, "utf-8"));
            System.out.println("x-windows-949 -> ksc5601    : " + new String(field, "ksc5601"));
            System.out.println("x-windows-949 -> iso-8859-1 : " + new String(field, "iso-8859-1"));
        }

        helper.returnResource(managerRedis);
        helper.destroyPool();
    }

    @Override
    public void duplicateList(String key) throws Exception {
        // TODO Auto-generated method stub

    }
}; 

输出:

REDIS DUPLICATOR - ORIGIN : [B@3d3c33b7
REDIS DUPLICATOR : ’t160
utf-8 -> euc-kr        : ыt160
utf-8 -> ksc5601       : ыt160
utf-8 -> x-windows-949 : ыt160
utf-8 -> iso-8859-1    : ’t160
iso-8859-1 -> euc-kr        : ыt160
iso-8859-1 -> ksc5601       : ыt160
iso-8859-1 -> x-windows-949 : ыt160
iso-8859-1 -> utf-8         : ��t160
euc-kr -> utf-8         : ��t160
euc-kr -> ksc5601       : ыt160
euc-kr -> x-windows-949 : ыt160
euc-kr -> iso-8859-1    : ’t160
ksc5601 -> euc-kr        : ыt160
ksc5601 -> utf-8         : ��t160
ksc5601 -> x-windows-949 : ыt160
ksc5601 -> iso-8859-1    : ’t160
x-windows-949 -> euc-kr     : ыt160
x-windows-949 -> utf-8      : ��t160
x-windows-949 -> ksc5601    : ыt160
x-windows-949 -> iso-8859-1 : ’t160
REDIS DUPLICATOR - ORIGIN : [B@4b0e18ba
REDIS DUPLICATOR : ’t130
utf-8 -> euc-kr        : ыt130
utf-8 -> ksc5601       : ыt130
utf-8 -> x-windows-949 : ыt130
utf-8 -> iso-8859-1    : ’t130
iso-8859-1 -> euc-kr        : ыt130
iso-8859-1 -> ksc5601       : ыt130
iso-8859-1 -> x-windows-949 : ыt130
iso-8859-1 -> utf-8         : ��t130
euc-kr -> utf-8         : ��t130
euc-kr -> ksc5601       : ыt130
euc-kr -> x-windows-949 : ыt130
euc-kr -> iso-8859-1    : ’t130
ksc5601 -> euc-kr        : ыt130
ksc5601 -> utf-8         : ��t130
ksc5601 -> x-windows-949 : ыt130
ksc5601 -> iso-8859-1    : ’t130
x-windows-949 -> euc-kr     : ыt130
x-windows-949 -> utf-8      : ��t130
x-windows-949 -> ksc5601    : ыt130
x-windows-949 -> iso-8859-1 : ’t130

我预计其中一些应该是'130'。

1 个答案:

答案 0 :(得分:0)

它出现了我的控制台编码问题。由于我的Eclipse工作区设置是默认设置,因此无法打印UTF-8文本:D(但为什么它无法打印iso-8859-1编码仍然很神秘......)