In this post, we will discuss about one of common hive clients, JDBC client for both HiveServer1 (Thrift Server) and HiveServer2. Use of HiveServer2 is recommended as HiveServer1 has several concurrency issues and lacks some features available in HiveServer2.
JDBC Data Types
The following table lists the data types implemented for HiveServer/HiveServer2 JDBC.
Hive JDBC Client Example for HiveServer1
Below is the code snippet for sample Java Client for HiveServer1. The connection URL used here is “jdbc:hive://host:port/dbname” where host and port are determined by where the Hive server is running. Default port number is 10000 and the dbname supported is ‘default’. At this point of time, no other dbnames are supported.
And the HiveDriver class, we use should be org.apache.hadoop.hive.jdbc.HiveDriver , and this class will be present in hive-jdbc-<version>.jar file, so this jar needs to be in classpath for compiling the below code. But for execution we need to make sure that all the jars present under $HIVE_HOME/lib/*.jar files and $HADOOP_HOME/share/hadoop/mapreduce/*.jar files and $HADOOP_HOME/share/hadoop/common/*.jar files need to be added to the build path in eclipse otherwise we get classNotFound exceptions.
In the below code, we are creating one sample empdata table and loading it with three column values per line and retrieving the results back from empdata table.
Load theHiveServerJDBC driver with
Connect to the database by creating a
Connectionobject with theJDBC driver.
Since, HiveServer 1 runs in non-secure mode, we can create the connection object without username and password parameters in the connections string as shown below.
Submit SQL to the database by creating a
Statementobject and using its
- Process the result set, if necessary.