分享

hive如何用Java代码通过JDBC连接Hiveserver

NEOGX 2014-1-11 01:27:29 发表于 实操演练 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 0 8393
本帖最后由 NEOGX 于 2014-1-11 01:28 编辑

 我们可以通过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 -p 10002
  2. Starting Hive Thrift Server
复制代码
上面代表你已经成功的在端口为10002(默认的端口是10000)启动了hiveserver服务。这时候,你就可以通过Java代码来连接hiveserver,代码如下:
  1. package com.wyp;
  2. /**
  3. * User: 过往记忆
  4. * Blog: http://www.iteblog.com/
  5. * Date: 13-11-27
  6. * Time: 下午5:52
  7. */
  8. import java.sql.SQLException;
  9. import java.sql.Connection;
  10. import java.sql.ResultSet;
  11. import java.sql.Statement;
  12. import java.sql.DriverManager;
  13. public class HiveJdbcTest {
  14.    
  15.     private static String driverName =
  16.                    "org.apache.hadoop.hive.jdbc.HiveDriver";
  17.   
  18.     public static void main(String[] args)
  19.                             throws SQLException {
  20.         try {
  21.             Class.forName(driverName);
  22.         } catch (ClassNotFoundException e) {
  23.             e.printStackTrace();
  24.             System.exit(1);
  25.         }
  26.         Connection con = DriverManager.getConnection(
  27.                            "jdbc:hive://localhost:10002/default", "wyp", "");
  28.         Statement stmt = con.createStatement();
  29.         String tableName = "wyphao";
  30.         stmt.execute("drop table if exists " + tableName);
  31.         stmt.execute("create table " + tableName +
  32.                                      " (key int, value string)");
  33.         System.out.println("Create table success!");
  34.         // show tables
  35.         String sql = "show tables '" + tableName + "'";
  36.         System.out.println("Running: " + sql);
  37.         ResultSet res = stmt.executeQuery(sql);
  38.         if (res.next()) {
  39.             System.out.println(res.getString(1));
  40.         }
  41.         // describe table
  42.         sql = "describe " + tableName;
  43.         System.out.println("Running: " + sql);
  44.         res = stmt.executeQuery(sql);
  45.         while (res.next()) {
  46.             System.out.println(res.getString(1) + "\t" + res.getString(2));
  47.         }
  48.         sql = "select * from " + tableName;
  49.         res = stmt.executeQuery(sql);
  50.         while (res.next()) {
  51.             System.out.println(String.valueOf(res.getInt(1)) + "\t"
  52.                                                + res.getString(2));
  53.         }
  54.         sql = "select count(1) from " + tableName;
  55.         System.out.println("Running: " + sql);
  56.         res = stmt.executeQuery(sql);
  57.         while (res.next()) {
  58.             System.out.println(res.getString(1));
  59.         }
  60.     }
  61. }
复制代码
编译上面的代码,之后就可以运行(我是在集成开发环境下面运行这个程序的),结果如下:
  1. Create table success!
  2. Running: show tables 'wyphao'
  3. wyphao
  4. Running: describe wyphao
  5. key                         int                 
  6. value                       string              
  7. Running: select count(1) from wyphao
  8. 0
  9. Process finished with exit code 0
复制代码
如果你想在脚本里面运行,请将上面的程序打包成jar文件,并将上面的依赖库放在/home/wyp/lib/(这个根据你自己的情况弄)中,同时加入到运行的环境变量,脚本如下:
  1. #!/bin/bash
  2. HADOOP_HOME=/home/q/hadoop-2.2.0
  3. HIVE_HOME=/home/q/hive-0.11.0-bin
  4. CLASSPATH=$CLASSPATH:
  5. for i in /home/wyp/lib/*.jar ; do
  6.     CLASSPATH=$CLASSPATH:$i
  7. done
  8. echo $CLASSPATH
  9. /home/q/java/jdk1.6.0_20/bin/java -cp  \
  10.    $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
复制代码
两种方式效果都一样的。但是以前的程序需要修改两个地方,如下所示:
  1. private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";
  2. 改为
  3. private static String driverName = "org.apache.hive.jdbc.HiveDriver";
  4. Connection con = DriverManager.getConnection(
  5.                            "jdbc:hive://localhost:10002/default", "wyp", "");
  6. 改为
  7. Connection con = DriverManager.getConnection(
  8.                            "jdbc:hive2://localhost:10002/default", "wyp", "");
复制代码
其他的不变就可以了。

这里顺便说说本程序所依赖的jar包,一共有以下几个:
  1. hadoop-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,加入以下依赖
  1. <dependency>
  2.         <groupId>org.apache.hive</groupId>
  3.         <artifactId>hive-jdbc</artifactId>
  4.         <version>0.11.0</version>
  5. </dependency>
  6. <dependency>
  7.         <groupId>org.apache.hadoop</groupId>
  8.         <artifactId>hadoop-common</artifactId>
  9.         <version>2.2.0</version>
  10. </dependency>
复制代码
欢迎加入about云群371358502、39327136,云计算爱好者群

没找到任何评论,期待你打破沉寂

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条