spark SQL Running the Thrift JDBC/ODBC server
本帖最后由 breaking 于 2016-3-31 13:48 编辑问题导读:
1.Saprk怎么Running在Thrift JDBC Server上?
2.java JDBC怎么操作?
static/image/hrline/2.gif
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
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:运行beelineNow you can use beeline to test the Thrift JDBC/ODBC server:
./bin/beeline
$ ./bin/beeline
Spark assembly has been built with Hive, including Datanucleus jars on classpath
Beeline version ??? by Apache Hive
3:连接server
参考:https://cwiki.apache.org/confluence/display/Hive/HiveServer2+Clients#HiveServer2Clients-BeelineExample
beeline> !connect jdbc:hive2://feng02:10000 jifeng jifeng org.apache.hive.jdbc.HiveDriver
Connecting to jdbc:hive2://feng02:10000
log4j:WARN No appenders could be found for logger (org.apache.thrift.transport.TSaslTransport).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Connected to: Spark SQL (version 1.2.0)
Driver: null (version null)
Transaction isolation: TRANSACTION_REPEATABLE_READ
4:查询
0: jdbc:hive2://feng02:10000> show tables;
+----------------+
| result |
+----------------+
| course |
| hbase_table_1|
| pokes |
| student |
+----------------+
4 rows selected (2.723 seconds)
0: jdbc:hive2://feng02:10000> select * from student;
+-----+----------+------+
| id| name | age|
+-----+----------+------+
| 1 | nick | 24 |
| 2 | doping | 25 |
| 3 | caizhi | 26 |
| 4 | liaozhi| 27 |
| 5 | wind | 30 |
+-----+----------+------+
5 rows selected (10.554 seconds)
0: jdbc:hive2://feng02:10000> select a.*,b.* from student ajoin course b where a.id=b.id ;
+-----+----------+------+-----+-----+-----+-----+-----+
| id| name | age| id| c1| c2| c3| c4|
+-----+----------+------+-----+-----+-----+-----+-----+
| 1 | nick | 24 | 1 | 英语| 中文| 法文| 日文|
| 2 | doping | 25 | 2 | 中文| 法文| | |
| 3 | caizhi | 26 | 3 | 中文| 法文| 日文| |
| 4 | liaozhi| 27 | 4 | 中文| 法文| 拉丁| |
| 5 | wind | 30 | 5 | 中文| 法文| 德文| |
+-----+----------+------+-----+-----+-----+-----+-----+
5 rows selected (2.33 seconds)
5:Java JDBC连接
package demo.test;
import java.sql.*;
public class Pretest {
public static void main( String args[] )
throws SQLException , ClassNotFoundException {
String jdbcdriver="org.apache.hive.jdbc.HiveDriver";
String jdbcurl="jdbc:hive2://feng02:10000";
String username="scott";
String password="tiger";
Class.forName(jdbcdriver);
Connection c = DriverManager.getConnection(jdbcurl,username,password);
Statement st = c.createStatement();
print( "num should be 1 " , st.executeQuery("select * from student"));
// TODO indexing
}
static void print( String name , ResultSet res )
throws SQLException {
System.out.println( name);
ResultSetMetaData meta=res.getMetaData();
//System.out.println( "\t"+res.getRow()+"条记录");
String str="";
for(int i=1;i<=meta.getColumnCount();i++){
str+=meta.getColumnName(i)+" ";
//System.out.println( meta.getColumnName(i)+" ");
}
System.out.println("\t"+str);
str="";
while ( res.next() ){
for(int i=1;i<=meta.getColumnCount();i++){
str+= res.getString(i)+" "; }
System.out.println("\t"+str);
str="";
}
}
}
上面是运行参数
结果显示:
num should be 1
id name age
1 nick 24
2 doping 25
3 caizhi 26
4 liaozhi 27
5 wind 30
原文链接:http://blog.csdn.net/wind520/article/details/44061563
实在是看不懂 我是菜鸟..我看了一下,感觉这就是在用hive jdbc连接。是不是没有使用spark sql.. very good!感谢分享! 在sparksql中连接MySQL数据库 能否直接执行sql语句操作MySQL woshichuanqi 发表于 2016-4-7 21:58
在sparksql中连接MySQL数据库 能否直接执行sql语句操作MySQL
这个是可以的,sparksql可以操作一般的主流关系型数据库。
页:
[1]