前几日使用jdbc连接hive时,由于特殊的使用需求,感觉resultset对接下来的数据解析不是很方便,于是将其转换成了一个自定义数据结构[{Score=80.0, UserName=张三, Subject=语文}, {Score=90.0, UserName=张三, Subject=数学}]
仅作记录使用,简单的jdbc连接器--使用构造器的形式
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 本demo为hiveJDBC连接的另一种形式,以构造器的方式建立连接
*
* 功能:
* 1、连接hive
* 2、执行无返回值类SQL
* 3、执行带返回结果集类SQL
* 4、将resultSet转成list List<Map<String, Object>>
*
*/
public class HiveJDBCUtils {
private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";
private String url = "";
private String user = "";
private String pwd = "";
private Connection con;
private Statement stmt;
HiveJDBCUtils(String url) {
this.url = url;
}
//为测试改为public,后续改回
public HiveJDBCUtils(String url, String user, String pwd) {
this.url = url;
this.user = user;
this.pwd = pwd;
//该步即已创建连接
this.conn();
}
private void conn () {
try {
Class.forName(driverName);
con = DriverManager.getConnection(url, user, pwd);
stmt = con.createStatement();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e){
e.printStackTrace();
}
}
/**
* 执行无返回值类SQL
* @param sql
* @throws SQLException
*/
public void executeSql (String sql) throws SQLException {
try {
stmt.execute(sql);
} catch (SQLException e){
e.printStackTrace();
}
}
/**
* 执行带返回结果集类SQL
* @param sql
* @return
* @throws SQLException
*/
public ResultSet executeQuerySql (String sql) throws SQLException {
ResultSet res = null;
try {
res = stmt.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return res;
}
/**
* 按需定制,可有可无,随意,为了更方便处理,也可不用
* 将resultSet转成list List<Map<String, Object>>
*
* 如不用此方法转换,则提取数据直接使用ResultSet.getInt,getString...即可
* 问题是会存在前端不好显示的问题,因为SQL是多样化的,列名也是多样化的,不方便为前端传过去
* [{Score=80.0, UserName=张三, Subject=语文}, {Score=90.0, UserName=张三, Subject=数学}]
* @param rs
* @return
* @throws SQLException
*/
public List<Map<String, Object>> resultSetToListMap(ResultSet rs) throws SQLException {
if(rs == null) return null;
List<Map<String, Object>> result = new ArrayList<Map<String,Object>>();
//获取结果集结构信息,字段类型、数量
ResultSetMetaData rsmd = null;
int cols = 0;
try {
rsmd = rs.getMetaData();
cols = rsmd.getColumnCount();
} catch (SQLException e) {
e.printStackTrace();
}
Map<String, Object> map = new HashMap<String, Object>();
while(rs.next()) {
map = new HashMap<String, Object>();
//将一条数据库表记录转为map存储
for (int i = 1; i <= cols; i++) {
map.put(rsmd.getColumnName(i), rs.getObject(i));
}
result.add(map);
}
return result;
}
}
Scala版本
/**
* 将resultSet转成list List<Map<String, Object>>
* [{Score=80.0, UserName=张三, Subject=语文}, {Score=90.0, UserName=张三, Subject=数学}]
* 使用是遍历,直接使用列名获取即可
*
* @param rs
* @return
*/
def resultSetToListMap(rs: ResultSet): List[Map[String, Any]] = {
if (rs == null) return null;
// var resultList:List[Map[String, Any]] = List()
var resultList:ListBuffer[Map[String, Any]] = ListBuffer()
//获取结果集结构信息,字段类型、数量
var rsmd: ResultSetMetaData = null
var cols = 0
try {
rsmd = rs.getMetaData
cols = rsmd.getColumnCount
} catch {
case e: SQLException =>
e.printStackTrace()
}
var map: Map[String, Any] = Map()
while (rs.next) {
//将一条数据库表记录转为map存储
for (i <- 1 to cols) {
map += (rsmd.getColumnName(i) -> rs.getObject(i))
}
//map +: resultList
resultList += map
}
resultList.toList
}