Hive 8、Hive2 beeline 和 Hive jdbc

时间:2022-02-12 06:02:14

1、Hive2 beeline 

Beeline 要与HiveServer2配合使用,支持嵌入模式和远程模式

启动beeline

打开两个Shell窗口,一个启动Hive2 一个beeline连接hive2

#启动HiverServer2 , ./bin/hiveserver2 
[[email protected] ~]# hiveserver2
16/02/23 22:55:25 WARN conf.HiveConf: HiveConf of name hive.metastore.local does not exist

  

#启动Beeline 
# $ ./bin/beeline 下面的方式需要配置Hive的环境变量
[[email protected] ~]# beeline
Beeline version 1.2.1 by Apache Hive
beeline> 

  启动beeline之后可以尝试连接hiveserver2

beeline> !connect jdbc:hive2://node5:10000
Connecting to jdbc:hive2://node5:10000
Enter username for jdbc:hive2://node5:10000: 
#默认 用户名就是登录账号 密码为空

  2、Hive jdbc

打开Eclipse 新建一个Java 项目:

public class Demo {

    public static void main(String[] args) {
        try {
            Class.forName("org.apache.hive.jdbc.HiveDriver");
            Connection conn = DriverManager.getConnection("jdbc:hive2://node5:10000/hive","root","123456");
            String sql = "select * from news";
            Statement sment = conn.createStatement();
            ResultSet rs = sment.executeQuery(sql);
            while(rs.next()){
                HiveQueryResultSet hqrs = (HiveQueryResultSet)rs;
                System.out.println(hqrs.getString(1) "t" hqrs.getString(2));
            }
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

  在Hive数据库中有这样一个表 news

hive> use hive
    > ;
OK
Time taken: 4.244 seconds
hive> select * from news;
OK
   I‘m tom
   what are you doing
   i‘m ok
Time taken: 1.247 seconds, Fetched: 3 row(s)

  执行完Java代码以后,可以看到数据正常查询出来了

Hive 8、Hive2 beeline 和 Hive jdbc