• 配置:
– 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所需要的類庫
然后是測試代碼:
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("====================================="); } }
最后是測試結果: