how to connect hive in secure mode(kerberos+ssl) using JDBC? - java

I need to connect hive(kerberos + SSL) using jdbc, i tried this code but getting error like :
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/hadoop/util/VersionInfo
at org.apache.hadoop.hive.shims.ShimLoader.getMajorVersion(ShimLoader.java:155)
at org.apache.hadoop.hive.shims.ShimLoader.loadShims(ShimLoader.java:134)
at org.apache.hadoop.hive.shims.ShimLoader.getHadoopThriftAuthBridge(ShimLoader.java:120)
at org.apache.hive.service.auth.HttpAuthUtils.getKerberosServiceTicket(HttpAuthUtils.java:52)
at org.apache.hive.jdbc.HttpKerberosRequestInterceptor.process(HttpKerberosRequestInterceptor.java:63)
at org.apache.http.protocol.ImmutableHttpProcessor.process(ImmutableHttpProcessor.java:109)
at org.apache.http.protocol.HttpRequestExecutor.preProcess(HttpRequestExecutor.java:176)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:518)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:827)
at org.apache.thrift.transport.THttpClient.flushUsingHttpClient(THttpClient.java:235)
at org.apache.thrift.transport.THttpClient.flush(THttpClient.java:297)
at org.apache.thrift.TServiceClient.sendBase(TServiceClient.java:65)
at org.apache.hive.service.cli.thrift.TCLIService$Client.send_OpenSession(TCLIService.java:141)
at org.apache.hive.service.cli.thrift.TCLIService$Client.OpenSession(TCLIService.java:133)
at org.apache.hive.jdbc.HiveConnection.createHttpTransport(HiveConnection.java:267)
at org.apache.hive.jdbc.HiveConnection.openTransport(HiveConnection.java:201)
at org.apache.hive.jdbc.HiveConnection.(HiveConnection.java:168)
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:270)
at prodhive.main(prod_test.java:25)
Caused by: java.lang.ClassNotFoundException: org.apache.hadoop.util.VersionInfo
private static String driverName = "org.apache.hive.jdbc.HiveDriver";
public static void main(String[] args) throws SQLException {
try {
Class.forName(driverName);
System.out.println("Driver Registered");
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println("Exception Occured");
System.exit(1);
}
System.out.println("before connection");
Connection con = DriverManager.getConnection(
("jdbc:hive2://abc.xyz.com:10000/default;principal=hive/_HOST#XYZ.COM;KrbRealm=PQR.COM;KrbHostFQDN=abc.xyz.com;KrbServiceName=hive;KrbAuthType=0;SSLKeyStore=/home/arcosadmin/va.xyz.com.jks;SSLKeyStorePwd=1234;transportMode=http;httpPath=cliservice;SSL=1;AuthMech=3;UID=HS2;PWD=1234")
System.out.println("Connection Established Successfully");
Statement stmt = con.createStatement();
System.out.println("Table Creation Started");
String tableName = "hive_con";
stmt.executeUpdate("CREATE TABLE IF NOT EXISTS " + tableName + "(eid int, name String,destination String)");
System.out.println("Table have been created");
con.close();
System.out.println("Connection Closed");
}

Related

Unable to connect with MariaDB using Java

I am trying to connect with My company's database which is 'MariaDB', using Java, I am using following code:
package co.companyname.helpers;
import java.sql.*;
public class DatabaseConnectivity {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
String query = "select * from time_clock limit 10";
try {
Class.forName("org.mariadb.jdbc.Driver");
System.out.println("Connecting to a selected database...");
conn = DriverManager.getConnection(
"jdbc:mariadb://host_url:3306", "root", "password");
System.out.println("Connected database successfully...");
System.out.println("Creating table in given database...");
stmt = conn.createStatement();
String sql = query;
stmt.executeUpdate(sql);
System.out.println("Created table in given database...");
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (stmt != null) {
conn.close();
}
} catch (SQLException se) {
}
try {
if (conn != null) {
conn.close();
}
} catch (SQLException se) {
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
}
At above I have used host_url, but I am using actual host within string, now I am getting following error:
java.lang.ClassNotFoundException: org.mariadb.jdbc.Driver
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:315)
at co.company_name.helpers.DatabaseConnectivity.main(DatabaseConnectivity.java:10)
Goodbye!
I need to know, if I am doing anything wrong.
Thanks.

Java program to connect azure database in Netbeans I'm getting error

I'm getting the following error:
I tried various ways I'm getting an error please see the following error.
run: java.lang.ClassNotFoundException:
com.microsoft.sqlserver.jdbc.SQLServerDriver at
java.net.URLClassLoader.findClass(URLClassLoader.java:381) at
java.lang.ClassLoader.loadClass(ClassLoader.java:424) at
sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338) at
java.lang.ClassLoader.loadClass(ClassLoader.java:357) at
java.lang.Class.forName0(Native Method) at
java.lang.Class.forName(Class.java:264) at
com.sqldbsamples.App.main(App.java:23)
BUILD SUCCESSFUL (total time: 0 seconds)
This is my code: please help me with how to connect my database to azure using java program !!!
public class App {
public static void main(String[] args) {
// Connect to database
String hostName = "testchinnaa.database.windows.net:1433"; // update me
String dbName = "Test_Gopi"; // update me
String user = "chinna"; // update me
String password = "******"; // update me
String url = String.format("jdbc:sqlserver://testchinnaa.database.windows.net:1433;database=Test_Gopi;user=chinna#testchinna;password=*****;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;"
+ "hostNameInCertificate=*.database.windows.net;loginTimeout=30;", hostName, dbName, user, password);
Connection connection = null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
connection = DriverManager.getConnection(url);
String schema = connection.getSchema();
System.out.println("Successful connection - Schema: " + schema);
System.out.println("Query data example:");
System.out.println("=========================================");
// Create and execute a SELECT SQL statement.
String selectSql = "SELECT TOP 20 pc.Name as CategoryName, p.name as ProductName "
+ "FROM [SalesLT].[ProductCategory] pc "
+ "JOIN [SalesLT].[Product] p ON pc.productcategoryid = p.productcategoryid";
try (Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(selectSql)) {
// Print results from select statement
System.out.println("Top 20 categories:");
while (resultSet.next())
{
System.out.println(resultSet.getString(1) + " "
+ resultSet.getString(2));
}
connection.close();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Sorry, you have to figure out if you are using MySql or MSSql. You said that you used mysql. However, in your connection string, it is a sqlserver which means it is a MSSql.
Here is the tutorial for accessing database using java:
You need to download connector for your database:
For MySql: MySQL
Connector/J
For MSSql: Microsoft JDBC Driver for SQL
Server
Manually add connector jar file to your classpath. Or you can use Maven dependencies manager to install and configure the Connector/J library in your project.
<!-- Example for mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>
Code sample
For MSSql:
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
connection = DriverManager.getConnection("conntection_string");
String SQL = "SELECT name FROM sysdatabases;";
try (Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(SQL)) {
// Print results from select statement
while (resultSet.next()) {
System.out.println(resultSet.getString(1));
}
connection.close();
}
} catch (Exception e) {
e.printStackTrace();
}
For MySql:
Connection conn = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String connectionUrl = "jdbc:mysql://{server_name}.mysql.database.azure.com:3306?useSSL=true&requireSSL=false&serverTimezone=UTC";;
conn = DriverManager.getConnection(connectionUrl, "username_from_portal, like: jack#mysqldemo258", "password");
rs = conn.prepareStatement("show databases").executeQuery();
while(rs.next()){
System.out.println(rs.getString(1));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try{
if(rs != null) rs.close();
if(conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

JDBC SQLServerConnection "Login failed for user 'sa'"

I am trying connection with database but this error frustrates me . Can anyone help me out what is the problem here?
I want to connect to SQL Server from Eclipse.
public class JDBC_Test {
public static void SQLServerConnection() {
try {
String url = "jdbc:sqlserver://localhost:1433;DatabaseName=Northwind";
String user = "sa";
String password = "832Waseem#";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection(url, user, password);
System.out.println("Connection Created Successfully.......");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from categories");
while (rs.next()) {
System.out.println(rs.getInt(1) + " " + rs.getString(2) + " " + rs.getString(3));
}
con.close();
System.out.println("Connection Closed Successfully.......");
} catch (Exception e) {
// System.out.println("Unable to create Connection reason is as below.......");
e.printStackTrace();
}
}
public static void main(String args[]) {
SQLServerConnection();
}
}
My Error:
com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'sa'. at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:196)

Type 5 DataDirect JDBC Drivers : No suitable driver found

I'm using Type 5 DataDirect JDBC Drivers in my program, but when I run this program then I get an exception:
java.sql.SQLException: No suitable driver found for
jdbc:datadirect:mysql://localhost:3306/jmysql?useSSL=false.
public class Type5DataDirectDriver {
public static void main(String[] args) {
Connection con = null;
// Establish the Connection
try {
con = DriverManager.getConnection("jdbc:datadirect:mysql://localhost:3306/jmysql?useSSL=false", "root", "");
// Verify the Connection
DatabaseMetaData metaData = con.getMetaData();
System.out.println("Driver Name: " + metaData.getDriverName());
System.out.println("Driver Version: " + metaData.getDriverVersion());
System.out.println("Database Name: " + metaData.getDatabaseProductName());
System.out.println("Database Version: " + metaData.getDatabaseProductVersion());
} catch(SQLException ex) {
ex.printStackTrace();
}
finally {
try {
if (con != null) {
con.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
ExceptionL
java.sql.SQLException: No suitable driver found for jdbc:datadirect:mysql://localhost:3306/jmysql?useSSL=false
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:703)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:229)
at java9.Type_3MiddlewareDriver.main(Type5DataDirectDriver.java:14)

cannot exexute SQL query wirh OJDBC and Oracle DB (but full working with posgtres JDBC and Postgres DB)

I wrote a java program which retrieve data from a PG gb, process them, and write them in an Oracle DB.
While the PG part is fully working, the Oracle one has issues.
I can connect to the DB, but every query ends with a rollback (ResultSet with Oracle is always null)
Of course i have both PG and Oracle JDBC driver.
Here are my DBs object and testing queries
private final static PostgresDB postgres = new PostgresDB("jdbc:postgresql://192.168.2.23:5432/T18CLEAN", "myPGUser", "myPGPasswd", true);
private final static OracleDB oracle = new OracleDB("jdbc:oracle:thin:#192.168.2.20:1521/EFFEVI.T18FV.IT", "myOracleUser", "myOraclePasswd");
private final static String testPostgres = "SELECT product_pricelist_item.x_product_name FROM public.product_pricelist_item;";
private final static String testOracle = "SELECT EFFEVI.PRESA_ORDINI.PO_CLIENTE FROM EFFEVI.PRESA_ORDINI;";
Then I setup the 2 connections:
PG:
public Connection getConnect() throws ClassNotFoundException {
System.out.println("-------- Posgres JDBC Connection Testing ------");
String url = c_url;
Connection conn = null;
Properties props = new Properties();
props.setProperty("user", user);
props.setProperty("password", passwd);
props.setProperty("ssl", boolToString(sslEnabled));
try{
Class.forName("org.postgresql.Driver");
System.out.println("Postgres JDBC Driver Registered!");
} catch(ClassNotFoundException e) {
System.out.println("Where is your Oracle JDBC Driver?");
e.printStackTrace();
return null;
}
try {
conn = DriverManager.getConnection(url, props);
System.out.println("You made it, take control your Postgres database now!");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("Failed to make connection to Postgres DB!");
}
return conn;
}
Oracle:
public Connection getConnect(){
Connection connection = null;
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 connection;
}
System.out.println("Oracle JDBC Driver Registered!");
try {
connection = DriverManager.getConnection(c_url, user, passwd);
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
return connection;
}
if (connection != null) {
System.out.println("You made it, take control your Oracle database now!");
return connection;
} else {
System.out.println("Failed to make connection to Oracle DB!");
}
return connection;
}
After all these pass i perform queries
public ResultSet executeCommand(Connection c, String command) {
Statement st = null;
ResultSet rs = null;
try {
st = c.createStatement();
rs = st.executeQuery(command);
} catch (SQLException e) {
}
if(rs==null){
System.out.println("Failed to Execute command " + command);
} else {
System.out.println("Command Executed: " + command);
}
return rs;
}
Assuming that there are no parameters error... What could it be? Any help?
Thank you very much
Remove a semicolon at the end of the query.
Use this:
private final static String testOracle =
"SELECT EFFEVI.PRESA_ORDINI.PO_CLIENTE FROM EFFEVI.PRESA_ORDINI";
instead of this one:
private final static String testOracle =
"SELECT EFFEVI.PRESA_ORDINI.PO_CLIENTE FROM EFFEVI.PRESA_ORDINI;";
Also don't silently "swallow" an exception in your code:
} catch (SQLException e) {
}
Rethrow the exception, or at least print the error to the log:
} catch (SQLException e) {
log.error("Error while executing query " + command, e);
throw new RuntimeException("Error while executing query " + command, e);
}

Categories