hive的jdbc

来源:互联网 发布:林珊珊的淘宝店 编辑:程序博客网 时间:2024/04/30 08:25

原文地址: http://blog.csdn.net/nsrainbow/article/details/43002387   最新课程请关注原作者博客,获得更好的显示体验

声明

  • 本文基于Centos 6.x + CDH 5.x
说到Hive就一定要说到写程序的时候怎么调用Hive。以下我通过一个例子说明如果通过java来调用hive查询数据

服务准备

使用Jdbc方式链接hive,首先需要启动hive的Thrift Server,否则会导致错误“Could not establish connection to localhost:10000/default: java.net.ConnectException: Connection refused”

hive --service hiveserver   是两”-“, 


数据准备

建立一个文本文件叫 a.txt,内容是
[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. 1,terry  
  2. 2,alex  
  3. 3,jimmy  
  4. 4,mike  
  5. 5,kate  

并上传到hive服务器的  /data/ 目录下

JDBC调用方法

加载Driver

加载driver (只说hive2的jdbc)
[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. Class.forName("org.apache.hive.jdbc.HiveDriver");  

连接数据库

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. Connection con = DriverManager.getConnection("jdbc:hive2://host1:10000/default""hive""");  

  • 这里的 host1 是主机名
  • 10000是hive默认的端口名
  • default是默认的database
  • hive是默认的用户名,默认密码是空

数据库操作语句

删除表
[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. stmt.execute("drop table if exists " + tableName);  

创建表
[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. stmt.execute("create table " + tableName + " (key int, value string) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\054'");  

查询数据
[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. ResultSet res = stmt.executeQuery("select * from " + tableName);  

导入数据
[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. stmt.execute("load data local inpath '" + filepath + "' into table " + tableName);  


例子

建立项目

先打开eclipse建立一个maven项目


pom.xml
[html] view plain copy
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.   
  5.     <groupId>org.crazycake</groupId>  
  6.     <artifactId>play-hive</artifactId>  
  7.     <version>0.0.1-SNAPSHOT</version>  
  8.     <packaging>jar</packaging>  
  9.   
  10.     <name>play-hive</name>  
  11.     <url>http://maven.apache.org</url>  
  12.   
  13.     <properties>  
  14.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  15.     </properties>  
  16.   
  17.     <dependencies>  
  18.         <dependency>  
  19.             <groupId>junit</groupId>  
  20.             <artifactId>junit</artifactId>  
  21.             <version>3.8.1</version>  
  22.             <scope>test</scope>  
  23.         </dependency>  
  24.         <dependency>  
  25.             <groupId>org.apache.hive</groupId>  
  26.             <artifactId>hive-jdbc</artifactId>  
  27.             <version>0.14.0</version>  
  28.         </dependency>  
  29.         <dependency>  
  30.             <groupId>org.apache.hadoop</groupId>  
  31.             <artifactId>hadoop-common</artifactId>  
  32.             <version>2.2.0</version>  
  33.         </dependency>  
  34.     </dependencies>  
  35.   
  36.     <build>  
  37.         <plugins>  
  38.             <plugin>  
  39.                 <artifactId>maven-compiler-plugin</artifactId>  
  40.                 <version>2.0.2</version>  
  41.                 <configuration>  
  42.                     <source>1.6</source>  
  43.                     <target>1.6</target>  
  44.                     <encoding>UTF-8</encoding>  
  45.                     <optimise>true</optimise>  
  46.                     <compilerArgument>-nowarn</compilerArgument>  
  47.                 </configuration>  
  48.             </plugin>  
  49.             <plugin>  
  50.                 <groupId>org.apache.maven.plugins</groupId>  
  51.                 <artifactId>maven-shade-plugin</artifactId>  
  52.                 <version>2.3</version>  
  53.                 <configuration>  
  54.                     <transformers>  
  55.                         <transformer  
  56.                             implementation="org.apache.maven.plugins.shade.resource.ApacheLicenseResourceTransformer">  
  57.                         </transformer>  
  58.                     </transformers>  
  59.                 </configuration>  
  60.                 <executions>  
  61.                     <execution>  
  62.                         <phase>package</phase>  
  63.                         <goals>  
  64.                             <goal>shade</goal>  
  65.                         </goals>  
  66.                     </execution>  
  67.                 </executions>  
  68.             </plugin>  
  69.         </plugins>  
  70.     </build>  
  71. </project>  



其中最重要的就是这两段
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <dependency>  
  2.     <groupId>org.apache.hive</groupId>  
  3.     <artifactId>hive-jdbc</artifactId>  
  4.     <version>0.14.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>  

其他的都无所谓

建表、导入以及查询数据

建立一个类 HiveJdbcClient
[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package org.crazycake.play_hive;  
  2. import java.sql.SQLException;  
  3. import java.sql.Connection;  
  4. import java.sql.ResultSet;  
  5. import java.sql.Statement;  
  6. import java.sql.DriverManager;  
  7.   
  8. /** 
  9.  * 测试hive 的客户端连接 
  10.  * @author alexxiyang (https://github.com/alexxiyang) 
  11.  * 
  12.  */  
  13. public class HiveJdbcClient {  
  14.     
  15.   /** 
  16.    * 注意:hive-server2 引用的driver是  org.apache.hive.* 而 hive-server 是 org.apache.hadoop.hive.* 
  17.    */  
  18.   private static String driverName = "org.apache.hive.jdbc.HiveDriver";  
  19.    
  20.   /** 
  21.    * @param args 
  22.    * @throws SQLException 
  23.    */  
  24.   public static void main(String[] args) throws SQLException {  
  25.       try {  
  26.       Class.forName(driverName);  
  27.     } catch (ClassNotFoundException e) {  
  28.       // TODO Auto-generated catch block  
  29.       e.printStackTrace();  
  30.       System.exit(1);  
  31.     }  
  32.     //hive的默认端口是 10000,如果要修改就修改 hive-site.xml 文件的hive.server2.thrift.port 属性值  
  33.     //默认用户名hive,默认密码为空  
  34.     Connection con = DriverManager.getConnection("jdbc:hive2://host1:10000/default""hive""");  
  35.       
  36.     Statement stmt = con.createStatement();  
  37.     //测试的表名 testhivedrivertable  
  38.     String tableName = "testhivedrivertable";  
  39.       
  40.     //如果已经存在就删除  
  41.     stmt.execute("drop table if exists " + tableName);  
  42.       
  43.     //创建这张表  
  44.     stmt.execute("create table " + tableName + " (key int, value string) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\054'");  
  45.     //看下创建是否成功  
  46.     String sql = "show tables '" + tableName + "'";  
  47.     System.out.println("Running: " + sql);  
  48.     ResultSet res = stmt.executeQuery(sql);  
  49.     if (res.next()) {  
  50.       System.out.println(res.getString(1));  
  51.     }  
  52.       
  53.     //看下表结构  
  54.     sql = "describe " + tableName;  
  55.     System.out.println("Running: " + sql);  
  56.     res = stmt.executeQuery(sql);  
  57.     while (res.next()) {  
  58.       System.out.println(res.getString(1) + "\t" + res.getString(2));  
  59.     }  
  60.    
  61.     // 加载数据到表里面  
  62.     // NOTE: filepath 是本地文件所在的位置,注意这个本地不是你的电脑!  
  63.     // 你得先把这个文件上传到服务器,然后这里的路径是服务器上这个文件的路径  
  64.     // NOTE: /data/a.txt  
  65.     String filepath = "/data/a.txt";  
  66.     sql = "load data local inpath '" + filepath + "' into table " + tableName;  
  67.     System.out.println("Running: " + sql);  
  68.     stmt.execute(sql);  
  69.    
  70.     // select * query  
  71.     sql = "select * from " + tableName;  
  72.     System.out.println("Running: " + sql);  
  73.     res = stmt.executeQuery(sql);  
  74.     while (res.next()) {  
  75.       System.out.println(String.valueOf(res.getInt(1)) + "\t" + res.getString(2));  
  76.     }  
  77.   }  
  78. }  


输出是
[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. Running: show tables 'testhivedrivertable'  
  2. testhivedrivertable  
  3. Running: describe testhivedrivertable  
  4. key int  
  5. value   string  
  6. Running: load data local inpath '/data/a.txt' into table testhivedrivertable  
  7. Running: select * from testhivedrivertable  
  8. 1   terry  
  9. 2   alex  
  10. 3   jimmy  
  11. 4   mike  
  12. 5   kate  
  13. Running: select count(1) from testhivedrivertable  

其实java调用很简单的,就是把你在hive shell 里面执行的语句用jdbc执行一遍而已,所以你传输过去的语句的环境是hive server机器,里面写的路径也是从hive server主机的根目录路径出发去寻找数据,所以我们的 a.txt 得上传到服务器上,这段代码才会运行正常。
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 金税盘处于报税期不能开票怎么办 小规模税率开错了怎么办 我是代购卖家被买家投诉偷税怎么办 天猫盒子内存不够怎么办 天猫品牌申请不通过怎么办 天猫商家发货发个空包裹怎么办 无限流量怎么办没有4g 海外直邮身份证过期了怎么办 买车的人不过户怎么办 天猫精灵球泡离线怎么办 花呗被骗了2万怎么办 天猫公司变更地址发票怎么办 支付宝自助解限怎么办 支付宝16岁限额怎么办 支付宝提不了现怎么办 支付宝余额受限需要身份证怎么办 微信被骗了6000怎么办 被代运营骗了该怎么办 淘宝店铺过节放假无人打理怎么办 淘宝店太久没打理出现未开店怎么办 淘宝店关了售后怎么办 发货运单号发错了怎么办 天猫积分为零怎么办 山东聊城小型车脱审一年怎么办? 廉租房如果夫妻离婚怎么办 淘宝客服不给退货怎么办 天猫客服打字慢怎么办 京东买的kindle坏了怎么办 欧巴怎么办韩语怎么写 聚划算淘宝口令打不开怎么办 道聚城白银礼包下架怎么办 聚星输了很多钱怎么办 弹力运动裤被烟烧了个洞怎么办 生完宝宝胯宽怎么办 黑色纯棉裤子洗的发白怎么办 金盾保险柜密码忘了怎么办 装修好的房子漏水怎么办 刚装修的房子墙面开裂怎么办 刚装修的房子有味道怎么办 代销产品规格填写不完整怎么办 我的信息被泄露怎么办