解析表字段内容为Gzip格式的数据
来源:互联网 发布:三菱q系列plc编程手册 编辑:程序博客网 时间:2024/05/29 10:51
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; }}
阅读全文
0 0
- 解析表字段内容为Gzip格式的数据
- Android 解析gzip格式数据
- contacts表字段解析
- 修改有数据的表字段
- sql 查询表字段相同的数据
- Excel2007数据透视表字段设置按钮为灰色
- mysql数据库替换表字段包含的某内容
- Imp 数据后,表字段的comments 中文显示为乱码,而检索表数据的中文无乱码
- Imp 数据后,表字段的comments 中文显示为乱码,而检索表数据的中文无乱码
- 为表字段增加 not null约束的风险
- A表字段更新为B表的字段值
- 数据库的表字段自动转换为java实体类
- java解析gzip格式的数据包
- 增加 大量数据 表字段
- 自建表字段不用建数据元素的方法
- 修改已有数据的oracle表字段长度
- sqlserver 复制表字段和数据的方法
- MSSQL触发器记录表字段数据变化的日志
- Macos终端获root权限
- codeforces 822D
- 二分查找、二叉搜索
- JDBC技术连接数据库
- #pragma once与#ifndef两种防止头文件二次编译的区别
- 解析表字段内容为Gzip格式的数据
- mqtt消息推送-Mosquitto安装
- 用gcc编译成可执行程序
- Redis之list函数
- 微信小程序开发之radio实现显示和隐藏功能
- java Object 全解析(未完待续)
- SQL语句的执行过程(以oracle为例)
- 字符串 和 模式匹配(python学习手册4)
- jquery jqGrid colModel 某一列添加超链接