美文网首页
jdbc连接hive2

jdbc连接hive2

作者: 圈半球 | 来源:发表于2019-03-19 14:28 被阅读0次

CDH版本:5.15.1
依赖的jar包:


image.png
package hive;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Hive2Demo {
        private static String driverName = "org.apache.hive.jdbc.HiveDriver";
        private static String connctUrl = "jdbc:hive2://10.43.250.89:10000";
        private static String userName = "";
        private static String password = "";
     
        public static void main(String[] args) throws SQLException {
            
            try {
                Class.forName(driverName);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
                System.exit(1);
            }
     
            Connection con = DriverManager.getConnection(connctUrl, userName, password);
            Statement stmt = con.createStatement();
           
           
                String sql ="select * from sjzx_dm.dm_td_gg limit 10 ";
                PreparedStatement ps = con.prepareStatement(sql);
                      
                ResultSet res = ps.executeQuery();
                int col = res.getMetaData().getColumnCount();
                System.out.println("=====================================");
                while (res.next()){
                    for(int i=1;i<=col;i++){
                        System.out.print(res.getString(i)+"\t");
                    }
                    System.out.print("\n");
                }
                    
            stmt.close();
            con.close();          
        }    
}

查询结果:


image.png

相关文章

网友评论

      本文标题:jdbc连接hive2

      本文链接:https://www.haomeiwen.com/subject/djbgmqtx.html