spark SQL Running the Thrift JDBC/ODBC server

本文档详细介绍了如何启动Spark SQL的Thrift JDBC/ODBC服务器,包括指定端口、服务器地址和Spark Master,以及使用beeline进行测试。通过示例展示了beeline连接服务器查询数据的过程,并提供了Java JDBC连接的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Running the Thrift JDBC/ODBC server

1:运行

./sbin/start-thriftserver.sh  --hiveconf hive.server2.thrift.port=10000  --hiveconf hive.server2.thrift.bind.host=feng02 --master spark://feng02:7077 --driver-class-path /home/jifeng/hadoop/spark-1.2.0-bin-2.4.1/lib/mysql-connector-java-5.1.32-bin.jar --executor-memory 1g

端口:10000

服务器:feng02

spark master:spark://feng02:7077

driver-class-path:mysql驱动包(hive配置的)


[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. [jifeng@feng02 spark-1.2.0-bin-2.4.1]$ ./sbin/start-thriftserver.sh  --hiveconf hive.server2.thrift.port=10000  --hiveconf hive.server2.thrift.bind.host=feng02 --master spark://feng02:7077 --driver-class-path /home/jifeng/hadoop/spark-1.2.0-bin-2.4.1/lib/mysql-connector-java-5.1.32-bin.jar  
  2. starting org.apache.spark.sql.hive.thriftserver.HiveThriftServer2, logging to /home/jifeng/hadoop/spark-1.2.0-bin-2.4.1/sbin/../logs/spark-jifeng-org.apache.spark.sql.hive.thriftserver.HiveThriftServer2-1-feng02.out  

2:运行beeline

Now you can use beeline to test the Thrift JDBC/ODBC server:

./bin/beeline
[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. [jifeng@feng02 spark-1.2.0-bin-2.4.1]$ ./bin/beeline  
  2. Spark assembly has been built with Hive, including Datanucleus jars on classpath  
  3. Beeline version ??? by Apache Hive  

3:连接server


参考:https://cwiki.apache.org/confluence/display/Hive/HiveServer2+Clients#HiveServer2Clients-BeelineExample

[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. beeline> !connect jdbc:hive2://feng02:10000 jifeng jifeng org.apache.hive.jdbc.HiveDriver  
  2. Connecting to jdbc:hive2://feng02:10000  
  3. log4j:WARN No appenders could be found for logger (org.apache.thrift.transport.TSaslTransport).  
  4. log4j:WARN Please initialize the log4j system properly.  
  5. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.  
  6. Connected to: Spark SQL (version 1.2.0)  
  7. Driver: null (version null)  
  8. Transaction isolation: TRANSACTION_REPEATABLE_READ  

4:查询

[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. 0: jdbc:hive2://feng02:10000> show tables;                                                                    
  2. +----------------+  
  3. |     result     |  
  4. +----------------+  
  5. | course         |  
  6. | hbase_table_1  |  
  7. | pokes          |  
  8. | student        |  
  9. +----------------+  
  10. 4 rows selected (2.723 seconds)  

[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. 0: jdbc:hive2://feng02:10000> select * from student;  
  2. +-----+----------+------+  
  3. | id  |   name   | age  |  
  4. +-----+----------+------+  
  5. | 1   | nick     | 24   |  
  6. | 2   | doping   | 25   |  
  7. | 3   | caizhi   | 26   |  
  8. | 4   | liaozhi  | 27   |  
  9. | 5   | wind     | 30   |  
  10. +-----+----------+------+  
  11. 5 rows selected (10.554 seconds)  
  12. 0: jdbc:hive2://feng02:10000> select a.*,b.* from student a  join course b where a.id=b.id ;  
  13. +-----+----------+------+-----+-----+-----+-----+-----+  
  14. | id  |   name   | age  | id  | c1  | c2  | c3  | c4  |  
  15. +-----+----------+------+-----+-----+-----+-----+-----+  
  16. | 1   | nick     | 24   | 1   | 英语  | 中文  | 法文  | 日文  |  
  17. | 2   | doping   | 25   | 2   | 中文  | 法文  |     |     |  
  18. | 3   | caizhi   | 26   | 3   | 中文  | 法文  | 日文  |     |  
  19. | 4   | liaozhi  | 27   | 4   | 中文  | 法文  | 拉丁  |     |  
  20. | 5   | wind     | 30   | 5   | 中文  | 法文  | 德文  |     |  
  21. +-----+----------+------+-----+-----+-----+-----+-----+  
  22. 5 rows selected (2.33 seconds)  

4:Java JDBC连接

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  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.                 String jdbcurl="jdbc:hive2://feng02:10000";  
  12.                 String username="scott";  
  13.                 String password="tiger";          
  14.                 Class.forName(jdbcdriver);  
  15.                 Connection c = DriverManager.getConnection(jdbcurl,username,password);   
  16.                 Statement st = c.createStatement();               
  17.                 print( "num should be 1 " , st.executeQuery("select * from student"));  
  18.   
  19.                 // TODO indexing  
  20.             }  
  21.              static void print( String name , ResultSet res )  
  22.                         throws SQLException {  
  23.                         System.out.println( name);  
  24.                         ResultSetMetaData meta=res.getMetaData();                         
  25.                         //System.out.println( "\t"+res.getRow()+"条记录");  
  26.                         String  str="";  
  27.                         for(int i=1;i<=meta.getColumnCount();i++){  
  28.                             str+=meta.getColumnName(i)+"   ";  
  29.                             //System.out.println( meta.getColumnName(i)+"   ");  
  30.                         }  
  31.                         System.out.println("\t"+str);  
  32.                         str="";  
  33.                         while ( res.next() ){  
  34.                             for(int i=1;i<=meta.getColumnCount();i++){     
  35.                                 str+= res.getString(i)+"   ";                           }   
  36.                             System.out.println("\t"+str);  
  37.                             str="";  
  38.                         }  
  39.                     }         
  40. }  


上面是运行参数

结果显示:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. num should be 1   
  2.     id   name   age     
  3.     1   nick   24     
  4.     2   doping   25     
  5.     3   caizhi   26     
  6.     4   liaozhi   27     
  7.     5   wind   30     
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值