Hive用户接口(二)—使用Hive JDBC驱动连接Hive操作实例

Hive用户接口(二)—使用Hive JDBC驱动连接Hive操作实例

原创  2015年01月12日 10:34:11
  • 13185

问题导读:

        1、Hive提供了哪三种用户访问方式?

        2、使用HiveServer时候,需要首先启动哪个服务?

        3、HiveServer的启动命令是?

        4、HiveServer是通过哪个服务来提供远程JDBC访问的?

        5、如何修改HiveServer的默认启动端口?

        6、Hive JDBC驱动连接需要哪些包?

        7、HiveServer2与HiveServer在使用上的不同点?

        Hive提供了三种用户接口:CLI、HWI和客户端。其中客户端即是使用JDBC驱动通过thrift,远程操作Hive。HWI即提供Web界面远程访问Hive,可参考我的另外一篇博文:Hive用户接口(一)—Hive Web接口HWI的操作及使用。但是最常见的使用方式还是使用CLI方式。下面介绍Hive使用JDBC驱动连接操作Hive,我的Hive版本是Hive-0.13.1。

         Hive JDBC驱动连接分为两种,早期的是HiveServer,最新的是HiveServer2,前者本身存在很多的问题,如安全性、并发性等,后者很好的解决了诸如安全性和并发性等问题。我先介绍HiveServer的用法。

一、启动元数据MetaStore

        使用任何方式连接Hive,都首先需要启动Hive元数据服务,否则执行HQL操作无法进行。

[html]  view plain  copy
  1. [hadoopUser@secondmgt ~]$ hive --service metastore  
  2. Starting Hive Metastore Server  
  3. 15/01/11 20:11:56 INFO Configuration.deprecation: mapred.reduce.tasks is deprecated. Instead, use mapreduce.job.reduces  
  4. 15/01/11 20:11:56 INFO Configuration.deprecation: mapred.min.split.size is deprecated. Instead, use mapreduce.input.fileinputformat.split.minsize  
  5. 15/01/11 20:11:56 INFO Configuration.deprecation: mapred.reduce.tasks.speculative.execution is deprecated. Instead, use mapreduce.reduce.speculative  
  6. 15/01/11 20:11:56 INFO Configuration.deprecation: mapred.min.split.size.per.node is deprecated. Instead, use mapreduce.input.fileinputformat.split.minsize.per.node  
  7. 15/01/11 20:11:56 INFO Configuration.deprecation: mapred.input.dir.recursive is deprecated. Instead, use mapreduce.input.fileinputformat.input.dir.recursive  
二、启动HiveServer服务

        HiveServer使用thrift服务来为客户端提供远程连接的访问端口,在JDBC连接Hive之前必须先启动HiveServer。

[html]  view plain  copy
  1. [hadoopUser@secondmgt ~]$ hive --service hiveserver  
  2. Starting Hive Thrift Server  
  3. 15/01/12 10:22:54 INFO Configuration.deprecation: mapred.reduce.tasks is deprecated. Instead, use mapreduce.job.reduces  
  4. 15/01/12 10:22:54 INFO Configuration.deprecation: mapred.min.split.size is deprecated. Instead, use mapreduce.input.fileinputformat.split.minsize  
  5. 15/01/12 10:22:54 INFO Configuration.deprecation: mapred.reduce.tasks.speculative.execution is deprecated. Instead, use mapreduce.reduce.speculative  
  6. 15/01/12 10:22:54 INFO Configuration.deprecation: mapred.min.split.size.per.node is deprecated. Instead, use mapreduce.input.fileinputformat.split.minsize.per.node  
  7. 15/01/12 10:22:54 INFO Configuration.deprecation: mapred.input.dir.recursive is deprecated. Instead, use mapreduce.input.fileinputformat.input.dir.recursive  
         hiveserver默认端口是10000,可以使用 hive --service hiveserver -p 10002,更改默认启动端口,此端口也是JDBC连接端口。

         注意:hiveserver不能和hwi服务同时启动使用。

三、在IDE中创建Hive工程

       我们使用Eclipse作为开发IDE,在Eclipse中创建hive工程,并导入Hive JDBC远程连接相关包,所需的包如下所示:

[html]  view plain  copy
  1. hive-jdbc-0.13.1.jar  
  2. commons-logging-1.1.3.jar  
  3. hive-exec-0.13.1.jar  
  4. hive-metastore-0.13.1.jar  
  5. hive-service-0.13.1.jar  
  6. libfb303-0.9.0.jar  
  7. slf4j-api-1.6.1.jar  
  8. hadoop-common-2.2.0.jar  
  9. log4j-1.2.16.jar  
  10. slf4j-nop-1.6.1.jar  
  11. httpclient-4.2.5.jar  
  12. httpcore-4.2.5.jar  
