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        yanghanwei@geotmt.com
 * @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;
    }
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值