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所需要的类库

  

  然后是测试代码:

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("=====================================");
	}
}

  最后是测试结果:

  

posted @ 2016-03-23 22:32  Bodi  阅读(4627)  评论(0编辑  收藏  举报