从hive中获取数据
来源:互联网 发布:淘宝助理使用教程视频 编辑:程序博客网 时间:2024/05/17 06:11
MySQL中获取数据
public RestMsg<Object> getZhen( HttpServletRequest request) {RestMsg<Object> rm = new RestMsg<Object>(); List<String> list = new ArrayList();Connection conn = null;Statement stmt = null;ResultSet rs = null;try {//第一步:加载MySQL的JDBC的驱动Class.forName("com.mysql.jdbc.Driver");//取得连接的 url,能访问MySQL数据库的用户名,密码;数据库名String url = "jdbc:mysql://10.1.65.13:3306/yinzhoufuben?useUnicode=true&characterEncoding=utf-8";String user = "root";String password = "root";//第二步:创建与MySQL数据库的连接类的实例conn = DriverManager.getConnection(url, user, password);//第三步:用conn创建Statement对象类实例 stmtstmt = conn.createStatement();//第四步:执行查询,用ResultSet类的对象,返回查询的结果String sql = "select DISTINCT xzname from zoning WHERE xzname like '%街道' or xzname LIKE '%镇' or xzname LIKE '%乡'";rs = stmt.executeQuery(sql);while(rs.next()){System.out.println(rs.getString(1)); //取得数据库中的数据list.add(rs.getString("xzname"));}} catch (ClassNotFoundException e) {//加载JDBC错误,所要用的驱动没有找到System.out.println("驱动加载错误");}catch (SQLException ex) {//显示数据库连接错误或查询错误System.err.println("SQLException:"+ex.getMessage());} finally {try{if(rs != null) {rs.close();rs = null;}if(stmt != null) {stmt.close();stmt = null;}if(conn != null) {conn.close();conn = null;}}catch(SQLException e) {System.err.println("SQLException:"+e.getMessage());}}rm = rm.successMsg();rm.setResult(list);return rm;}
hive中获取数据
@Override public Map<String, Object> getResultBySelcet(String selectSql, String... field) { Map<String,Object> result = new HashMap<>(); Connection con = null; Statement stmt = null; ResultSet datSet = null; try{ Class.forName(jdbcHiveDriver); con = DriverManager.getConnection(jdbcHiveUrl, hiveUser, hivePwd); stmt = con.createStatement(); datSet = stmt.executeQuery(selectSql); if(datSet != null){ while(datSet.next()){ for(String name : field) { if (datSet.getString(name) != null) { result.put(name,datSet.getString(name)); } } break; } } }catch (Exception e){ e.printStackTrace(); }finally { try { if (datSet != null) { datSet.close(); } if(stmt != null){ stmt.close(); } if(con != null){ con.close(); } }catch (Exception e){ e.printStackTrace(); } } return result; } @Override public List<Map<String, Object>> getAllResultBySelcet(String selectSql, String... field) { List<Map<String,Object>> result = new ArrayList<>(); Connection con = null; Statement stmt = null; ResultSet rs = null; try { Class.forName(jdbcHiveDriver); con = DriverManager.getConnection(jdbcHiveUrl, hiveUser, hivePwd); stmt = con.createStatement(); rs = stmt.executeQuery(selectSql); while (rs.next()) { Map<String,Object> temp = new HashMap<>(); for(String name : field) { if (rs.getString(name) != null) { temp.put(name,rs.getString(name)); } } result.add(temp); } rs.close(); }catch (Exception e){ e.printStackTrace(); } finally { try { if (rs != null) { rs.close(); } if(stmt != null){ stmt.close(); } if(con != null){ con.close(); } }catch (Exception e){ e.printStackTrace(); } } return result; }
阅读全文
0 0
- 从hive中获取数据
- hive从查询中获取数据插入到表或动态分区
- hive从查询中获取数据插入到表或动态分区
- hive从查询中获取数据插入到表或动态分区
- hive从查询中获取数据插入到表或动态分区
- hive从查询中获取数据插入到表或动态分区
- hive从查询中获取数据插入到表或动态分区
- hive从查询中获取数据插入到表或动态分区
- sqoop从hive中导出oracle数据
- Hive数据仓库-Sqoop将数据从Mysql导入Hive中
- 从xml中获取数据
- 从store中获取数据
- 从配置文件中获取数据
- 从store中获取数据
- 从store中获取数据
- 从txt文本中导入数据到Hive中
- 从数据窗口中获取数据
- SQOOP中从mysql导入数据到hive中报错解决方法
- 微信公众号 自定义菜单创建接口
- 2017暑假训练第二十天
- iOS_Runtime5_消息转发
- 众数问题
- View 添加双击事件
- 从hive中获取数据
- 如何关闭CentOS7上的firewall防火墙
- ELK+Kafka集群日志分析系统
- 代码实现高低电平反转或0-1连续输出
- 出题
- 何恺明相关工作小结
- Java为什么选择unicode字符集?字符编码的那些事
- error: jump to label ‘XXXX’ [-fpermissive]
- LeetCode-61. Rotate List