通過JDBC/ODBC->Thirft Server->Spark SQL->Hive取代傳統(tǒng)數(shù)據(jù)庫為后臺的系統(tǒng) 啟動hive: hive --service metastore &
hive
服務(wù)端:啟動thrift服務(wù)端 ./start-thriftserver.sh --master spark://Master:7077 --hiveconf hive.server2.transport.mode=http --hiveconf hive.server2.thrift.http.path=cliservice
Java代碼 /**
* Java通過JDBC訪問Thrift Server,進(jìn)而訪問Hive,這是企業(yè)級開發(fā)中最為常見的方式
*/
public class SparkSQLJDBC2ThriftServer {
/**
* @param args
*/
public static void main(String[] args) {
Connection conn = null;
ResultSet rs = null;
String sql = "select * from people where age = ?";
try {
Class.forName("org.apache.hive.jdbc.HiveDriver");
conn = DriverManager.getConnection("jdbc:hive2://Master:10001/default?" //10001為thrift默認(rèn)端口,default為hive'中的庫
+ "hive.server2.transport.mode=http;hive.server2.thrift.http.path=cliservice",
"root","");
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setInt(1, 30);
rs = stmt.executeQuery();
while(rs.next()){
System.out.println(rs.getString(1)); //數(shù)據(jù)應(yīng)保存成parquet
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
rs.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
|