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

来源:互联网 发布:网络硬盘播放器怎么用 编辑:程序博客网 时间:2024/05/22 04:47
我们可以通过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[wyp@localhost/home/q/hive-0.11.0]$ bin/hive --service hiveserver -p10002
2Starting Hive Thrift Server

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

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

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

01Create table success!
02Running: show tables 'wyphao'
03wyphao
04Running: describe wyphao
05key                     int                
06value                   string             
07Running: select count(1) from wyphao
080
09 
10Process finished with exit code 0
  如果你想在脚本里面运行,请将上面的程序打包成jar文件,并将上面的依赖库放在/home/wyp/lib/(这个根据你自己的情况弄)中,同时加入到运行的环境变量,脚本如下:
01#!/bin/bash
02HADOOP_HOME=/home/q/hadoop-2.2.0
03HIVE_HOME=/home/q/hive-0.11.0-bin
04 
05CLASSPATH=$CLASSPATH:
06 
07for i in /home/wyp/lib/*.jar ;do
08    CLASSPATH=$CLASSPATH:$i
09done
10 
11echo $CLASSPATH
12/home/q/java/jdk1.6.0_20/bin/java -cp  \
13   $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

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

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

其他的不变就可以了。

  这里顺便说说本程序所依赖的jar包,一共有以下几个:
1hadoop-2.2.0/share/hadoop/common/hadoop-common-2.2.0.jar
2$HIVE_HOME/lib/hive-exec-0.11.0.jar
3$HIVE_HOME/lib/hive-jdbc-0.11.0.jar
4$HIVE_HOME/lib/hive-metastore-0.11.0.jar 
5$HIVE_HOME/lib/hive-service-0.11.0.jar  
6$HIVE_HOME/lib/libfb303-0.9.0.jar  
7$HIVE_HOME/lib/commons-logging-1.0.4.jar 
8$HIVE_HOME/lib/slf4j-api-1.6.1.jar

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

01<dependency>
02        <groupId>org.apache.hive</groupId>
03        <artifactId>hive-jdbc</artifactId>
04        <version>0.11.0</version>
05</dependency>
06 
07<dependency>
08        <groupId>org.apache.hadoop</groupId>
09        <artifactId>hadoop-common</artifactId>
10        <version>2.2.0</version>
11</dependency>

尊重原创: 转载自过往记忆(http://www.iteblog.com/)

0 0