Impala 4、Impala JDBC
配置:
– impala.driver=org.apache.hive.jdbc.HiveDriver
– impala.url=jdbc:hive2://node2:21050/;auth=noSasl
– impala.username=
– impala.password=
• 尽量使用PreparedStatement执行SQL语句:
– 1.性能上PreparedStatement要好于Statement
– 2.Statement存在查询不出数据的情况
下面是Java的测试代码:
首先是引入Impala所需要的类库
然后是测试代码:
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
|
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.junit.Test;
public class demo
{
public static Connection
getConnection() throws ClassNotFoundException,
SQLException{
String
driver = "org.apache.hive.jdbc.HiveDriver" ;
String
url = "jdbc:hive2://node23:21050/;auth=noSasl" ;
String
username = "" ;
String
password = "" ;
Connection
conn = null ;
Class.forName(driver);
conn
= (Connection) DriverManager.getConnection(url,username,password);
return conn;
}
@Test
public void select() throws ClassNotFoundException,
SQLException{
Connection
conn = getConnection();
String
sql = "select
* from t_stu;" ;
PreparedStatement
ps = conn.prepareStatement(sql);
ResultSet
rs = ps.executeQuery();
int col
= rs.getMetaData().getColumnCount();
System.out.println( "=====================================" );
while (rs.next()){
for ( int i= 1 ;i<=col;i++){
System.out.print(rs.getString(i)+ "\t" );
}
System.out.print( "\n" );
}
System.out.println( "=====================================" );
}
}
|
最后是测试结果: