Java JDBC Hive Connection getConnection()

Here you can find the source of getConnection()

Description

get Connection

License

Apache License

Declaration

public static Connection getConnection() 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.security.PrivilegedExceptionAction;
import java.sql.*;
import javax.security.auth.Subject;

public class Main {
    public static String driver = "org.apache.hive.jdbc.HiveDriver";
    public static String url = "jdbc:hive2://172.16.19.156:10000/default";

    public static Connection getConnection() {
        try {/*from   w  w  w . j a  va2  s. c om*/
            Class.forName(driver);
            return DriverManager.getConnection(url, "", "");
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    static Connection getConnection(Subject signedOnUserSubject) throws Exception {
        Connection conn = (Connection) Subject.doAs(signedOnUserSubject, new PrivilegedExceptionAction<Object>() {
            public Object run() {
                Connection con = null;
                String JDBC_DB_URL = "jdbc:hive2://HiveHost:10000/default;"
                        + "principal=hive/localhost.localdomain@EXAMPLE.COM;" + "kerberosAuthType=fromSubject";
                try {
                    Class.forName(driver);
                    con = DriverManager.getConnection(JDBC_DB_URL);
                } catch (SQLException e) {
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
                return con;
            }
        });
        return conn;
    }
}

Related

  1. getConnection()