Free Online Certification Courses – Learn Today. Lead Tomorrow. › Forums › Apache Hadoop › JDBC connectivity in Hive?
- This topic has 1 reply, 1 voice, and was last updated 5 years, 7 months ago by DataFlair Team.
Viewing 1 reply thread
-
AuthorPosts
-
-
September 20, 2018 at 11:38 am #4673DataFlair TeamSpectator
<div class=”post”>
How to connect hive using JDBC connector to java program ?
</div>
-
September 20, 2018 at 11:39 am #4674DataFlair TeamSpectator
Step 1:
import java.sql.SQLException; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import java.sql.DriverManager; public class HiveJdbcClient { private static String driverName = “org.apache.hadoop.hive.jdbc.HiveDriver”; /** * @param args * @throws SQLException **/ public static void main(String[] args) throws SQLException { try { Class.forName(driverName); } catch (ClassNotFoundException e){ // TODO Auto-generated catch block e.printStackTrace(); System.exit(1); } Connection con = DriverManager.getConnection(“jdbc:hive://localhost: 10000/default”, “”, “”); Statement stmt = con.createStatement(); String tableName = “testHiveDriverTable”; stmt.executeQuery(“drop table ” + tableName); ResultSet res = stmt.executeQuery(“create table ” + tableName + ” (key int, value string)”); // show tables String sql = “show tables ‘” + tableName + “‘”; System.out.println(“Running: ” + sql); res = stmt.executeQuery(sql); if (res.next()) { System.out.println(res.getString(1)); } // describe table sql = “describe ” + tableName; System.out.println(“Running: ” + sql); res = stmt.executeQuery(sql); while (res.next()) { System.out.println(res.getString(1) + “t” + res.getString(2)); } // load data into table // NOTE: filepath has to be local to the hive server // NOTE: /tmp/test_hive_server.txt is a ctrl-A separated file with two fields per line String filepath = “/tmp/test_hive_server.txt”; sql = “load data local inpath ‘” + filepath + “‘ into table ” + tableName; System.out.println(“Running: ” + sql); res = stmt.executeQuery(sql); // select * query sql = “select * from ” + tableName; System.out.println(“Running: ” + sql); 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; System.out.println(“Running: ” + sql); res = stmt.executeQuery(sql); while (res.next()){ System.out.println(res.getString(1)); } } }
Step 2:
JDBC configuration in HIVE-SITE.XML JAVAX.JDO.OPTION.CONNECTIONURL JDBC.MYSQL://MYHOST/DBNAME?CREATEDATABASE IF NOEXIT=TRUE JAVAX.JDO.OPTION.CONNECTDRIVERNAME COM.MYSQL.JDBC.DRIVER JAVAX.JDO.OPTION.CONNECTIONUSERNAME MYDBUSERNAME JAVAX.JDO.OPTION.CONNECTIONPASSWORD MYDBPASSWORD
To learn more about the Hive follow: Hive Tutorial
To learn about Hive installation follow: Hive Installation
-
-
AuthorPosts
Viewing 1 reply thread
- You must be logged in to reply to this topic.