四、编写连接与查询代码

[html]  view plain  copy
  1. package com.gxnzx.hive;  
  2.   
  3. import java.sql.Connection;  
  4. import java.sql.DriverManager;  
  5. import java.sql.ResultSet;  
  6. import java.sql.SQLException;  
  7. import java.sql.Statement;  
  8.   
  9. public class HiveServer2 {  
  10.   
  11. private static Connection conn=null;  
  12.   
  13.         public static void main(String args[]){  
  14.   
  15.                 try {  
  16.                           Class.forName("org.apache.hadoop.hive.jdbc.HiveDriver");  
  17.   
  18.                           conn=DriverManager.getConnection("jdbc:hive://192.168.2.133:10000/hive", "hadoopUser", "");  
  19.   
  20.                           Statement st=conn.createStatement();  
  21.   
  22.                           String sql1="select name,age from log";  
  23.                            
  24.                           ResultSet rs=st.executeQuery(sql1);  
  25.   
  26.                           while(rs.next()){  
  27.   
  28.                                   System.out.println(rs.getString(1)+"     "+rs.getString(2));  
  29.                           }  
  30.   
  31.                 } catch (ClassNotFoundException e) {  
  32.   
  33.                         e.printStackTrace();  
  34.                 } catch (SQLException e) {  
  35.   
  36.                         e.printStackTrace();  
  37.                 }  
  38.         }  
  39. }  
         其中:org.apache.hive.jdbc.HiveDriver是Hive JDBC连接驱动名,使用DriverManager.getConnection("jdbc:hive2://<host>:<port>", "<user>", "");创建连接。运行结果如下:

[html]  view plain  copy
  1. Tom     19  
  2. Jack     21  
  3. HaoNing     12  
  4. Hadoop     20  
  5. Rose     23  
五、HiveServer2与HiveServer的区别

         hiveserver2在安全性和并发性等方面比hiveserver好,在JDBC实现上面差别不大,主要有以下方面不同:

        1、服务启动不一样,首先要启动hiveserver2服务

[html]  view plain  copy
  1. [hadoopUser@secondmgt ~]$ hive --service hiveserver2  
  2. Starting HiveServer2  
  3. 15/01/12 10:13:42 INFO Configuration.deprecation: mapred.reduce.tasks is deprecated. Instead, use mapreduce.job.reduces  
  4. 15/01/12 10:13:42 INFO Configuration.deprecation: mapred.min.split.size is deprecated. Instead, use mapreduce.input.fileinputformat.split.minsize  
  5. 15/01/12 10:13:42 INFO Configuration.deprecation: mapred.reduce.tasks.speculative.execution is deprecated. Instead, use mapreduce.reduce.speculative  
  6. 15/01/12 10:13:42 INFO Configuration.deprecation: mapred.min.split.size.per.node is deprecated. Instead, use mapreduce.input.fileinputformat.split.minsize.per.node  
  7. 15/01/12 10:13:42 INFO Configuration.deprecation: mapred.input.dir.recursive is deprecated. Instead, use mapreduce.input.fileinputformat.input.dir.recursive  

        2、驱动名不一样

[html]  view plain  copy
  1. HiveServer—>org.apache.hadoop.hive.jdbc.HiveDriver  
  2.   
  3. HiveServer2—>org.apache.hive.jdbc.HiveDriver  
       3、创建连接不一样

[html]  view plain  copy
  1. HiveServer—>DriverManager.getConnection("jdbc:hive://<host>:<port>", "<user>", "");  
  2.   
  3. HiveServer2—>DriverManager.getConnection("jdbc:hive2://<host>:<port>", "<user>", "");  
        4、完整实例

[html]  view plain  copy
  1. package com.gxnzx.hive;  
  2.   
  3. import java.sql.Connection;  
  4. import java.sql.DriverManager;  
  5. import java.sql.ResultSet;  
  6. import java.sql.SQLException;  
  7. import java.sql.Statement;  
  8.   
  9. public class HiveJDBCTest {  
  10.   
  11.         private static Connection conn=null;  
  12.   
  13.         public static void main(String args[]){  
  14.   
  15.                 try {  
  16.                           Class.forName("org.apache.hive.jdbc.HiveDriver");  
  17.   
  18.                           conn=DriverManager.getConnection("jdbc:hive2://192.168.2.133:10000/hive", "hadoopUser", "");  
  19.   
  20.                           Statement st=conn.createStatement();  
  21.   
  22.                           String sql1="select name,age from log";  
  23.   
  24.                           ResultSet rs=st.executeQuery(sql1);  
  25.   
  26.                           while(rs.next()){  
  27.   
  28.                                   System.out.println(rs.getString(1)+"     "+rs.getString(2));  
  29.                           }  
  30.   
  31.                 } catch (ClassNotFoundException e) {  
  32.   
  33.                         e.printStackTrace();  
  34.                 } catch (SQLException e) {  
  35.   
  36.                         e.printStackTrace();  
  37.                 }  
  38.   
  39.   
  40.         }  
  41. }  
