hive--jdbc的使用

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hive--jdbc的使用相关的知识,希望对你有一定的参考价值。

一、jdbc连接mysql代码示例

public class TestConnector {
    final static String USER = "root";
    final static String PASSWORD = "wjt86912572";
    final static String URL = "jdbc:mysql://bigdata121:3306/metastore?useSSL=false&serverTimezone=UTC&useUnicode=true";
    //这是 mysql.connector 在8.x版本中新驱动,com.mysql.jdbc.Driver在此版本中已弃用
    final static String DRIVER = "com.mysql.cj.jdbc.Driver";

    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;

        try {
            //加载驱动类
            Class.forName(DRIVER);
            //获取连接对象
            connection = DriverManager.getConnection(URL, USER, PASSWORD);
            //获取连接的代理对象,用于传递sql请求
            statement = connection.createStatement();
            boolean result = statement.execute("show databases;");
            resultSet = statement.getResultSet();

            //resultSet.next()看返回的结果是否还有数据,如果有为true
            while (resultSet.next()) {
                //resultSet提供了很多获取的方法,getInt,getString等,具体用哪个,看字段的数据类型
                System.out.println(resultSet.getString("Database"));
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (resultSet != null) {resultSet.close();}
                if (statement != null) {statement.close();}
                if (connection != null) {connection.close();}
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }

    }
}

二、jdbc连接hive代码示例

首先添加maven依赖:

<!-- https://mvnrepository.com/artifact/org.apache.hive/hive-jdbc -->
<dependency>
    <groupId>org.apache.hive</groupId>
    <artifactId>hive-jdbc</artifactId>
    <version>1.2.1</version>
</dependency>

代码:

package com.zy.hivejdbc;

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

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class HiveJDBC {

    //这是hive的driver名字
    private static String driverName="org.apache.hive.jdbc.HiveDriver";
    //数据库连接字符串
    private static String url = "jdbc:hive2://192.168.134.153:10000/mydb";
    private static String user = "root";
    private static String password="root";

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

    @Before
    public void init() throws Exception{
        Class.forName(driverName);
        conn = DriverManager.getConnection(url, user, password);
        stmt = conn.createStatement();
    }
    @Test
    public void createDatabase() throws Exception{
        String sql = "create database hive_jdbc_test";
        System.out.println("Running: " + sql);
        stmt.executeQuery(sql);
    }
    @Test
    public void dropDatabase() throws Exception {
        String sql = "drop database if exists hive_jdbc_test";
        System.out.println("Running: " + sql);
        stmt.execute(sql);
    }

    @Test
    public void showDatabases() throws Exception {
        String sql = "show databases";
        System.out.println("Running: " + sql + "
");
        rs = stmt.executeQuery(sql);
        while (rs.next()) {
            System.out.println(rs.getString(1) );
        }
    }

    @Test
    public void createTable() throws Exception {
        String sql = "create table t2(id int ,name String) row format delimited fields terminated by ‘,‘;";
        System.out.println("Running: " + sql);
        stmt.execute(sql);
    }

    @Test
    public void loadData() throws Exception {
         String filePath = "/usr/tmp/student";
         String sql = "load data local inpath ‘" + filePath + "‘ overwrite into table t2";
         System.out.println("Running: " + sql);
         stmt.execute(sql);
    }

    @Test
    public void selectData() throws Exception {
        String sql = "select * from t2";
        System.out.println("Running: " + sql);
        rs = stmt.executeQuery(sql);
        System.out.println("编号" + "	" + "姓名" );
        while (rs.next()) {
            System.out.println(rs.getInt(1) + "	" + rs.getString(2));
        }
    }
    @Test
    public static void drop(Statement stmt) throws Exception {
        String dropSQL = "drop table t2";
        boolean bool = stmt.execute(dropSQL);
        System.out.println("删除表是否成功:" + bool);
        }
    @After
    public void destory() throws Exception {
        if (rs != null) {
            rs.close();
        }
        if (stmt != null) {
            stmt.close();
        }
        if (conn != null) {
            conn.close();
        }
    }

}

基本使用和jdbc连接mysql类似。

以上是关于hive--jdbc的使用的主要内容,如果未能解决你的问题,请参考以下文章

使用HIVE-JDBC+TOMCAT-JDBC连接(CONNECTION)中断的处理过程

Spark 1.5.1 不适用于 hive jdbc 1.2.0

在 Spark 中使用 jdbc 驱动程序连接到 Hive

Hive JDBC 连接hiveserver2

无法使用 Zookeeper 连接字符串通过 JDBC 连接到 Hive

Hive JDBC 连接超时