• jdbc连接hive0.14


    Jdbc连接hive0.14版本号

    眼下官网最新版本号是hive0.13,要想下载最新的hive得去git上去clone一个。

    Hive0.14最大特点是支持直接插入。

    如今做一个jdbc连接hive0.14的样例。

    须要的jar包:

     

    不要去引入单独的一个集成hivejar由于那个包括了tomcat里面的几个jar包。

    当建立hiveproject时。会冲突导致hive的集成包载入不上。

    1.hive连接的工具类:

    package com.fish;

    import java.sql.Connection;

    import java.sql.DriverManager;

    import java.sql.PreparedStatement;

    import java.sql.ResultSet;

    import java.sql.SQLException;

    import java.util.ResourceBundle;

    /**

     * 连接数据库的工具类,被定义成不可继承且是私有訪问

     */

    public final class DBTool {

    final private static String OPTION_FILE_NAME = "hivedatabase";

    private static Connection conn;

    static ResourceBundle res;

    static {

    res = ResourceBundle.getBundle(OPTION_FILE_NAME);

    try {

    String driver = res.getString("jdbc.driver");

    Class.forName(driver);

    catch (ClassNotFoundException e) {

    e.printStackTrace();

    throw new RuntimeException(e);

    }

    }

    /**

     * 获取数据库的连接

     * 

     * @return conn

     */

    public static Connection getConnection() {

    if (null == conn) {

    try {

    String url = res.getString("jdbc.url");

    String user = res.getString("jdbc.username");

    String password = res.getString("jdbc.password");

    conn = DriverManager.getConnection(url, user, password);

    catch (SQLException e) {

    e.printStackTrace();

    throw new RuntimeException(e);

    }

    }

    return conn;

    }

    /**

     * 释放资源

     * 

     * @param conn

     * @param pstmt

     * @param rs

     */

    public static void closeJDBC(Connection conn, PreparedStatement pstmt,

    ResultSet rs) {

    if (null != rs) {

    try {

    rs.close();

    catch (SQLException e) {

    e.printStackTrace();

    throw new RuntimeException(e);

    finally {

    if (null != pstmt) {

    try {

    pstmt.close();

    catch (SQLException e) {

    e.printStackTrace();

    throw new RuntimeException(e);

    finally {

    if (null != conn) {

    try {

    conn.close();

    catch (SQLException e) {

    e.printStackTrace();

    throw new RuntimeException(e);

    }

    }

    }

    }

    }

    }

    }

    }

    2.hivedatabase.properties

    #hive database setting

    jdbc.type=hive

    jdbc.driver=org.apache.hive.jdbc.HiveDriver

    jdbc.url=jdbc:hive2://192.168.2.150:10000/default

    jdbc.username=hadoop

    jdbc.password=

    3.test是类

    public class Test {

    public static void main(String[] args) throws Exception {

    Connection connection = DBTool.getConnection();

    System.out.println(connection);}

    }

    假设能连接通说明你已经成功连接上hive了。

  • 相关阅读:
    JS-15 (class)
    JS-14 (解构)
    AI CycleGAN
    AI GAN
    AI StarGAN
    AI VGG
    硬件 PCIe总线
    工具 docker
    MySql开启慢速查询日志
    AI StyleGAN
  • 原文地址:https://www.cnblogs.com/llguanli/p/6741774.html
Copyright © 2020-2023  润新知