Hive Jdbc Connection

Our HadoopTpoint App is now available in google play store,please rate and comment it in play store : W3Schools

Hive Jdbc Connection program

Below hive jdbc connection program, have connected  to hive using hive driver  “org.apache.hive.jdbc.HiveDriver” and then also given url for that and also provide username and password  to hive url.

HiveServer2 has a new JDBC driver. It supports both embedded and remote access to HiveServer2.

Connection URL for Remote or Embedded Mode

The JDBC connection URL format has the prefix jdbc:hive2:// and the Driver class is org.apache.hive.jdbc.HiveDriver. Note that this is different from the old HiveServer.

  • For a remote server, the URL format is jdbc:hive2://<host>:<port>/<db> (default port for HiveServer2 is 10000).
  • For an embedded server, the URL format is jdbc:hive2:// (no host or port).

Program:

import java.sql.SQLException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;
public class HiveJdbc {        //class name hive jdbc
private static String driverName = “org.apache.hive.jdbc.HiveDriver”;

public static void main(String[] args) throws SQLException {
try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.exit(1);
}
//replace “hive” here with the name of the user the queries should run as
Connection con = DriverManager.getConnection(“jdbc:hive2://pcp.dev.local:10000/default”, “hdfs”, “hdfs”);
Statement stmt = con.createStatement();
String Database = “airanalytics”;
String tableName = “airlines”;
stmt.execute(“use ” + Database);
//stmt.execute(“drop table if exists ” + tableName);
//stmt.execute(“create table ” + tableName + ” (key int, value string)”);
// show tables
String sql = “select origin from airlines where year=1988″;
System.out.println(“Running: ” + sql);
ResultSet res = stmt.executeQuery(sql);
while (res.next()) {
System.out.println(res.getString(“origin”));
}
}
}

Speak Your Mind

*