hiveserver2 启动后jdbc 连接上的原因

来源:互联网 发布:买网络电话卡被骗 编辑:程序博客网 时间:2024/05/16 14:47

1 首先修改 hive-site.xml

<property>  <name>hive.server2.thrift.port</name>  <value>10000</value></property><property>  <name>hive.server2.thrift.bind.host</name>  <value>localhost</value> <!-- 默认是localhost,但我手动改成了本机的ip地址,很可能就是我改了这个才起作用的 --></property>


2 启动hiveserver2

$HIVE_HOME/bin/hive --service hiveserver2


3 测试连接是否以连上

不用写jdbc程序,运行 bin/beeline.sh

然后输入  !connect jdbc:hive2://上面设置的ip地址:10000 hiveuser hiveuser  后面两个是你创建的用户名和密码

如果能连接上就表示 jdbc没有问题了

注: !connect jdbc:hive2://localhost:10000 hiveuser hiveuser  这里不要使用localhost,应该使用配置的ip


4 通过程序连接jdbc

也可以通过自己写程序连接jdbc

package test;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;// import org.apache.hive.jdbc.HiveDriver;public class HiveJdbcClient {private static String driverName = "org.apache.hive.jdbc.HiveDriver";public boolean run() {try {Class.forName(driverName);Connection con = null;con = DriverManager.getConnection("jdbc:hive2://192.168.17.15:10000/hivedb", "hiveuser", "hiveuser");Statement stmt = con.createStatement();ResultSet res = null;String sql = "select count(*) from test_data";System.out.println("Running: " + sql);res = stmt.executeQuery(sql);System.out.println("ok");while (res.next()) {System.out.println(res.getString(1));}return true;} catch (Exception e) {e.printStackTrace();System.out.println("error");return false;}}public static void main(String[] args) throws SQLException {HiveJdbcClient hiveJdbcClient = new HiveJdbcClient();hiveJdbcClient.run();}}


参考的url为:http://www.tuicool.com/articles/Bbqaea




1 0
原创粉丝点击