从无到有系列之Hive-jdbc测试操作hive03

来源:互联网 发布:微信订餐软件 编辑:程序博客网 时间:2024/05/21 16:48

本文转载至:http://blog.csdn.net/wind520/article/details/44084953(有个别自己的修改)

注:这里的hive版本是1.2.0,所以用的hiveserver2,区别于hiveserver1,不要混淆使用

1:运行

命令行模式:

hive --service hiveserver2 --hiveconf hive.server2.thrift.port=10001

服务模式:

hiveserver2 start
  1. [jifeng@feng01 conf]$ hive --service hiveserver2 --hiveconf hive.server2.thrift.port=10001  
  2. Starting HiveServer2  
  3. 15/03/05 16:59:33 WARN conf.HiveConf: DEPRECATED: hive.metastore.ds.retry.* no longer has any effect.  Use hive.hmshandler.retry.* instead  


2:权限问题

java jdbc连接报错:
  1. Exception in thread "main" java.sql.SQLException: Error while compiling statement: FAILED: RuntimeException Cannot make directory: hdfs://feng01:9000/tmp/hive-jifeng/hive_2015-03-05_17-04-43_349_5945847416346775092-3  
  2.     at org.apache.hive.jdbc.Utils.verifySuccess(Utils.java:121)  
  3.     at org.apache.hive.jdbc.Utils.verifySuccessWithInfo(Utils.java:109)  
  4.     at org.apache.hive.jdbc.HiveStatement.execute(HiveStatement.java:231)  
  5.     at org.apache.hive.jdbc.HiveStatement.executeQuery(HiveStatement.java:355)  
  6.     at demo.test.Pretest.main(Pretest.java:28)  

服务端也看到错误
  1. FAILED: RuntimeException Cannot make directory: hdfs://feng01:9000/tmp/hive-jifeng/hive_2015-03-05_17-04-43_349_5945847416346775092-3  
无权限创建目录,修改为管理hadoop的用户和密码

修改后

3:For input string: "5000L"

JDBC连接报:

  1. Exception in thread "main" java.sql.SQLException: For input string: "5000L"  
  2.     at org.apache.hive.jdbc.Utils.verifySuccess(Utils.java:121)  
  3.     at org.apache.hive.jdbc.Utils.verifySuccessWithInfo(Utils.java:109)  
  4.     at org.apache.hive.jdbc.HiveStatement.execute(HiveStatement.java:263)  
  5.     at org.apache.hive.jdbc.HiveStatement.executeQuery(HiveStatement.java:355)  
  6.     at demo.test.Pretest.main(Pretest.java:28)  
修改配置:

把hive-site.xml

这个配置文件里hive.server2.long.polling.timeout这个参数是5000L,改成5000

  1. [jifeng@feng01 conf]$ vi hive-site.xml   
  2.   
  3.   <name>hive.server2.long.polling.timeout</name>  
  4.   <value>5000</value>  
  5.   <description>Time in milliseconds that HiveServer2 will wait, before responding to asynchronous calls that  
  6.  use long polling</description>  
  7. </property>  
4:JDBC 连接HIVE OK(注:这里需要引入hadoop的相关jar包和apache component的httpclient的jar包以及hive service的相关包)
  1. package demo.test;  
  2.   
  3. import java.sql.*;  
  4.   
  5. public class Pretest {  
  6.   
  7.   
  8.             public static void main( String args[] )  
  9.                 throws SQLException , ClassNotFoundException {  
  10.                 String jdbcdriver="org.apache.hive.jdbc.HiveDriver";  
  11.   
  12.                 String jdbcurl="jdbc:hive2://localhost:10001";  
  13.   
  14.                 String username="hive";  
  15.                 String password="hive";         
  16.                 Class.forName(jdbcdriver);  
  17.                 Connection c = DriverManager.getConnection(jdbcurl,username,password);   
  18.                 Statement st = c.createStatement();  
  19.                // select * from firewall where idauto=16600918"));//  
  20.                 print( "num should be 1 " , st.executeQuery("select * from test"));  
  21.                 //( "select id,name,vip from users order by id limit 5" ) );  
  22.                 // TODO indexing  
  23.             }  
  24.              static void print( String name , ResultSet res )  
  25.                         throws SQLException {  
  26.                         System.out.println( name);  
  27.                         ResultSetMetaData meta=res.getMetaData();                         
  28.                         //System.out.println( "\t"+res.getRow()+"条记录");  
  29.                         String  str="";  
  30.                         for(int i=1;i<=meta.getColumnCount();i++){  
  31.                             str+=meta.getColumnName(i)+"   ";  
  32.                             //System.out.println( meta.getColumnName(i)+"   ");  
  33.                         }  
  34.                         System.out.println("\t"+str);  
  35.                         str="";  
  36.                         while ( res.next() ){  
  37.                             for(int i=1;i<=meta.getColumnCount();i++){     
  38.                                 str+= res.getString(i)+"   ";                           }   
  39.                             System.out.println("\t"+str);  
  40.                             str="";  
  41.                         }  
  42.                     }         
  43. }  

运行结果:   num should be 1
                                         test.name   
                                         wangjian

       



0 0
原创粉丝点击