首页 > 其他分享 >关于压缩后字符串写入clickhouse再读取后无法反解压的问题

关于压缩后字符串写入clickhouse再读取后无法反解压的问题

时间:2023-02-21 16:34:56浏览次数:33  
标签:解压 读取 encode new byte getBytes clickhouse String


我们将一个长字符串进行了压缩,采用zstd或者snappy之类的,将字符串压成了byte[],然后将byte[]作为一个属性写入了clickhouse数据库,clickhouse会默认将byte[]转为String进行存储。

但是当从数据库读取到该字段,得到一个String类型的值,再用getBytes()方法获取到byte[],再试图用zstd的反解压功能对该byte[]试图还原为压缩前的字符串时,会发现报错,已经无法解压还原了。

关于压缩后字符串写入clickhouse再读取后无法反解压的问题_数据库

 对应该图的情形,运行会报错

关于压缩后字符串写入clickhouse再读取后无法反解压的问题_数据库_02

 那么做了如下修改,设置编码方式后,即可

关于压缩后字符串写入clickhouse再读取后无法反解压的问题_字段_03

String encode = "ISO8859-1";

String s = "FilterRegistrationBean registration = new FilterRegistrationBean();";
byte[] bytes = ZstdUtils.compress(s.getBytes(encode));


String ss = new String(bytes, encode);


byte[] sss = ZstdUtils.decompressBytes(ss.getBytes(encode));
System.out.println(new String(sss));

 

标签:解压,读取,encode,new,byte,getBytes,clickhouse,String
From: https://blog.51cto.com/u_13706148/6076708

相关文章