美文网首页Hive我爱编程
HiveServer2、Beeline、JDBC使用

HiveServer2、Beeline、JDBC使用

作者: 明明德撩码 | 来源:发表于2018-03-09 07:40 被阅读340次

    HiveServer2

    HiveServer 2(HS2)是一种服务,使客户端能够对Hive执行查询。HiveServer 2是HiveServer 1的继承者,HiveServer 1已被废弃。HS2支持多客户端并发和身份验证。它的设计是为了更好地支持开放API客户机,如JDBC和ODBC。HS2是一个作为复合服务运行的单个进程,它包括基于Thwift的Hive服务(TCP或HTTP)和用于WebUI的JettyWeb服务器。

    启动HiveServer2

    [root@hadoop-senior hive-0.13.1]# bin/hiveserver2

    servier2启动成功

    说明:如果不启动此服务或者服务启动失败,客户端beeline会报错.
    Error: Invalid URL: jdbc:hive2://hadoop-senior.beifeng.com:10000 (state=08S01,code=0)

    使用客户端连接Hiveserver2

    1、启用客户端[root@hadoop-senior hive-0.13.1]# bin/beeline
    2、连接服务器端

    连接服务器命令:
    beeline>!connect jdbc:hive2://hadoop-senior.beifeng.com:10000
    scan complete in 2ms
    Connecting to jdbc:hive2://hadoop-senior.beifeng.com:10000
    Enter username for jdbc:hive2://hadoop-senior.beifeng.com:10000: root
    Enter password for jdbc:hive2://hadoop-senior.beifeng.com:10000: 123456

    beeline 连接成功后的信息

    测试结果

    beeline 和 bing/hive 中的命令都是一样的,但是beeline 使用起来更人性化,查询数据的时候展现方式内容比较齐全,数据存在格式。

    ______________________________________________________________________________\

    Hive的JDBC使用

    HiveServer 2有一个JDBC驱动程序。它既支持嵌入式访问,也支持远程访问HiveServer 2。建议生产使用Remote HiveServer 2模式,因为它更安全,并且不需要为用户提供直接的HDFS/亚稳访问。

    步骤如下:
    You can use JDBC to access data stored in a relational database or other tabular format.

    1. Load the HiveServer2 JDBC driver. As of 1.2.0 applications no longer need to explicitly load JDBC drivers using Class.forName().

    For example:

    Class.forName("org.apache.hive.jdbc.HiveDriver");

    1. Connect to the database by creating a Connection object with the JDBC driver.

    For example:

    Connection cnct = DriverManager.getConnection("jdbc:hive2://<host>:<port>", "<user>", "<password>");

    The default <port> is 10000. In non-secure configurations, specify a <user> for the query to run as. The <password> field value is ignored in non-secure mode.

    Connection cnct = DriverManager.getConnection("jdbc:hive2://<host>:<port>", "<user>", "");

    In Kerberos secure mode, the user information is based on the Kerberos credentials.

    1. Submit SQL to the database by creating a Statement object and using its executeQuery() method.

    For example:

    Statement stmt = cnct.createStatement();

    ResultSet rset = stmt.executeQuery("SELECT foo FROM bar");

    1. Process the result set, if necessary.

    These steps are illustrated in the sample code below.

    JDBC Client 样例代码
    import java.sql.SQLException;
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.Statement;
    import java.sql.DriverManager;
     
    public class HiveJdbcClient {
      private static String driverName = "org.apache.hive.jdbc.HiveDriver";
     
      /**
       * @param args
       * @throws SQLException
       */
      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);
        }
        //replace "hive" here with the name of the user the queries should run as
        Connection con = DriverManager.getConnection("jdbc:hive2://hadoop-senior.beifeng.com:10000/default", "root", "123456");
        Statement stmt = con.createStatement();
        String tableName = "emp";
        stmt.execute("drop table if exists " + tableName);
        stmt.execute("create table " + tableName + " (key int, value string)");
        // show tables
        String sql = "show tables '" + tableName + "'";
        System.out.println("Running: " + sql);
        ResultSet res = stmt.executeQuery(sql);
        if (res.next()) {
          System.out.println(res.getString(1));
        }
           // describe table
        sql = "describe " + tableName;
        System.out.println("Running: " + sql);
        res = stmt.executeQuery(sql);
        while (res.next()) {
          System.out.println(res.getString(1) + "\t" + res.getString(2));
        }
     
        // load data into table
        // NOTE: filepath has to be local to the hive server
        // NOTE: /tmp/a.txt is a ctrl-A separated file with two fields per line
        String filepath = "/tmp/a.txt";
        sql = "load data local inpath '" + filepath + "' into table " + tableName;
        System.out.println("Running: " + sql);
        stmt.execute(sql);
     
        // select * query
        sql = "select * from " + tableName;
        System.out.println("Running: " + sql);
        res = stmt.executeQuery(sql);
        while (res.next()) {
          System.out.println(String.valueOf(res.getInt(1)) + "\t" + res.getString(2));
        }
     
        // regular hive query
        sql = "select count(1) from " + tableName;
        System.out.println("Running: " + sql);
        res = stmt.executeQuery(sql);
        while (res.next()) {
          System.out.println(res.getString(1));
        }
      }
    }
    

    Hive使用JDBC的原因

    相关文章

      网友评论

        本文标题:HiveServer2、Beeline、JDBC使用

        本文链接:https://www.haomeiwen.com/subject/vsvufftx.html