JDBC操作Hive概述

一.服务器配置

  1. 配置beeline
  2. 启动hiveserver2并置后台运行
    nohup hive --service hiveserver2 > /dev/null 2>&1 &

二.pom.xml

<dependencies>
    <dependency>
        <groupId>org.apache.hive</groupId>
        <artifactId>hive-jdbc</artifactId>
        <version>2.1.1</version>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>RELEASE</version>
    </dependency>
</dependencies>

三.Java JDBC操作

public class HiveJDBC {

    private static String driverName = "org.apache.hive.jdbc.HiveDriver";
    private static String url = "jdbc:hive2://node3:10000/mytest";
    private static String user = "root";
    private static String password = "123456";

    private static Connection conn = null;
    private static Statement stmt = null;
    private static ResultSet rs = null;

    public static void main(String[] args) throws Exception {
        Class.forName(driverName);
        conn = DriverManager.getConnection(url,user,password);
        stmt = conn.createStatement();

        String sql = "select * from t_test";
        rs = stmt.executeQuery(sql);
        while(rs.next()){
            System.out.print(rs.getString("name"));
        }

        if ( rs != null) {
            rs.close();
        }
        if (stmt != null) {
            stmt.close();
        }
        if (conn != null) {
            conn.close();
        }
    }
}

Related post

  1. 设计模式-单例模式

    2020-09-10

  2. Java 编码

    2020-07-31

  3. 设计模式-工厂模式

    2020-09-09

  4. 异常处理的合理使用与防御式编程

    2022-02-21

There are no comment yet.

COMMENT

Take a Coffee Break

Recommend post

  1. 常用工具指令

    2022-09-18

Category list

ABOUT

Welcome to FullStar, a captivating online destination where the realms of software development and personal reflections intertwine.

April 2025
M T W T F S S
 123456
78910111213
14151617181920
21222324252627
282930  

Life Logs

  1. 回首

    2023-07-14

Return Top