解析表字段内容为Gzip格式的数据

import org.apache.commons.io.IOUtils;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.map.ObjectMapper;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.zip.GZIPInputStream;

/**
 * Created by hnx on 2017/7/4.
 */
public class MysqlTest {
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://localhost:3306/test";
    static final String USER = "root";
    static final String PASS = "root";
    static final String SQL = "SELECT id, project_name, flow_id, enc_type, data FROM execution_options";

    /**
     * @param args
     */
    public static void main(String[] args) throws SQLException {
        Connection conn = null;
        Statement stmt = null;
        try {
            conn = getConnection();
            stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(SQL);
            System.out.println(rs.next());
            while (rs.next()) {
                int id = rs.getInt(1);
                String projectName = rs.getString(2);
                String flowId = rs.getString(3);
                int encodingType = rs.getInt(4);
                byte[] data = rs.getBytes(5);
                String jsonObj = null;
                if (data != null) {
                    String jsonString = unGzipString(data, "UTF-8");
                    jsonObj = parseJSONFromString(jsonString).toString();
                    System.out.println(jsonObj);
                }
            }


            // System.out.println("Goodbye!");
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            conn.close();
        }
    }

    public static byte[] unGzipBytes(byte[] bytes) throws IOException {
        ByteArrayInputStream byteInputStream = new ByteArrayInputStream(bytes);
        GZIPInputStream gzipInputStream = new GZIPInputStream(byteInputStream);

        ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
        IOUtils.copy(gzipInputStream, byteOutputStream);

        return byteOutputStream.toByteArray();
    }

    public static String unGzipString(byte[] bytes, String encType)
            throws IOException {
        byte[] response = unGzipBytes(bytes);
        return new String(response, encType);
    }


    public static Object parseJSONFromString(String json) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        JsonFactory factory = new JsonFactory();
        JsonParser parser = factory.createJsonParser(json);
        JsonNode node = mapper.readTree(parser);

        return toObjectFromJSONNode(node);
    }

    private static Object toObjectFromJSONNode(JsonNode node) {
        if (node.isObject()) {
            HashMap obj = new HashMap();
            Iterator iter = node.getFieldNames();
            while (iter.hasNext()) {
                String fieldName = (String)iter.next();
                JsonNode subNode = node.get(fieldName);
                Object subObj = toObjectFromJSONNode(subNode);
                obj.put(fieldName, subObj);
            }

            return obj;
        } else if (node.isArray()) {
            ArrayList array = new ArrayList();
            Iterator iter = node.getElements();
            while (iter.hasNext()) {
                JsonNode element = (JsonNode)iter.next();
                Object subObject = toObjectFromJSONNode(element);
                array.add(subObject);
            }
            return array;
        } else if (node.isTextual()) {
            return node.asText();
        } else if (node.isNumber()) {
            if (node.isInt()) {
                return node.asInt();
            } else if (node.isLong()) {
                return node.asLong();
            } else if (node.isDouble()) {
                return node.asDouble();
            } else {
                System.err.println("ERROR What is this!? " + node.getNumberType());
                return null;
            }
        } else if (node.isBoolean()) {
            return node.asBoolean();
        } else {
            return null;
        }
    }

    private static Connection getConnection() {
        Connection conn = null;
        try {
            Class.forName(JDBC_DRIVER);
            conn = DriverManager.getConnection(DB_URL, USER, PASS);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值