Skip to content
xml
<dependencies>
    <!-- hive-jdbc驱动 -->
    <dependency>
        <groupId>org.apache.hive</groupId>
        <artifactId>hive-jdbc</artifactId>
        <version>3.1.3</version>
        <exclusions>
            <!-- 和本机jar包冲突,你可以先不排除,如果无法正常加载依赖再排除 -->
            <exclusion>
                <artifactId>jdk.tools</artifactId>
                <groupId>jdk.tools</groupId>
            </exclusion>
            <!-- 日志冲突解决 -->
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

入门案例-查询所有数据

java
public static void main(String[] args) throws SQLException {
    // 1.指定hiveserver2的连接
    String jdbcUrl = "jdbc:hive2://192.168.133.106:10000";
    // 2.获取jdbc连接,这里的user使用root,就是linux中的用户名,password随便指定即
    Connection conn = DriverManager.getConnection(jdbcUrl, "root", "any");
    // 3.获取Statement
    Statement stmt = conn.createStatement();
    // 4.指定查询的sql
    String sql = "select * from t1";
    // 5.执行sql
    ResultSet res = stmt.executeQuery(sql);
    // 6.循环读取结果
    while (res.next()) {
        System.out.println(res.getInt("id") + "\t" + res.getString("name"));
    }
}