Hive:用Java代码通过JDBC连接Hiveserver

来源:互联网 发布:店铺淘宝客交流论坛 编辑:程序博客网 时间:2024/05/22 01:25

填写您的邮件地址,订阅我们的精彩内容:



  写在前面的话,学Hive这么久了,发现目前国内还没有一本完整的介绍Hive的书籍,而且互联网上面的资料很乱,于是我决定写一些关于《Hive的那些事》序列文章,分享给大家。我会在接下来的时间整理有关Hive的资料,如果对Hive的东西感兴趣,请关注本博客。

  我们可以通过CLI、Client、Web UI等Hive提供的用户接口来和Hive通信,但这三种方式最常用的是CLI;Client 是Hive的客户端,用户连接至 Hive Server。在启动 Client 模式的时候,需要指出Hive Server所在节点,并且在该节点启动 Hive Server。 WUI 是通过浏览器访问 Hive。今天我们来谈谈怎么通过HiveServer来操作Hive。

  Hive提供了jdbc驱动,使得我们可以用Java代码来连接Hive并进行一些类关系型数据库的sql语句查询等操作。同关系型数据库一样,我们也需要将Hive的服务打开;在Hive 0.11.0版本之前,只有HiveServer服务可用,你得在程序操作Hive之前,必须在Hive安装的服务器上打开HiveServer服务,如下:

1
2
[wyp@localhost/home/q/hive-0.11.0]$ bin/hive --service hiveserver -p 10002
Starting Hive Thrift Server

上面代表你已经成功的在端口为10002(默认的端口是10000)启动了hiveserver服务。这时候,你就可以通过Java代码来连接hiveserver,代码如下:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
packagecom.wyp;
/**
 * User: 过往记忆
 * Blog: http://www.iteblog.com/
 * Date: 13-11-27
 * Time: 下午5:52
 */
importjava.sql.SQLException;
importjava.sql.Connection;
importjava.sql.ResultSet;
importjava.sql.Statement;
importjava.sql.DriverManager;
 
publicclass HiveJdbcTest {
     
    privatestatic String driverName =
                   "org.apache.hadoop.hive.jdbc.HiveDriver";
   
    publicstatic void main(String[] args)
                            throwsSQLException {
        try{
            Class.forName(driverName);
        }catch(ClassNotFoundException e) {
            e.printStackTrace();
            System.exit(1);
        }
 
        Connection con = DriverManager.getConnection(
                           "jdbc:hive://localhost:10002/default","wyp","");
        Statement stmt = con.createStatement();
        String tableName = "wyphao";
        stmt.execute("drop table if exists " + tableName);
        stmt.execute("create table " + tableName +
                                     " (key int, value string)");
        System.out.println("Create table success!");
        // 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));
        }
 
 
        sql = "select * from " + tableName;
        res = stmt.executeQuery(sql);
        while(res.next()) {
            System.out.println(String.valueOf(res.getInt(1)) + "\t"
                                               + res.getString(2));
        }
 
        sql = "select count(1) from " + tableName;
        System.out.println("Running: " + sql);
        res = stmt.executeQuery(sql);
        while(res.next()) {
            System.out.println(res.getString(1));
        }
    }
}

编译上面的代码,之后就可以运行(我是在集成开发环境下面运行这个程序的),结果如下:

01
02
03
04
05
06
07
08
09
10
Create table success!
Running: show tables 'wyphao'
wyphao
Running: describe wyphao
key                    int                
value                   string             
Running: select count(1) from wyphao
0
 
Process finished with exit code 0
  如果你想在脚本里面运行,请将上面的程序打包成jar文件,并将上面的依赖库放在/home/wyp/lib/(这个根据你自己的情况弄)中,同时加入到运行的环境变量,脚本如下:

01
02
03
04
05
06
07
08
09
10
11
12
13
#!/bin/bash
HADOOP_HOME=/home/q/hadoop-2.2.0
HIVE_HOME=/home/q/hive-0.11.0-bin
 
CLASSPATH=$CLASSPATH:
 
fori in /home/wyp/lib/*.jar ; do
    CLASSPATH=$CLASSPATH:$i
done
 
echo $CLASSPATH
/home/q/java/jdk1.6.0_20/bin/java -cp  \
   $CLASSPATH:/export1/tmp/yangping.wu/OutputText.jar  com.wyp.HiveJdbcTest

上面是用Java连接HiveServer,而HiveServer本身存在很多问题(比如:安全性、并发性等);针对这些问题,Hive0.11.0版本提供了一个全新的服务:HiveServer2,这个很好的解决HiveServer存在的安全性、并发性等问题。这个服务启动程序在${HIVE_HOME}/bin/hiveserver2里面,你可以通过下面的方式来启动HiveServer2服务:

1
$HIVE_HOME/bin/hiveserver2

也可以通过下面的方式启动HiveServer2

1
$HIVE_HOME/bin/hive --service hiveserver2

两种方式效果都一样的。但是以前的程序需要修改两个地方,如下所示:

01
02
03
04
05
06
07
08
09
10
11
privatestatic String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";
改为
privatestatic String driverName = "org.apache.hive.jdbc.HiveDriver";
 
 
 
Connection con = DriverManager.getConnection(
                           "jdbc:hive://localhost:10002/default","wyp","");
改为
Connection con = DriverManager.getConnection(
                           "jdbc:hive2://localhost:10002/default","wyp","");

其他的不变就可以了。

  这里顺便说说本程序所依赖的jar包,一共有以下几个:

1
2
3
4
5
6
7
8
hadoop-2.2.0/share/hadoop/common/hadoop-common-2.2.0.jar
$HIVE_HOME/lib/hive-exec-0.11.0.jar
$HIVE_HOME/lib/hive-jdbc-0.11.0.jar
$HIVE_HOME/lib/hive-metastore-0.11.0.jar 
$HIVE_HOME/lib/hive-service-0.11.0.jar  
$HIVE_HOME/lib/libfb303-0.9.0.jar  
$HIVE_HOME/lib/commons-logging-1.0.4.jar 
$HIVE_HOME/lib/slf4j-api-1.6.1.jar

  如果你是用Maven,加入以下依赖

01
02
03
04
05
06
07
08
09
10
11
<dependency>
        <groupId>org.apache.hive</groupId>
        <artifactId>hive-jdbc</artifactId>
        <version>0.11.0</version>
</dependency>
 
<dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-common</artifactId>
        <version>2.2.0</version>
</dependency>
本博客文章除特别声明,全部都是原创!

尊重原创,转载请注明: 转载自过往记忆(http://www.iteblog.com/)
本文链接地址: 《Hive:用Java代码通过JDBC连接Hiveserver》(http://www.iteblog.com/archives/846)
E-mail:wyphao.2007@163.com    QQ:397090770
原创粉丝点击