附:相关异常及解决办法

       异常或错误一

[html]  view plain  copy
  1. SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".  
  2. SLF4J: Defaulting to no-operation (NOP) logger implementation  
  3. Failed to load class org.slf4j.impl.StaticLoggerBinder  
      官方解决方法

[html]  view plain  copy
  1. This error is reported when the org.slf4j.impl.StaticLoggerBinder class could not be loaded into memory. This happens when no appropriate SLF4J binding could be found on the class path. Placing one (and only one) of slf4j-nop.jar, slf4j-simple.jar, slf4j-log4j12.jar, slf4j-jdk14.jar or logback-classic.jar on the class path should solve the problem.  
  2.   
  3. since 1.6.0 As of SLF4J version 1.6, in the absence of a binding, SLF4J will default to a no-operation (NOP) logger implementation.  
     将slf4j-nop.jar, slf4j-simple.jar, slf4j-log4j12.jar, slf4j-jdk14.jar 或者logback-classic.jar中的 任何一个导入到工程lib下,slf4j相关包下载地址如下: slf4j bindings
     异常或错误二

[html]  view plain  copy
  1. Job Submission failed with exception 'org.apache.hadoop.security.AccessControlException(Permission denied: user=anonymous,   
  2.   
  3. access=EXECUTEinode="/tmp":hadoopUser:supergroup:drwx------  
  4.         at org.apache.hadoop.hdfs.server.namenode.FSPermissionChecker.check(FSPermissionChecker.java:234)  
  5.         at org.apache.hadoop.hdfs.server.namenode.FSPermissionChecker.checkTraverse(FSPermissionChecker.java:187)  
  6.         at org.apache.hadoop.hdfs.server.namenode.FSPermissionChecker.checkPermission(FSPermissionChecker.java:150)  
  7.         at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.checkPermission(FSNamesystem.java:5185)  
  8.         at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.checkPermission(FSNamesystem.java:5167)  
  9.         at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.checkOwner(FSNamesystem.java:5123)  
  10.         at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.setPermissionInt(FSNamesystem.java:1338)  
  11.         at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.setPermission(FSNamesystem.java:1317)  
  12.         at org.apache.hadoop.hdfs.server.namenode.NameNodeRpcServer.setPermission(NameNodeRpcServer.java:528)  
  13.         at org.apache.hadoop.hdfs.protocolPB.ClientNamenodeProtocolServerSideTranslatorPB.setPermission  
  14.   
  15. (ClientNamenodeProtocolServerSideTranslatorPB.java:348)  
  16.         at org.apache.hadoop.hdfs.protocol.proto.ClientNamenodeProtocolProtos$ClientNamenodeProtocol$2.callBlockingMethod  
  17.   
  18. (ClientNamenodeProtocolProtos.java:59576)  
  19.         at org.apache.hadoop.ipc.ProtobufRpcEngine$Server$ProtoBufRpcInvoker.call(ProtobufRpcEngine.java:585)  
  20.         at org.apache.hadoop.ipc.RPC$Server.call(RPC.java:928)  
  21.         at org.apache.hadoop.ipc.Server$Handler$1.run(Server.java:2048)  
  22.         at org.apache.hadoop.ipc.Server$Handler$1.run(Server.java:2044)  
  23.         at java.security.AccessController.doPrivileged(Native Method)  
  24.         at javax.security.auth.Subject.doAs(Subject.java:415)  
  25.         at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1491)  
  26.         at org.apache.hadoop.ipc.Server$Handler.run(Server.java:2042)  
       执行程序的时候,报上述错误,是因为一开始我的连接内容是如下方式,没有添加用户, 此处的用户不应该是hive的用户,而应该是Hadoop的用户:

[html]  view plain  copy
  1. conn=DriverManager.getConnection("jdbc:hive2://192.168.2.133:10000/hive", "", "");  

    
      解决办法: 
  

[html]  view plain  copy
  1. conn=DriverManager.getConnection("jdbc:hive2://192.168.2.133:10000/hive", "hadoopUser", "");  
       hadoopUser 是我Hadoop的用户,添加后使用正常。
       

       更多内容,请参考官网网址学习:HiveServer2 Clients

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值