现在的位置: 首页 > 综合 > 正文

hiveapi通过Java程序调用

2019年05月22日 ⁄ 综合 ⁄ 共 5575字 ⁄ 字号 评论关闭

基于hadoop的Hive数据仓库JavaAPI简单调用的实例,关于Hive的简介在此不赘述。hive提供了三种用户接口:CLI,JDBC/ODBC和 WebUI

  1. CLI,即Shell命令行
  2. JDBC/ODBC 是 Hive 的Java,与使用传统数据库JDBC的方式类似
  3. WebGUI是通过浏览器访问 Hive

本文主要介绍的就是第二种用户接口,直接进入正题。

 1、Hive 安装:

        1)hive的安装请参考网上的相关文章,测试时只在hadoop一个节点上安装hive即可。

        2)测试数据data文件'\t'分隔:

              1   zhangsan

              2    lisi

              3   wangwu

        3)将测试数据data上传到linux目录下,我放置在:/home/hadoop01/data 

 2、在使用 JDBC 开发 Hive 程序时,  必须首先开启 Hive 的远程服务接口。使用下面命令进行开启:

  

Java代码  收藏代码
  1. hive --service hiveserver >/dev/null 2>/dev/null &  

 

 

 3、测试代码:

 

Java代码  收藏代码
  1. import java.sql.Connection;  
  2. import java.sql.DriverManager;  
  3. import java.sql.ResultSet;  
  4. import java.sql.SQLException;  
  5. import java.sql.Statement;  
  6.   
  7. import org.apache.log4j.Logger;  
  8.   
  9. /** 
  10.  * Hive的JavaApi 
  11.  *  
  12.  * 启动hive的远程服务接口命令行执行:hive --service hiveserver >/dev/null 2>/dev/null & 
  13.  *  
  14.  * @author 吖大哥 
  15.  *  
  16.  */  
  17. public class HiveJdbcCli {  
  18.   
  19.     private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";  
  20.     private static String url = "jdbc:hive://hadoop3:10000/default";  
  21.     private static String user = "hive";  
  22.     private static String password = "mysql";  
  23.     private static String sql = "";  
  24.     private static ResultSet res;  
  25.     private static final Logger log = Logger.getLogger(HiveJdbcCli.class);  
  26.   
  27.     public static void main(String[] args) {  
  28.         Connection conn = null;  
  29.         Statement stmt = null;  
  30.         try {  
  31.             conn = getConn();  
  32.             stmt = conn.createStatement();  
  33.   
  34.             // 第一步:存在就先删除  
  35.             String tableName = dropTable(stmt);  
  36.   
  37.             // 第二步:不存在就创建  
  38.             createTable(stmt, tableName);  
  39.   
  40.             // 第三步:查看创建的表  
  41.             showTables(stmt, tableName);  
  42.   
  43.             // 执行describe table操作  
  44.             describeTables(stmt, tableName);  
  45.   
  46.             // 执行load data into table操作  
  47.             loadData(stmt, tableName);  
  48.   
  49.             // 执行 select * query 操作  
  50.             selectData(stmt, tableName);  
  51.   
  52.             // 执行 regular hive query 统计操作  
  53.             countData(stmt, tableName);  
  54.   
  55.         } catch (ClassNotFoundException e) {  
  56.             e.printStackTrace();  
  57.             log.error(driverName + " not found!", e);  
  58.             System.exit(1);  
  59.         } catch (SQLException e) {  
  60.             e.printStackTrace();  
  61.             log.error("Connection error!", e);  
  62.             System.exit(1);  
  63.         } finally {  
  64.             try {  
  65.                 if (conn != null) {  
  66.                     conn.close();  
  67.                     conn = null;  
  68.                 }  
  69.                 if (stmt != null) {  
  70.                     stmt.close();  
  71.                     stmt = null;  
  72.                 }  
  73.             } catch (SQLException e) {  
  74.                 e.printStackTrace();  
  75.             }  
  76.         }  
  77.     }  
  78.   
  79.     private static void countData(Statement stmt, String tableName)  
  80.             throws SQLException {  
  81.         sql = "select count(1) from " + tableName;  
  82.         System.out.println("Running:" + sql);  
  83.         res = stmt.executeQuery(sql);  
  84.         System.out.println("执行“regular hive query”运行结果:");  
  85.         while (res.next()) {  
  86.             System.out.println("count ------>" + res.getString(1));  
  87.         }  
  88.     }  
  89.   
  90.     private static void selectData(Statement stmt, String tableName)  
  91.             throws SQLException {  
  92.         sql = "select * from " + tableName;  
  93.         System.out.println("Running:" + sql);  
  94.         res = stmt.executeQuery(sql);  
  95.         System.out.println("执行 select * query 运行结果:");  
  96.         while (res.next()) {  
  97.             System.out.println(res.getInt(1) + "\t" + res.getString(2));  
  98.         }  
  99.     }  
  100.   
  101.     private static void loadData(Statement stmt, String tableName)  
  102.             throws SQLException {  
  103.         String filepath = "/home/hadoop01/data";  
  104.         sql = "load data local inpath '" + filepath + "' into table "  
  105.                 + tableName;  
  106.         System.out.println("Running:" + sql);  
  107.         res = stmt.executeQuery(sql);  
  108.     }  
  109.   
  110.     private static void describeTables(Statement stmt, String tableName)  
  111.             throws SQLException {  
  112.         sql = "describe " + tableName;  
  113.         System.out.println("Running:" + sql);  
  114.         res = stmt.executeQuery(sql);  
  115.         System.out.println("执行 describe table 运行结果:");  
  116.         while (res.next()) {  
  117.             System.out.println(res.getString(1) + "\t" + res.getString(2));  
  118.         }  
  119.     }  
  120.   
  121.     private static void showTables(Statement stmt, String tableName)  
  122.             throws SQLException {  
  123.         sql = "show tables '" + tableName + "'";  
  124.         System.out.println("Running:" + sql);  
  125.         res = stmt.executeQuery(sql);  
  126.         System.out.println("执行 show tables 运行结果:");  
  127.         if (res.next()) {  
  128.             System.out.println(res.getString(1));  
  129.         }  
  130.     }  
  131.   
  132.     private static void createTable(Statement stmt, String tableName)  
  133.             throws SQLException {  
  134.         sql = "create table "  
  135.                 + tableName  
  136.                 + " (key int, value string)  row format delimited fields terminated by '\t'";  
  137.         stmt.executeQuery(sql);  
  138.     }  
  139.   
  140.     private static String dropTable(Statement stmt) throws SQLException {  
  141.         // 创建的表名  
  142.         String tableName = "testHive";  
  143.         sql = "drop table " + tableName;  
  144.         stmt.executeQuery(sql);  
  145.         return tableName;  
  146.     }  
  147.   
  148.     private static Connection getConn() throws ClassNotFoundException,  
  149.             SQLException {  
  150.         Class.forName(driverName);  
  151.         Connection conn = DriverManager.getConnection(url, user, password);  
  152.         return conn;  
  153.     }  
  154.   
  155. }  

 

4、测试结果

 

5、终端查询结果:

hive> select * from testHive;

OK

1       zhangsan

2       lisi

3       wangwu

Time taken: 11.232 seconds

抱歉!评论已关闭.