Home > Enterprise >  Connecting Scala with Hive Database using sbt for dependencies using IntelliJ
Connecting Scala with Hive Database using sbt for dependencies using IntelliJ

Time:11-04

I am having a very difficult time connecting to hive database using Intellij or basic Command line with scala ( would be happy with java too). I have in the past been able to connect to a MYSQL database by adding it on the library mysql-Connector. but I am unable somehow add a jar file to the project structure where it works.

and to make things abit more difficult. I have installed ubuntu with hive,spark, hadoop and I am connecting to it over the network.

Is there someway I can add a depedency on the sbt file?

Lastly, I know there are similar questions but they do not show in detail how to connect to a hive database from scala

enter image description here

enter image description here

enter image description here


`import java.sql.SQLException;
 import java.sql.Connection;
 import java.sql.ResultSet;
 import java.sql.Statement;
 import java.sql.DriverManager;
 object HiveJdbcClient extends App {
 val driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";
 Class.forName(driverName);
 val con=DriverManager.getConnection("jdbc:hive://http://192.168.43.64:10000/default", "", "");
 val stmt = con.createStatement();
 val tableName = "testHiveDriverTable";
 stmt.executeQuery("drop table "   "wti");
 var res = stmt.executeQuery("create table "   tableName   " (key int, value string)");
 // select * query
 var sql = "select * from "   tableName;
 res = stmt.executeQuery(sql);
 while (res.next()) {System.out.println(String.valueOf(res.getInt(1))   "\t"   res.getString(2));
 }
 // regular hive query
 sql = "select count(1) from "   tableName;
 res = stmt.executeQuery(sql);
 while (res.next()) {
      System.out.println(res.getString(1));
 }
}`

CodePudding user response:

The driver name is not correct for hive 3.1.2, it should be

org.apache.hive.jdbc.HiveDriver

Cf https://hive.apache.org/javadocs/r3.1.2/api/org/apache/hive/jdbc/HiveDriver.html

  • Related