jdbc读取Gzip编码后的Blob类型的数据

azkaban库中的project_flows 表中的 json 字段是Blob类型的数据,而且经过Gzip 编码,还原过程示例:

public static void main(String[] args) {
    String SELECT_ALL_PROJECT_FLOWS =
            "SELECT project_id, version, flow_id, modified_time, encoding_type, json " +
                    "FROM project_flows WHERE project_id=283 AND version=1";
    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql://xxxx:3306/azkaban", "root", "root");
        PreparedStatement pstm = connection.prepareStatement(SELECT_ALL_PROJECT_FLOWS);
        ResultSet rs = pstm.executeQuery();
        while (rs.next()) {
            String flowId = rs.getString(3);
            int encodingType = rs.getInt(5);
            byte[] dataBytes = rs.getBytes(6);
            ByteArrayInputStream byteInputStream = new ByteArrayInputStream(dataBytes);
            GZIPInputStream gzipInputStream = new GZIPInputStream(byteInputStream);

            ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
            IOUtils.copy(gzipInputStream, byteOutputStream);
            String s = new String(byteOutputStream.toByteArray(), "UTF-8");
            System.out.println(s);
        }
    } catch (ClassNotFoundException | SQLException | IOException e) {
        e.printStackTrace();
    }
}
上一篇:blob是什么类型?


下一篇:MySQL数据类型分类