Hive’s JDBC connection

Time:2021-6-26

Related knowledge
Hive is the basic component of data warehouse application in big data technology cluster, and it is the benchmark of other similar data warehouse applications. Basic data operations can be processed by hive client in script mode. If you need to develop applications, you need to use hive’s jdbc driver to connect.
Hive jdbc driver connection is divided into two types, the early one is hiveserver, and the latest one is hiveserver2. The former has many problems, such as security and concurrency, while the latter solves the problems such as security and concurrency.

System environment
Linux Ubuntu 20.04
OpenJDK-11.0.11
hadoop-3.2.2
mysql-8.0.25

Task content
Practice using JDBC to connect hive and operate hive through Java code.


Task steps
1. First, create a new / data / hive6 directory in Linux to store the required files.

mkdir -p /data/hive6  

2. Switch to / apps / hive / conf directory, modify hive-site.xml file, and write the following configuration to

<property>  
   <name>hive.server2.thrift.port</name>  
   <value>10000</value>  
</property>  
<property>  
    <name>hive.server2.thrift.bind.host</name>  
    <value>127.0.0.1</value>  
    </property>  

3. Switch to / apps / Hadoop / SBIN directory and start Hadoop related processes

cd /apps/hadoop/sbin  
./start-all.sh  

4. Start MySQL service

sudo service mysql start  

5. Switch to the / apps / hive directory and start hiveserver2

cd /apps/hive        
./bin/hive --service hiveserver2 

Hive's JDBC connection

6. Open a terminal simulator and use the netstat command to view port 10000

netstat -nptl | grep 10000  

Hive's JDBC connection

7. Switch to / apps / hive / bin and start beeline

cd /apps/hive/bin  
./beeline      

Hive's JDBC connection

8. Use (! connect jdbc:hive2 : / / IP address: 10000 hiveuser hivepassword) to test whether it can connect. The user name and password are the created user name (root) and password (123456).

!connect jdbc:hive2://127.0.0.1:10000  

9. Create a project in idea and add the configuration file in Hadoop to resources
Hive's JDBC connection

10. Add Maven dependency

<dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.25</version>
    </dependency>
    <dependency>
        <groupId>org.apache.hive</groupId>
        <artifactId>hive-jdbc</artifactId>
        <version>2.3.8</version>
    </dependency>   

Hive's JDBC connection


package my.study.hive;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class HiveClient {
    private static String driverName = "org.apache.hive.jdbc.HiveDriver";
    public static void main(String args[]){
        try {
            Class.forName(driverName);
            Connection con = DriverManager.getConnection("jdbc:hive2://localhost:10000/hive", "root", "123456");

            Statement st =  con.createStatement();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.exit(1);
        }catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}