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("====================================="); } }
最后是测试结果:
更多参考
作者:大数据和人工智能躺过的坑
出处:http://www.cnblogs.com/zlslch/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。
如果您认为这篇文章还不错或者有所收获,您可以通过右边的“打赏”功能 打赏我一杯咖啡【物质支持】,也可以点击右下角的【好文要顶】按钮【精神支持】,因为这两种支持都是我继续写作,分享的最大动力!