0

This is the Putty image:

enter image description here

I tried to connect to my Putty and was able to connect, then trying to connect to Hive with JDBC, but still facing the same connection issue. Here's my code.

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

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

public class SSHFile2 {

    static int lport;
    static String rhost;
    static int rport;
    public static void go(){
        String user = "ec2-user";
        //String password = "";
        String host = "18.188.88.49";
        int port=22;
        try
            {
            JSch jsch = new JSch();
            Session session = jsch.getSession(user, host, port);
            lport = 4321;
            rhost = "localhost";
            rport = 10000;
            //session.setPassword(password);
            session.setConfig("StrictHostKeyChecking", "no");
            System.out.println("Establishing Connection...");
            session.connect();
            int assinged_port=session.setPortForwardingL(lport, rhost, rport);
            System.out.println("localhost:"+assinged_port+" -> "+rhost+":"+rport);
            }
        catch(Exception e){System.err.print(e);}
    }
    public static void main(String[] args) {
        try{
            go();
        } catch(Exception ex){
            ex.printStackTrace();
        }
          System.out.println("An example for updating a Row from Hive Database!");
          Connection con = null;
          String driver = "org.apache.hive.jdbc.HiveDriver";
          String url = "jdbc:hive2://" + rhost +":" + lport + "/";
          String db = "sahoo";
          String dbUser = "hive";
          String dbPasswd = "";
          try{
          Class.forName(driver);
          con = DriverManager.getConnection(url+db, dbUser, dbPasswd);
          try{
          Statement st = con.createStatement();
          System.out.println("Connected to Hive");
          /*String sql = "UPDATE MyTableName " +
                  "SET email = 'ripon.wasim@smile.com' WHERE email='peace@happy.com'";

          int update = st.executeUpdate(sql);
          if(update >= 1){
          System.out.println("Row is updated.");
          }
          else{
          System.out.println("Row is not updated.");
          }
*/          }
          catch (SQLException s){
          System.out.println("SQL statement is not executed!");
          }
          }
          catch (Exception e){
          e.printStackTrace();
          }
          }

}

After this I got this message. Although I'm not aware of the Lport, what to use exactly for Hive.

This is the error report:

com.jcraft.jsch.JSchException: Auth failApr 18, 2019 4:56:00 PM org.apache.hive.jdbc.Utils parseURL
INFO: Supplied authorities: localhost:4321
Apr 18, 2019 4:56:00 PM org.apache.hive.jdbc.Utils parseURL
INFO: Resolved authority: localhost:4321
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".

INFO: Transport Used for JDBC connection: null
java.sql.SQLException: Could not open client transport with JDBC Uri: jdbc:hive2://localhost:4321/sahoo: java.net.ConnectException: Connection refused: connect
    at org.apache.hive.jdbc.HiveConnection.openTransport(HiveConnection.java:231)
    at org.apache.hive.jdbc.HiveConnection.<init>(HiveConnection.java:176)
    at org.apache.hive.jdbc.HiveDriver.connect(HiveDriver.java:105)
    at java.sql.DriverManager.getConnection(Unknown Source)
halfer
  • 19,824
  • 17
  • 99
  • 186
Sayan Sahoo
  • 87
  • 3
  • 10
  • You seem to have a false idea, that by opening SSH connection in JSch, the database client will magically be able to connect to a remote database, whoch is not directly accessible. It won't. – Martin Prikryl Apr 18 '19 at 06:59
  • No I thought if I can connect to the SSH connection, my hive environment is also setup there only, so it might be a help. Can you please help me in this connection? your mentioned link didn't work for me. – Sayan Sahoo Apr 18 '19 at 07:16
  • I do not understand the explanation in your comment -- Anyway, *"didn't work for me"* does not work for us as a problem description. Did you try the code from the answer by @RiponAlWasim? – Martin Prikryl Apr 18 '19 at 08:01
  • No, I tried the first one. Can you tell me what will be my localport for Hive? Remote port will be 10000, but what will be my lport? @MartinPrikryl – Sayan Sahoo Apr 18 '19 at 09:09
  • I've updated the question in the comment, can you please tell me where I'm doing wrong? I'm new to this thing, that's why not aware much. please need help much. @MartinPrikryl – Sayan Sahoo Apr 18 '19 at 09:34
  • Note the `rhost = "localhost"` in the `url`, while you use `host = "18.188.88.49"`. – Martin Prikryl Apr 18 '19 at 10:36
  • Because it's hosted in that ip address, 18.188.88.49, it's an ec2 linux instance. – Sayan Sahoo Apr 18 '19 at 10:44
  • 1
    Sure, the same way the database is actually on `myhost.ripon.wasim` in the code by @RiponAlWasim . But you need to connect to the **local end** of the SSH tunnel, which leads to `18.188.88.49` on the **remote** end. Just do, what the answer by @RiponAlWasim does. – Martin Prikryl Apr 18 '19 at 10:46
  • I have posted a simple example there: https://stackoverflow.com/q/1968293/850848#55744561 – Martin Prikryl Apr 18 '19 at 10:57
  • int forwardedPort = jumpboxSession.setPortForwardingL(0, databaseHost, databasePort); you have added this line in your code. What will be databaseHost and databasePort for me? databasePort = 10000 maybe, and databaseHost should be localhost? or that ip? @MartinPrikryl – Sayan Sahoo Apr 18 '19 at 11:07
  • As my answer says *"The hostname/IP address and port, you would use **on the SSH server** to connect to the database."* + It should be `**session**.setPort...` - It's corrected already in the answer. – Martin Prikryl Apr 18 '19 at 11:08
  • String databaseHost = "database.example.com"; And what about this? i didn't get this part. @MartinPrikryl – Sayan Sahoo Apr 18 '19 at 11:14
  • I cannot answer that for you. You have to know that. How do you connect to the database now? – Martin Prikryl Apr 18 '19 at 11:15
  • If the database runs on the same machine as your SSH server, then use `localhost`. – Martin Prikryl Apr 18 '19 at 11:16
  • Okay @MartinPrikryl, I've edited the question. Can you tell me now where I'm doing the mistake. I will add your part also if you want. – Sayan Sahoo Apr 18 '19 at 11:28
  • There are two different error messages in your question. Which one are you getting now? – Martin Prikryl Apr 18 '19 at 11:30
  • If would also help, if you use my simple code. It way easier to follow than the one by @RiponAlWasim. + Also show us how you connect with PuTTY. – Martin Prikryl Apr 18 '19 at 11:31
  • Both. first that JSch exception then ConnectionException. @MartinPrikryl – Sayan Sahoo Apr 18 '19 at 11:33
  • ohh sorry I forgot to add the method addIdentity(), when I added that for Jsch, that JSch exception is not there now. Only ConnectionException is still there. @MartinPrikryl – Sayan Sahoo Apr 18 '19 at 11:35
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/192045/discussion-between-sayan-sahoo-and-martin-prikryl). – Sayan Sahoo Apr 18 '19 at 11:38

0 Answers0