JDBC接続Hive(ClassNotFoundException、No suitable driver found、Could not open client transport wit)の使用

2723 ワード

package hive;

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

public class App {
    public static void main(String[] args) throws Exception {

        Class.forName("org.apache.hive.jdbc.HiveDriver");
        Connection conn = DriverManager.getConnection("jdbc:hive2://hadoop0:10000/default", "", "");
                                                // default   hive     
        Statement stmt = conn.createStatement();
        String querySQL="SELECT * FROM default.t1";

        ResultSet res = stmt.executeQuery(querySQL);  

        while (res.next()) {
        System.out.println(res.getInt(1));
        }
    }
}

接続時の3つの問題
JDBCがHiveに接続する際に発生する可能性のある問題は、主にhiveバージョンの違いによるものです.
  • (1)java.lang.ClassNotFoundException:org.apache.hadoop.hive.jdbc.HiveDriverソリューション:
    Class.forName("org.apache.hive.jdbc.HiveDriver");
    ではなく
    Class.forName("org.apache.hadoop.hive.jdbc.HiveDriver");
  • (2)java.sql.SQLException: No suitable driver found for jdbc:hive://localhost:10000/default hive 1.2.1ではjdbc:hive 2://localhost:100000/defaultが必要ですjdbcではありません:hive://localhost:10000/default
  • (3)java.sql.SQLException:Could not open client transport with JDBC Uri:jdbc:hive 2:///X.X.X:10000/default:java.net.ConnectException:Connection refused hive 1.2以上のバージョン、hiveは使用されず、hiveserver 2命令を直接使用します.Linux shell:
    [root@hadoop0 ~]# hiveserver2 &
  • References
    [1] Connect from Java to Hive using JDBC
    [2] java.sql.SQLException: No suitable driver found for jdbc:hive://localhost:10000/default
    [3] How to start hiveserver2 as service