1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > java 连接hive2 java jdbc连接hiveserver2

java 连接hive2 java jdbc连接hiveserver2

时间:2018-10-23 16:16:34

相关推荐

java 连接hive2 java jdbc连接hiveserver2

1 首先修改 hive-site.xml配置端口(默认就是10000)

hive.server2.thrift.port

10000

hive.server2.thrift.bind.host

localhost

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();

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。