Hive工具类

本文介绍了一个用于操作Hive数据库的工具类HiveUtil,包括连接配置、基本的数据库操作如获取连接、创建表、删除表等,并提供了示例代码。
public class HiveUtil {

    private static Logger logger = LoggerFactory.getLogger(HiveUtil.class);

    private static BasicDataSource basicDataSource = null;
    
    private static String HIVE_DRIVER = null;
    private static String HIVE_URL = null;
    
    static {
        HIVE_DRIVER = ConfigProperties.getInstance().getValue("hive.driver");
        HIVE_URL = ConfigProperties.getInstance().getValue("hive.url");
        
        //创建数据源对象
        if (basicDataSource == null) {
            basicDataSource = new BasicDataSource();
            //设置连接数据库的驱动
            basicDataSource.setDriverClassName(HIVE_DRIVER);
            //设置连接数据库的url
            basicDataSource.setUrl(HIVE_URL);
            //设置连接数据库的用户名
            basicDataSource.setUsername("root");
            //设置连接数据库的密码
            basicDataSource.setPassword("");
            //设置连接池启动时的初始值
            basicDataSource.setInitialSize(5);
            //设置连接池的最大值
            basicDataSource.setMaxActive(256);
            //最大空闲值.当经过一个高峰时间后,连接池可以慢慢将用不到的连接慢慢释放一部分,一直减少到maxIdle为止
            basicDataSource.setMaxIdle(20);
            //最小空闲值.当空闲的连接数少于该值时,连接池就会预申请一些连接,以避免洪峰来时再申请而造成的性能开销
            basicDataSource.setMinIdle(5);
        }
    }

    public static Connection getConnection() throws SQLException {
        return basicDataSource.getConnection();
    }

    public static Statement getStatement(Connection connection) throws SQLException {
        return connection.createStatement();
    }

    public static PreparedStatement getPreparedStatement(Connection connection, String sql) throws SQLException {
        return connection.prepareStatement(sql);
    }

    public static void switchDB(Connection connection, String database) throws SQLException {
        Statement statement = getStatement(connection);
        if (StringUtils.isNotEmpty(database)) {
            statement.execute("use " + database);
        }

    }

    public static void createTable(Connection connection, String database, String tableName, String sql) throws SQLException {
        Statement statement = getStatement(connection);
        if (StringUtils.isNotEmpty(tableName)) {
            statement.execute("drop table if exists " + tableName);
            statement.execute(sql);
        }
        close(statement);
    }

    public static void listTables(Connection connection, String database) throws SQLException {
        Statement statement = getStatement(connection);
        ResultSet resultSet = statement.executeQuery("show tables");
        while (resultSet.next()) {
            logger.info(resultSet.getString("tab_name"));
        }
        close(resultSet);
        close(statement);
    }

    public static void listTableStructure(Connection connection, String database, String tableName) throws SQLException {
        Statement statement = getStatement(connection);
        ResultSet resultSet = statement.executeQuery("describe " + tableName);
        while (resultSet.next()) {
            logger.info(resultSet.getString("col_name") + " | " + resultSet.getString("data_type") + " | "
                    + resultSet.getString("comment"));
        }
        close(resultSet);
    }

    public static void dropTable(Connection connection, String database, String tableName) throws SQLException {
        Statement statement = getStatement(connection);
        if (StringUtils.isNotEmpty(tableName)) {
            statement.execute("drop table if exists " + tableName);
        }
        close(statement);
    }

    public static void execute(Connection connection, String sql, Boolean isPrepared) throws SQLException {
        if (isPrepared) {
            PreparedStatement preparedStatement = getPreparedStatement(connection, sql);
            ResultSet resultSet = preparedStatement.executeQuery();
            close(resultSet);
            close(preparedStatement);
        } else {
            Statement statement = getStatement(connection);
            statement.execute(sql);
            close(statement);
        }
    }

    public static ResultSet list(Connection connection, PreparedStatement preparedStatement, Object[] objs) throws SQLException {
        if (ArrayUtils.isNotEmpty(objs)) {
            for (int i = 0; i < objs.length; i++) {
                preparedStatement.setObject(i, objs[i]);
            }
        }
        return preparedStatement.executeQuery();
    }

    public static void close(Connection connection) throws SQLException {
        if (connection != null) {
            connection.close();
            connection = null;
        }
    }

    public static void close(Statement statement) throws SQLException {
        if (statement != null) {
            statement.close();
            statement = null;
        }

    }

    public static void close(PreparedStatement preparedStatement) {
        if (preparedStatement != null) {
            try {
                preparedStatement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            preparedStatement = null;
        }
    }

    public static void close(ResultSet resultSet) {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            resultSet = null;
        }

    }

}

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值