Connect Hive through Java JDBC

后端 未结 6 655
北海茫月
北海茫月 2021-02-06 02:29

There is a question here connect from java to Hive but mine is different

My hive running on machine1 and I need to pass some queries using Java server

6条回答
  •  孤独总比滥情好
    2021-02-06 03:19

    For others wondering around about what exactly is required to execute remotely a HIVE query using java...

    Java code

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    public class Runner
    { 
            private static String driverName = "org.apache.hive.jdbc.HiveDriver";
            public static void main(String[] args) throws SQLException {
    
                    try {
                            // Register driver and create driver instance
                            Class.forName(driverName);
                    } catch (ClassNotFoundException ex) {
                          ex.printStackTrace();
                    }
    
                    // get connection
                    System.out.println("before trying to connect");
                    Connection con = DriverManager.getConnection("jdbc:hive2://[HOST IP]:10000/", "hive", "");
                    System.out.println("connected");
    
                    // create statement
                    Statement stmt = con.createStatement();
    
                    // execute statement
                    stmt.executeQuery("show tables");
    
                    con.close();
            }
    }
    

    Together with the pom file with the only required dependencies..

    
    
        4.0.0
    
        test-executor
        test-executor
        1.0-SNAPSHOT
        
            2.5.2
        
    
        
            org.apache.hive
            hive-exec
            1.2.1
        
        
            org.apache.hive
            hive-jdbc
            1.2.1
        
        
            org.apache.hadoop
            hadoop-hdfs
            ${hadoop.version}
        
        
            org.apache.hadoop
            hadoop-common
            ${hadoop.version}
        
    
    
    

提交回复
热议问题