温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java API如何操作Hive

发布时间:2021-12-10 14:16:13 来源:亿速云 阅读:372 作者:小新 栏目:大数据

这篇文章给大家分享的是有关Java API如何操作Hive的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

环境:

   IDEA2017.3+Maven-3.3.9+Hive1.1.0

1. pom.xml里面的依赖包配置

<properties>

  <hive.version>1.1.0</hive.version>
</properties>
<dependencies>

  <!-- 添加Hive-jdbc的dependency -->

  <dependency>

    <groupId>org.apache.hive</groupId>

    <artifactId>hive-jdbc</artifactId>

    <version>${hive.version}</version>

  </dependency>
</dependencies>
2. 新建HiveJdbcClient.java 文件
package com.ruozedata.day6;

import java.sql.*;

public class HiveJdbcClient {

    /**

     * 需要把:org.apache.hadoop.hive.jdbc.HiveDriver

     * 改成:org.apache.hive.jdbc.HiveDriver

     */

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


    public static void main(String[] args) throws SQLException {

        try {

            Class.forName(driverName);

        } catch (ClassNotFoundException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

            System.exit(1);

        }

        /**

         * 需要把:jdbc:hive://localhost:10000/default

         * 改成:jdbc:hive2://192.168.1.108:10000/ruozedata_test

         */

        Connection con = DriverManager.getConnection("jdbc:hive2://192.168.1.108:10000/ruozedata_test", "root", "root");

        Statement stmt = con.createStatement();

        String tableName = "a";

        String sql = "" ;

        /**

         * a的表结构:

         * # col_name              data_type

         * id                      int

         * name                    string

         * age                     int

         * select id,name,age query

         */


        sql = "select id,name,age  from " + tableName;

        System.out.println("Running: " + sql);

        ResultSet  res = stmt.executeQuery(sql);

        while (res.next()) {

            System.out.println(String.valueOf(res.getInt(1)) + "\t"

                    + res.getString(2)+ "\t"

                    + res.getInt(3));

        }

    }
}

需要注意2点:

 1.官网的驱动名称driverName: org.apache.hadoop.hive.jdbc.HiveDriver

 需要改成:org.apache.hive.jdbc.HiveDriver

 

 2.官网的数据库连接URL:jdbc:hive://localhost:10000/default

 需要改成:jdbc:hive2://localhost:10000/default   

 其中localhost改成数据库的IP,default改成需要连接的数据名

 
3. 需要开启Hive的 hiveserver2 服务。

   如果不开启hiveserver2 服务,运行程序的时候就会报错:

   Exception in thread "main" java.sql.SQLException: Could not open client transport with JDBC Uri: jdbc:hive2://192.168.1.108:10000/ruozedata_test: java.net.SocketException: Connection reset

4. 运行程序的结果:
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/D:/software/apache-maven-3.3.9/repository/org/slf4j/slf4j-log4j12/1.7.5/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/D:/software/apache-maven-3.3.9/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
log4j:WARN No appenders could be found for logger (org.apache.hive.jdbc.Utils).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Running: select id,name,age  from a
1	zhangsan	15

Process finished with exit code 0

感谢各位的阅读!关于“Java API如何操作Hive”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI