栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

spring boot 集成gzip和zip数据压缩传输(适用大数据信息传输)

spring boot 集成gzip和zip数据压缩传输(适用大数据信息传输)

1、背景

        在查询数据库信息的时候,由于数据库信息返回数据条数较多,数据从服务器端传至客户端耗费大量时间,导致查询数据变慢。

2、方案思路

        1)、从查询sql上入手,进行sql优化;

        2)、从业务层面优化,复杂接口拆分成多个接口,避免大量数据堆积返回(视业务需求而定);

        3)、对返回的大数据信息进行数据压缩。(本文要点)

        

3、压缩数据方案

        1)、gzip压缩

        2)、zip压缩

4、具体实现 (1)、gzip压缩方案

    GzipUtils工具类

package com.自己的包.util;

import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;


@Component
public class GzipUtils {
    
    public byte[] compress(byte[] data) throws IOException {
        if (data == null || data.length == 0) {
            return null;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        GZIPOutputStream gzip = new GZIPOutputStream(out);
        gzip.write(data);
        gzip.close();
        return out.toByteArray();
    }

    
    public byte[] compress(String str) throws IOException {
        if (str == null || str.length() == 0) {
            return null;
        }
        return compress(str.getBytes(StandardCharsets.UTF_8));
    }

    
    public byte[] uncompress(byte[] data) throws IOException {
        if (data == null || data.length == 0) {
            return data;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayInputStream in = new ByteArrayInputStream(data);
        GZIPInputStream gunzip = new GZIPInputStream(in);
        byte[] buffer = new byte[1024];
        int n;
        while ((n = gunzip.read(buffer)) >= 0) {
            out.write(buffer, 0, n);
        }
        gunzip.close();
        in.close();
        return out.toByteArray();
    }

    
    public String uncompress(String str) throws IOException {
        if (str == null || str.length() == 0) {
            return str;
        }
        byte[] data = uncompress(str.getBytes(StandardCharsets.ISO_8859_1));
        return new String(data);
    }
}

数据压缩

    @Autowired
    private GzipUtils gzipUtils;

    @RequestMapping(value = "testGzip", method = RequestMethod.POST)
    public JSonBeansResponse testGzip(@RequestBody Map map) throws IOException {
        if (null != map) {
            String sqlStr = map.get("paramStr");
            // 调用数据库获取数据
            Map resMap = testMapper.findInfo(sqlStr);
            String dataStr = JSONObject.toJSonString(resMap);

            // 开始压缩数据
            byte[] compress1 = gzipUtils.compress(dataStr);
            String FileBuf = base64.getEncoder().encodeToString(compress1);
            return new JSONBeansResponse<>(FileBuf);
        }
        return new JSONBeansResponse<>(new ArrayList<>(0));
    }

数据解压

    @RequestMapping(value = "testUnGzip", method = RequestMethod.POST)
    public JSonBeansResponse testUnGzip(@RequestBody Map map) throws IOException {
        if (null != map) {
            String dataStream = map.get("dataStream ");
            byte[] decode = base64.getDecoder().decode(dataStream);
            byte[] compress1 = gzipUtils.uncompress(decode);
            String dataStr = new String(compress1);
            Map res = JSONObject.parseObject(dataStr, Map.class);
            return new JSONBeansResponse<>(res);
        }
        return new JSONBeansResponse<>(new ArrayList<>(0));
    }

遇到问题

解压时候报错:java.util.zip.ZipException: Not in GZIP format

解决方案:在转换为字符串时,一定要使用ISO-8859-1这样的单字节编码

 

(2)、zip压缩方案

             待续。。。。。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/630013.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号