如何通过eclipse连接到emr上的hive?

fnvucqvd  于 2021-06-26  发布在  Hive
关注(0)|答案(1)|浏览(375)

我正试图通过eclipse连接到emr上的hive,但出现了一个错误。

Exception in thread "main" java.sql.SQLException: Could not open client transport with JDBC Uri: jdbc:hive2://localhost:8158: java.net.ConnectException: Connection refused
    at org.apache.hive.jdbc.HiveConnection.openTransport(HiveConnection.java:215)
    at org.apache.hive.jdbc.HiveConnection.<init>(HiveConnection.java:163)
    at org.apache.hive.jdbc.HiveDriver.connect(HiveDriver.java:105)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at com.readypulse.sparkanalytics.HiveQLConnector.<init>(HiveQLConnector.java:31)
    at com.readypulse.sparkanalytics.HiveQLConnector.main(HiveQLConnector.java:83)
Caused by: org.apache.thrift.transport.TTransportException: java.net.ConnectException: Connection refused
pbwdgjma

pbwdgjma1#

你需要一个ssh隧道来戳一个洞给emr主机。然后通过jdbc连接。

private static String driverName = "org.apache.hive.jdbc.HiveDriver";
private static String hiveConnectionString = "jdbc:hive2://localhost:8158";

//Need port forwarding
SparkPortForwarding.portForwardForSpark();

Class c = Class.forName(driverName);
Connection connection = DriverManager.getConnection(hiveConnectionString,
                "user", "pwd");
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(sql);

您可以通过shell进行端口转发,如下所示:

ssh -i ~/mykey.pem -N -L 8158:<maserhost>:10000 hadoop@<masterhost>

或者可以使用jsch库来使用代码

public static void portForwardForSpark() {
    try {
        if(session != null && session.isConnected()) {
            return;
        }

        JSch jsch = new JSch();
        jsch.addIdentity(PATH_TO_SSH_KEY_PEM);
        String host = REMOTE_HOST;
        session = jsch.getSession(USER, host, 22);

        // username and password will be given via UserInfo interface.
        UserInfo ui = new MyUserInfo();
        session.setUserInfo(ui);

        session.connect();
        int assingedPort = session.setPortForwardingL(LPORT, RHOST, RPORT);
        System.out.println("Port forwarding done for the post : " + assingedPort);
    } catch (Exception e) {
        System.out.println(e);
    }
}

相关问题