-
Notifications
You must be signed in to change notification settings - Fork 1
/
SparkSQLJDBC2ThriftServer.java
40 lines (30 loc) · 1.17 KB
/
SparkSQLJDBC2ThriftServer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.dt.spark.SparkApps.sql;
import java.util.ArrayList;
import java.sql.Connection;
import java.sql.DriverManager;
/**
* @author DT大数据梦工厂
* 新浪微博:http://weibo.com/ilovepains/
* 实战演示Java通过JDBC访问Thrift Server,进而访问Spark SQL,进而访问Hive,这是企业级开发中最为常见的方式;
*/
public class SparkSQLJDBC2ThriftServer {
public static void main(String[] args) {
String sql = "select name from people where age = ?";
Connection conn = null;
ResultSet resultSet = null;
try{
Class.forName("org.apache.hive.jdbc.HiveDriver");
conn = DriverManager.getConnection("jdbc:hive2://<host>:10001/<database>?hive.server2.transport.mode=http;hive.server2.thrift.http.path=cliservice", "root", "");
PreparedStatement preparedStatement = conn.prepareStatement(sql);
preparedStatement.setInt(1, 30);
resultSet = preparedStatement.executeQuery();
while(resultSet.next()){
System.out.println(resultSet.getString(1)); //此处的数据应该保存在Parquet中
}
}catch(Exception e){
e.printStackTrace();
}finally{
conn.close();
}
}
}