第四篇 JDBC连接Hive查询
jdbc 连接hive 查询,要做以下几个事情,前面也曾提到:
1 在hadoop的core-site.xml 中增加配置
<property> <name>hadoop.proxyuser.root.groups</name> <value>*</value> </property> <property> <name>hadoop.proxyuser.root.hosts</name> <value>*</value> </property>
2 让hive支持自定义验证
http://www.jb51.cc/article/p-evbqggss-bhy.html
3 编写测试程序
package hive.server2.query; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; public class ApiQueryTest { private static String driverName = "org.apache.hive.jdbc.HiveDriver"; private static final Logger logger = LogManager.getLogger("ApiQueryTest"); public static void main(String[] args) { try { Class.forName(driverName); Connection con = null; con = DriverManager.getConnection("jdbc:hive2://10.68.128.215:10000","root","kangyun9413"); Statement stmt = con.createStatement(); ResultSet res = null; String sql = "select sum(num) total,url,status from apis.api_logs group by status,url order by total desc limit 10"; System.out.println("Running: " + sql); res = stmt.executeQuery(sql); System.out.println("ok"); while (res.next()) { System.out.println(res.getString(1) + "\t" + res.getString(2) + "\t" + res.getString(3)); } } catch (Exception e) { e.printStackTrace(); System.out.println("error"); } } }
运行: