JDBC驱动访问hive中的数据库信息

在pom.xml中导入依赖

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

新建xxx.java,连接hive中的数据库

public class HiveJdbcTest {
    public static void main(String[] args) throws Exception {
    //连接hive中数据库的驱动、URL、用户名、密码
        String driver = "org.apache.hive.jdbc.HiveDriver";
        String url = "jdbc:hive2://hadoop02:10000";
        String username = "root";
        String passwd = "";
        //加载驱动,连接数据库
        Class.forName(driver);
        Connection connection = DriverManager.getConnection(url, username, passwd);
        //向数据库发送要执行的sql语句
        Statement createStatement = connection.createStatement();
        //执行SQL语句,返回查询到的结果集
        ResultSet resultSet = createStatement.executeQuery("select * from t_user");
        while(resultSet.next()){
            String string = resultSet.getString(1);
            System.out.println(string+":"+string2);
        }
        //关闭资源
        resultSet.close();
        createStatement.close();
        connection.close(); 
    }

**:Hive的JDBC驱动连接数据库的使用和Java的JDBC使用基本类似,只是使用驱动的类和url不同

猜你喜欢

转载自blog.csdn.net/amin_hui/article/details/82287665