hiveserver2 启动后jdbc 连接上的原因

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
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值