首页 > 其他 > 详细

Hive进阶_Hive的客户端操作

时间:2017-01-24 19:19:58      阅读:317      评论:0      收藏:0      [点我收藏+]

启动远程客户端 # hive --service hiveserver2
获取连接-〉创建运行环境-〉执行HQL-〉处理结果-〉释放资源

技术分享
 1 package demo.utils;
 2 
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.sql.ResultSet;
 6 import java.sql.SQLException;
 7 import java.sql.Statement;
 8 
 9 
10 public class JDBCUtils {
11     private static String driverString = "org.apache.hive.jdbc.HiveDriver";
12     private static String urlString = "jdbc:hive2://sd-9c1f-2eac:10000/default";
13     static {
14         try {
15             Class.forName(driverString);
16         } catch (ClassNotFoundException e) {
17             throw new ExceptionInInitializerError(e);
18         }
19     }
20 
21 
22     public static Connection getConnection() {
23         try {
24             return DriverManager.getConnection(urlString);
25         } catch (SQLException e) {
26             e.printStackTrace();
27         }
28         return null;
29     }
30     
31     public static void release (Connection conn, Statement st, ResultSet rs){
32         if (rs!=null){
33             try{
34                 rs.close();
35             }catch(SQLException e){
36                 e.printStackTrace();
37             }finally{
38                 rs=null;
39             }
40         }
41         if (st!=null){
42             try{
43                 st.close();
44             }catch(SQLException e){
45                 e.printStackTrace();
46             }finally{
47                 st=null;
48             }
49         }
50         if (conn!=null){
51             try{
52                 conn.close();
53             }catch(SQLException e){
54                 e.printStackTrace();
55             }finally{
56                 conn=null;
57             }
58         }
59     }
60 
61 }
JDBCUtil.java

 

技术分享
 1 package demo.hive;
 2 
 3 import java.sql.Connection;
 4 import java.sql.ResultSet;
 5 import java.sql.SQLException;
 6 import java.sql.Statement;
 7 
 8 import demo.utils.JDBCUtils;
 9 
10 public class HiveJDBCDemo {
11     public static void main(String[] args) {
12         Connection conn=null;
13         Statement st=null;
14         ResultSet rs=null;
15         String sql="select * from sampledata";
16         conn=JDBCUtils.getConnection();
17         try {
18             st=conn.createStatement();
19             rs = st.executeQuery(sql);
20             while(rs.next()){
21                 String sid = rs.getString(1);
22                 String sname = rs.getString(2);
23                 String gender = rs.getString(3);
24                 System.out.println(sid+" "+sname+" "+gender);
25             }
26         } catch (SQLException e) {
27             e.printStackTrace();
28         }finally{
29             JDBCUtils.release(conn, st, rs);
30         }
31         
32     }
33 
34 
35 }
HiveJDBCDemo.java

 

Hive进阶_Hive的客户端操作

原文:http://www.cnblogs.com/liupuLearning/p/6347636.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!