Here is the code to connect to hive in kerberos mode
import java.sql.*;
import org.apache.hadoop.security.UserGroupInformation;
public class hive2 {
public static void main (String args[]) {
try {
org.apache.hadoop.conf.Configuration conf = new org.apache.hadoop.conf.Configuration();
conf.set("hadoop.security.authentication", "Kerberos");
UserGroupInformation.setConfiguration(conf);
UserGroupInformation.loginUserFromKeytab("hive/ambari2012.howard2012.local#HOWARD2012.LOCAL", "/etc/security/keytabs/hive.service.keytab");
Class.forName("org.apache.hive.jdbc.HiveDriver");
System.out.println("getting connection");
Connection con = DriverManager.getConnection("jdbc:hive2://ambari2012:10000/;principal=hive/ambari2012.howard2012.local#HOWARD2012.LOCAL");
System.out.println("got connection");
con.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
the issue is doesnt matter what keytab I pass it's always giving the below error -
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
java.io.IOException: Login failure for hive/ambari2012.howard2012.local#HOWARD2012.LOCAL from keytab /etc/security/keytabs/hive.service.keytab
at org.apache.hadoop.security.UserGroupInformation.loginUserFromKeytab(UserGroupInformation.java:921)
at hive.connect.java.hive.connect.java.App.main(App.java:21)
Caused by: javax.security.auth.login.LoginException: Unable to obtain password from user
I don't think it's even trying to check if the right keytab is given to it.
How should I ensure it's reading the correct keytab file and also if the keytab file is not present it should give unable to locate the keytab
Please let me know if I have to copy the keytab ,krb files in my local machine
I don't think you can connect to a kerberized HIVE this way.
Try using a JAAS file https://docs.oracle.com/javase/7/docs/technotes/guides/security/jgss/tutorials/LoginConfigFile.html
and add the 2 following properties to your JVM :
-Djavax.security.auth.useSubjectCredsOnly=False
-Djava.security.auth.login.config=jaas.conf
Sample file jaas.conf :
com.sun.security.jgss.krb5.initiate
{ com.sun.security.auth.module.Krb5LoginModule required
useKeyTab =true
useTicketCache =false
doNotPrompt =true
principal ="hive/ambari2012.howard2012.local#HOWARD2012.LOCAL"
keyTab ="/etc/security/keytabs/hive.service.keytab"
debug =false;
};
Client
{ com.sun.security.auth.module.Krb5LoginModule required
useKeyTab =true
useTicketCache =false
doNotPrompt =true
principal ="hive/ambari2012.howard2012.local#HOWARD2012.LOCAL"
keyTab ="/etc/security/keytabs/hive.service.keytab"
debug =false;
};
I was missing jar files so if you add all jar files it's fine, here is the complete code
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.security.UserGroupInformation;
public class App {
private static Connection hiveConnection;
// get Hive Connection
public static void main(String [] args) throws IOException, SQLException {
String principal="principal";
String keytab="keytab";
Runtime rt = Runtime.getRuntime();
try{ Process p = rt.exec("kinit -k -t " + keytab + " " + principal);
p.waitFor(); }
catch(InterruptedException exception)
{
System.out.println("wait for threw an exception - it was interrupted");
exception.printStackTrace();
}
catch (IOException exception){
System.out.println("Exception in running kinit process") ;
exception.printStackTrace();
}
System.out.println("Preparing Hive connection1");
Configuration conf = new Configuration();
System.setProperty("javax.security.auth.useSubjectCredsOnly","false");
conf.set("hadoop.security.authentication", "Kerberos");
UserGroupInformation.setConfiguration(conf);
UserGroupInformation.loginUserFromKeytab(principal, keytab);
// Hive Connection
try {
Class.forName("org.apache.hive.jdbc.HiveDriver");
if(hiveConnection == null) {
hiveConnection = DriverManager.getConnection("jdbc:hive2://host:10000/;principal=principal;auth=kerberos;kerberosAuthType=fromSubject");
// return hiveConnection;
System.out.println("Got Connection");
} else {
//return hiveConnection;
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
// return null;
} catch (SQLException e) {
e.printStackTrace();
// return null;
}
}
}
Related
I am receiving an error that I did see on your site, but as a novice java coder I think that I flubbed my implementation of the solution.
I am getting a message
"SQL Server version 8 is not supported by this driver. ClientConnectionId:602d619d-c033-41d0-9109-80f56e3ab9b3"
I am using Eclipse Mars 1
The database is a sql server 2000, so I downloaded sqljdbc.jar from the Microsoft site as suggested by an earlier question. I loaded it to c:\temp and added that to the CLASSPATH and rebooted.
code snippet reads:
package texasLAMPConversion;
import java.sql.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class TexasLAMPConversion {
public static void main(String[] args) {
// TODO Auto-generated method stub
String host = "jdbc:sqlserver://serverName\\instanceName";
String uName = "*********";
String uPass = "*********";
Connection conn = null;
try {
conn = DriverManager.getConnection(host, uName,uPass);
if (conn != null) { System.out.println("Connected to the database");
}
} catch (SQLException ex) {
System.out.println(ex.getMessage() );
ex.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
FileWriter fw= null;
File file =null;
try {
file=new File("C:/temp/generated_stmts_update_t_customer_master.sql");
if(!file.exists()) {
file.createNewFile();
}
fw = new FileWriter(file);
}catch (IOException e) {
e.printStackTrace();
}
String oldKey = "8000001234", newKey = "8";
int lenAcct = 10;
oldKey = ReformatOldAcct(lenAcct, oldKey );
newKey = leftZeroAcct(lenAcct, newKey);
System.out.println("oldkey = " + oldKey + " and newKey = " + newKey);
}
private static String ReformatOldAcct(int lenAcct, String oldKey) {
// TODO Auto-generated method stub
String reformatedAcct = "";
reformatedAcct = "0" + oldKey.substring(2, lenAcct);
return (reformatedAcct);
}
private static String leftZeroAcct(int lenAcct, String acctNo) {
// TODO Auto-generated method stub
int index =0;
int currentLengthAcct;
String leadingZero = "";
currentLengthAcct = acctNo.length();
while (index < (lenAcct -currentLengthAcct) ) {
leadingZero = '0' + leadingZero;
index ++;
}
return (leadingZero + acctNo);
}
}
and it is throwing this complete message
Apr 19, 2016 11:29:44 AM com.microsoft.sqlserver.jdbc.SQLServerConnection Prelogin
WARNING: ConnectionID:1 ClientConnectionId: 602d619d-c033-41d0-9109-80f56e3ab9b3 Server major version:8 is not supported by this driver.
Apr 19, 2016 11:29:44 AM com.microsoft.sqlserver.jdbc.SQLServerConnection Prelogin
WARNING: ConnectionID:1 ClientConnectionId: 602d619d-c033-41d0-9109-80f56e3ab9b3 Server major version:8 is not supported by this driver.
com.microsoft.sqlserver.jdbc.SQLServerException: SQL Server version 8 is not supported by this driver. ClientConnectionId:602d619d-c033-41d0-9109-80f56e3ab9b3com.microsoft.sqlserver.jdbc.SQLServerException: SQL Server version 8 is not supported by this driver. ClientConnectionId:602d619d-c033-41d0-9109-80f56e3ab9b3
at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:2226) at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:2226)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:2210)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:2210)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.Prelogin(SQLServerConnection.java:2095)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.Prelogin(SQLServerConnection.java:2095)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1799)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1799)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:1454)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:1454)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:1285)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:1285)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:700)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:700)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1131)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1131)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at texasLAMPConversion.TexasLAMPConversion.main(TexasLAMPConversion.java:20)
at texasLAMPConversion.TexasLAMPConversion.main(TexasLAMPConversion.java:20)
SQL Server version 8 is not supported by this driver. ClientConnectionId:602d619d-c033-41d0-9109-80f56e3ab9b3
SQL Server version 8 is not supported by this driver. ClientConnectionId:602d619d-c033-41d0-9109-80f56e3ab9b3
Any thoughts for a novice java programmer moving away from PowerBuilder?
We are developing generic automated application which will download *.pgp file from SFTP server.
The application working fine with *.txt files. But when we are trying to pull *.pgp files we are getting the below exception.
2016-03-18 17:45:45 INFO jsch:52 - SSH_MSG_SERVICE_REQUEST sent
2016-03-18 17:45:46 INFO jsch:52 - SSH_MSG_SERVICE_ACCEPT received
2016-03-18 17:45:46 INFO jsch:52 - Next authentication method: publickey
2016-03-18 17:45:48 INFO jsch:52 - Authentication succeeded (publickey).
sftpSession org.springframework.integration.sftp.session.SftpSession#37831f
files size158
java.io.IOException: inputstream is closed
at com.jcraft.jsch.ChannelSftp.fill(ChannelSftp.java:2884)
at com.jcraft.jsch.ChannelSftp.header(ChannelSftp.java:2908)
at com.jcraft.jsch.ChannelSftp.access$500(ChannelSftp.java:36)
at com.jcraft.jsch.ChannelSftp$2.read(ChannelSftp.java:1390)
at com.jcraft.jsch.ChannelSftp$2.read(ChannelSftp.java:1340)
at org.springframework.util.StreamUtils.copy(StreamUtils.java:126)
at org.springframework.util.FileCopyUtils.copy(FileCopyUtils.java:109)
at org.springframework.integration.sftp.session.SftpSession.read(SftpSession.java:129)
at com.sftp.test.SFTPTest.main(SFTPTest.java:49)
java code :
public class SFTPTest {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
DefaultSftpSessionFactory defaultSftpSessionFactory = applicationContext.getBean("defaultSftpSessionFactory", DefaultSftpSessionFactory.class);
System.out.println(defaultSftpSessionFactory);
SftpSession sftpSession = defaultSftpSessionFactory.getSession();
System.out.println("sftpSessikon "+sftpSession);
String remoteDirectory = "/";
String localDirectory = "C:/312421/temp/";
OutputStream outputStream = null;
List<String> fileAtSFTPList = new ArrayList<String>();
try {
String[] fileNames = sftpSession.listNames(remoteDirectory);
for (String fileName : fileNames) {
boolean isMatch = fileCheckingAtSFTPWithPattern(fileName);
if(isMatch){
fileAtSFTPList.add(fileName);
}
}
System.out.println("files size" + fileAtSFTPList.size());
for (String fileName : fileAtSFTPList) {
File file = new File(localDirectory + fileName);
/*InputStream ipstream= sftpSession.readRaw(fileName);
FileUtils.writeByteArrayToFile(file, IOUtils.toByteArray(ipstream));
ipstream.close();*/
outputStream = new FileOutputStream(file);
sftpSession.read(remoteDirectory + fileName, outputStream);
outputStream.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
if (outputStream != null)
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static boolean fileCheckingAtSFTPWithPattern(String fileName){
Pattern pattern = Pattern.compile(".*\\.pgp$");
Matcher matcher = pattern.matcher(fileName);
if(matcher.find()){
return true;
}
return false;
}
}
Please suggest how to sort out this issue.
Thanks
The file type is irrelevant to Spring Integration - it looks like the server is closing the connection while reading the preamble - before the data is being fetched...
at com.jcraft.jsch.ChannelSftp.header(ChannelSftp.java:2908)
at com.jcraft.jsch.ChannelSftp.access$500(ChannelSftp.java:36)
at com.jcraft.jsch.ChannelSftp$2.read(ChannelSftp.java:1390)
at com.jcraft.jsch.ChannelSftp$2.read(ChannelSftp.java:1340)
The data itself is not read until later (line 1442 in ChannelSftp).
So it looks like a server-side problem.
I need to use commons-httpclient-3.1.jar library even it is now end of life.
I am trying to do a simple http GET from url http://www.apache.org/ via proxy server address "10.100.1.44" with proxy port "8080" which requires no credential whatsoever.
Below is my sample code.
import java.io.IOException;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
public class HttpClientTutorial {
private static String url = "http://www.apache.org/";
public static void main(String[] args) {
// Create an instance of HttpClient.
HttpClient client = new HttpClient();
// Create a method instance.
GetMethod method = new GetMethod(url);
// Provide custom retry handler is necessary
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(3, false));
try {
// setting proxy
client.getParams().setAuthenticationPreemptive(true);
client.getState().setProxyCredentials(
new AuthScope("10.100.1.44", 8080),
new UsernamePasswordCredentials("", ""));
// Execute the method.
int statusCode = client.executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: " + method.getStatusLine());
}
// Read the response body.
byte[] responseBody = method.getResponseBody();
// Deal with the response.
// Use caution: ensure correct character encoding and is not binary
// data
System.out.println(new String(responseBody));
} catch (HttpException e) {
System.err.println("Fatal protocol violation: " + e.getMessage());
e.printStackTrace();
} catch (IOException e) {
System.err.println("Fatal transport error: " + e.getMessage());
e.printStackTrace();
} finally {
// Release the connection.
method.releaseConnection();
}
}
}
However when I run the code I get exception thrown which is
14 ส.ค. 2556 13:11:28 org.apache.commons.httpclient.HttpMethodDirector authenticateHost
WARNING: Required credentials not available for BASIC <any realm>#www.apache.org:80
14 ส.ค. 2556 13:11:28 org.apache.commons.httpclient.HttpMethodDirector authenticateHost
WARNING: Preemptive authentication requested but no default credentials available
Fatal transport error: www.apache.org
java.net.UnknownHostException: www.apache.org
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:80)
at org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:122)
at org.apache.commons.httpclient.HttpConnection.open(HttpConnection.java:707)
at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:387)
at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:323)
at jenkins.plugin.assembla.api.HttpClientTutorial.main(HttpClientTutorial.java:38)
Could someone tell me what I did wrong as I am new to this HttpClient library however I need to use it as I have no intention to use HttpComponents library?
Now I have found a solution to this problem. Please refer to the code below.
import java.io.IOException;
import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
public class HttpClientTutorial {
private static String url = "http://www.apache.org/";
public static void main(String[] args) {
// Create an instance of HttpClient.
HttpClient client = new HttpClient();
// Create a method instance.
GetMethod method = new GetMethod(url);
// Provide custom retry handler is necessary
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(3, false));
try {
// setting proxy
client.getHostConfiguration().setProxy("10.100.1.44", 8080);
// Execute the method.
int statusCode = client.executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: " + method.getStatusLine());
}
// Read the response body.
byte[] responseBody = method.getResponseBody();
// Deal with the response.
// Use caution: ensure correct character encoding and is not binary
// data
System.out.println(new String(responseBody));
} catch (HttpException e) {
System.err.println("Fatal protocol violation: " + e.getMessage());
e.printStackTrace();
} catch (IOException e) {
System.err.println("Fatal transport error: " + e.getMessage());
e.printStackTrace();
} finally {
// Release the connection.
method.releaseConnection();
}
}
}
There is a similar problem here with the answer.
I think it is the same case as yours:
Using HttpProxy to connect to a host with preemtive authentication
I'm trying to develop an adf mobile app using jDeveloper and oracle sql developer.
I have connected jDev and sql. I want to populate selectOneChoice comp. that I m gonna fetch datas.
this is my connection method;
package salesorder.application;
import groovy.sql.Sql;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.sql.DataSource;
import oracle.adf.share.jndi.InitialContextFactoryImpl;
import oracle.adfmf.framework.api.AdfmfJavaUtilities;
import oracle.jbo.server.InitialContextImpl;
import oracle.jdbc.connector.OracleConnectionManager;
import oracle.jdbc.pool.OracleDataSource;
public class DBConnection {
public DBConnection() {
super();
}
private static String jdbcUrl = "jdbc:oracle:thin:#10.172.105.37:1521:VIS";
private static String userid = "***";
private static String password = "***";
protected static Connection conn = null;
public static Connection getConnection()throws Exception{
if (conn == null) {
try {
OracleDataSource ds; ds = new OracleDataSource();
ds.setURL(jdbcUrl);
conn=ds.getConnection(userid,password);
} catch (SQLException e) {
// if the error message is "out of memory",
// it probably means no database file is found
System.err.println(e.getMessage());
}
}
return conn;
}
}
and this, my method to fetch data;
private void Execute() {
Trace.log(Utility.ApplicationLogger, Level.INFO, Customers.class, "Execute",
"!!!!!!!!!!!!!!!!!In COUNTRY Execute!!!!!!!!!!!!!!!!!!!!!!!!!");
try{
Connection conn = DBConnection.getConnection();
customers.clear();
conn.setAutoCommit(false);
PreparedStatement stat= conn.prepareStatement("select cust_account_id,account_name from hz_cust_accounts_all where account_name is not null order by account_name asc");
// fetching customers name
ResultSet rs = stat.executeQuery();
Trace.log(Utility.ApplicationLogger, Level.INFO, Customers.class, "Execute",
"!!!!!!!!!!!!!!!!!Query Executed!!!!!!!!!!!!!!!!!!!!!!!!!");
while(rs.next()){
int id = rs.getInt("CUST_ACCOUNT_ID"); // customer id
String name = rs.getString("ACCOUNT_NAME"); // customer name
Customer c = new Customer(id,name);
customers.add(c);
}
rs.close();
} catch (SQLException e) {
System.err.println(e.getMessage());
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
when i try to start application, an error comes up like that.
i cant use an image. so
Error
oracle.jdbc.pool.OracleDataSource
I dont know how Im gonna solve that, i cannot figure out why . Any help ?
Just to clarify - are you trying to use ADF Mobile (AMX pages)?
If so then you can't connect with JDBC to a remote database from the client.
You can only connect with JDBC to the local SQLite DB on your device.
To access data from remote servers you'll need to expose this data with web services and call those.
i am trying connect to Oracle Database through following program. but throws an error
java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver "
only SQL developer client is installed on my machine whereas Actual data base is store on Server. Please help on resolving the issue
package test;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
public class DBConnection {
public static void main(String[] argv) {
System.out.println("-------- Oracle JDBC Connection Testing ------");
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
System.out.println("Where is your Oracle JDBC Driver?");
e.printStackTrace();
return;
}
System.out.println("Oracle JDBC Driver Registered!");
Connection connection = null;
enter code here
try {
connection = DriverManager.getConnection(
"jdbc:oracle:thin:#hostname:5800:SID", "user","password");
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
return;
}
if (connection != null) {
System.out.println("You made it, take control your database now!");
} else {
System.out.println("Failed to make connection!");
}
}
}
Add your Oracle JDBC Driver jar to the classpath
Can download the driver for your Oracle Database Version from here