java连接hive和连接mysql都是一样的
1.首先 hadoop 配置不验证权限,要不然会报权限之类的错误
<property> <name>hadoop.proxyuser.root.hosts</name> <value>*</value> </property> <property> <name>hadoop.proxyuser.root.groups</name> <value>*</value> </property>
2.启动hive
nohup hive --service metastore & nohup hive --service hiveserver2 &
3.java连接操作hive案例代码:
package net.itxw.example; import java.sql.*; /** * @Author: itxw * @Date: 2019/1/9 */ public class HiveTest { private static String driverName = "org.apache.hive.jdbc.HiveDriver"; public boolean run() { try { Class.forName(driverName); Connection con = null; con = DriverManager.getConnection("jdbc:hive2://10.10.22.133:10000/datacenter", "root", "123456"); Statement stmt = con.createStatement(); ResultSet res = null; String sql = "select * from test table"; System.out.println("Running: " + sql); res = stmt.executeQuery(sql); System.out.println("ok"); while (res.next()) { System.out.println("###"); System.out.println(res.getString(1)); } res.close(); stmt.close(); con.close(); return true; } catch (Exception e) { e.printStackTrace(); System.out.println("error"); return false; } } public static void main(String[] args) throws SQLException { HiveTest hiveJdbcClient = new HiveTest(); hiveJdbcClient.run(); } };