connect to kerbros enable hive from Windows machine

While connecting the hive installed in linux machin with kebros enabled from windows maching using jdbc driver we will face few issues with the kebros. 

 to address the kerbros issue we need to add the keytab file and kerbros conf file as well. 

add necessary jars and then add the krb.conf file to the  JVM argument 

-Djava.security.krb5.conf="C:\\MyFiles\krb5.conf"



Java Code:

package com.hive.connect;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.logging.Logger;
import org.apache.hadoop.security.UserGroupInformation;
public class HiveConnect {
private static final Logger logger = Logger.getLogger("com.hive.connect.HiveConnect ");
private static final String JDBC_DRIVER_NAME = "org.apache.hive.jdbc.HiveDriver";
private static String connectionUrl;

public static void main(String[] args) {


System.out.println("Staring HiveConnect");
if (args.length < 4) {
logger.severe("4 arg is required :\n\t- connectionurl Query keytab_user keytab_filepath");
System.err.println("4 arg is required :\n\t- connectionurl Query keytab_user keytab_filepath ");
System.exit(10);
}
// Get url Connection
connectionUrl = args[0];
String sqlStatementInsert = args[1];
String keytab_user = args[2];
String keytab_path = args[3];

// Init Connection
Connection con = null;
 
try {
/* System.setProperty("java.security.krb5.realm", "us.test.com");
        System.setProperty("java.security.krb5.kdc","server.test.com);
System.setProperty("java.security.krb5.conf", "C:\MyFiles\krb5.conf");*/
org.apache.hadoop.conf.Configuration conf = new org.apache.hadoop.conf.Configuration();
      conf.set("hadoop.security.authentication", "Kerberos");
      
      UserGroupInformation.setConfiguration(conf);
      //System.out.println("keytab_path :" +keytab_path);
      UserGroupInformation.loginUserFromKeytab(keytab_user, keytab_path);
      
// Set JDBC Hive Driver
Class.forName(JDBC_DRIVER_NAME);
// Connect to Hive
con = DriverManager.getConnection(connectionUrl,"hdfs","");
// Init Statement
Statement stmt = con.createStatement();
 
// Execute INSERT Query
boolean status = stmt.execute(sqlStatementInsert);
if (!status) {
logger.info("Insert into Hive table : OK");
}else {
logger.info("Insert into Hive table : FAILED");
}
 
} catch (Exception e) {
logger.severe(e.getMessage());
} finally {
try {
con.close();
} catch (Exception e) {
// swallow
}
}
}

 


}


Comments

Popular posts from this blog

Uninstalling eclipse plugins from command line

MS office installation