jdbc连接hive2

CDH版本:5.15.1
依赖的jar包:


jdbc连接hive2_第1张图片
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();          
        }    
}

查询结果:


jdbc连接hive2_第2张图片
image.png

你可能感兴趣的:(jdbc连接hive2)