java 實用 jdbc 鏈接 hive

 

此種方式 可以鏈接 zookeeper 集羣hive

package com.geotmt.billingcenter.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;

import java.sql.*;
import java.util.Properties;

/**
 * @Description hive Jdbc 工具類
 * @Author      yanghanwei
 * @Mail        [email protected]
 * @Date        15:59 2019-11-22
 * @Version     v1
 **/
public class HiveUtils implements InitializingBean {

    /**
     * 網上寫 org.apache.hadoop.hive.jdbc.HiveDriver ,新版本不能這樣寫
     */
    private static String user = "hdfs";
    private static String password = "";
    private static String sql = "";
    private static ResultSet res;
    private static final Logger log = LoggerFactory.getLogger(HiveUtils.class);

    private static String driverName = "org.apache.hive.jdbc.HiveDriver";

    private static String url = "jdbc:hive2://hadoop26.geotmt.com:2181,hadoop206.geotmt.com:2181/;serviceDiscoveryMode=zooKeeper;zooKeeperNamespace=hiveserver2";

    private static Connection conn = null;

    @Override
    public void afterPropertiesSet() throws Exception {
        Class.forName(driverName);
    }

    /**
     * main 測試
     * @param args
     */
    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        try {
            conn = getConn();
            stmt = conn.createStatement();
            // 執行 select * query 操作
            selectData(stmt, "dp_businessdata.dws_business_inner_charging_day_v1");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            log.error(driverName + " not found!", e);
            System.exit(1);
        } catch (SQLException e) {
            e.printStackTrace();
            log.error("Connection error!", e);
            System.exit(1);
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 查詢數據
     * @param stmt
     * @param tableName
     * @throws SQLException
     */
    private static void selectData(Statement stmt, String tableName)
            throws SQLException {
        sql = "select * from " + tableName;
        System.out.println("Running:" + sql);
        res = stmt.executeQuery(sql);
        System.out.println("執行 select * query 運行結果:");
        while (res.next()) {
            System.out.println(res.getInt(1) + "\t" + res.getString(2));
        }
    }

    /**
     * 獲取鏈接
     * @return
     * @throws ClassNotFoundException
     * @throws SQLException
     */
    private static Connection getConn() throws ClassNotFoundException, SQLException {
        if (conn == null) {
            try {
                Class.forName(driverName);
                conn =(Connection) DriverManager.getConnection(url, user, password);
            } catch (ClassNotFoundException e){
                e.printStackTrace();
                return null;
            } catch (SQLException e) {
                e.printStackTrace();
                return null;
            }
        }
        return conn;
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章