虽然网上介绍idea远程连接hive的方法挺多的,但是我还是整了一段时间才连接成功,在这边记录一下,方便他人,方便自己。
首先看一下我通过xshell5远程连接hive的配置
这是登陆时候的界面,可以看到我选择的是10.100.34.110这个服务器
第一种,使用SSH,在idea中Tools--StartSSH session--edit
host --172.18.254.49 Port --22 user name --zhangpanpan180108 password --******
save password 就会进入选择select server这个界面了,OK
第二种,使用idea database tool 1.database这个选项在view--toolWindows --database
在右侧面可以看到database了
2.idea没有内置hive的驱动,需要自己新建一个driver, 这边可以详细看http://blog.youkuaiyun.com/u010814849/article/details/77649724
这里详细说一下url,url格式为:jdbc:hive2://ip地址:port端口号/database数据库
我这里的url为jdbc:hive2://10.100.12.30:10000/dm_research
user和password为yjy_research和**** 这里我整了好久,一直不明白为什么要输入这个密码,而不是上面的user name。。。。
再详细说一下sql进行测试,我之前用xshell一直写;,但是这样写是错误的,正确的是
show tables
坑死我了,一直报错,幸好找到正确的方法了
3.还有使用java代码通过JDBC连接hive,下面是我的代码
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class hive { private static String driverName = "org.apache.hive.jdbc.HiveDriver"; private static String url = "jdbc:hive2://localhost:10000/database"; private static String user = ""; private static String password = ""; private static String sql = ""; private static ResultSet res; public static void main(String[] args) { Connection conn = null; Statement stmt = null; try { conn = getConn(); stmt = conn.createStatement(); stmt.execute("use dm_research"); selectData(stmt, "dm_research.collection");//表名 } catch (ClassNotFoundException e) { e.printStackTrace(); System.exit(1); } catch (SQLException e) { e.printStackTrace(); System.exit(1); } finally { try { if (conn != null) { conn.close(); conn = null; } if (stmt != null) { stmt.close(); stmt = null; } } catch (SQLException e) { e.printStackTrace(); } } } private static void countData(Statement stmt, String tableName) throws SQLException { sql = "select count(1) from " + tableName + " limit 10"; System.out.println("Running:" + sql); res = stmt.executeQuery(sql); System.out.println("执行“regular hive query”运行结果:"); while (res.next()) { System.out.println("count ------>" + res.getString(1)); } } private static void selectData(Statement stmt, String tableName) throws SQLException { sql = "select * from " + tableName + " limit 10"; System.out.println("Running:" + sql); res = stmt.executeQuery(sql); System.out.println("执行 select * query 运行结果:"); while (res.next()) { System.out.println(res.getString(1) + "\t" + res.getString(2)); } }
}