服务器远程连接hive
时间: 2023-11-29 12:45:34 浏览: 44
以下是连接远程Hive服务器的步骤:
1.进入Hive目录,运行beeline命令:
```shell
cd /hive
bin/beeline
```
2.使用以下命令连接远程Hive服务器:
```shell
!connect jdbc:hive2://远程服务器ip:10000/default
```
3.如果需要在远程服务器上启动Hive服务接口,可以使用以下命令:
```shell
nohup hive --service hiveserver 10000 &
```
4.如果需要在本地编写Java程序连接远程Hive服务器,可以使用以下示例程序:
```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Test {
private static String driverName = "org.apache.hive.jdbc.HiveDriver";
private static String url = "jdbc:hive2://远程服务器ip:10000/default";
private static String user = "hive";
private static String password = "";
public static void main(String[] args) throws SQLException {
try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.exit(1);
}
Connection con = DriverManager.getConnection(url, user, password);
Statement stmt = con.createStatement();
String tableName = "test";
stmt.execute("drop table if exists " + tableName);
stmt.execute("create table " + tableName + " (key int, value string)");
System.out.println("Create table success!");
String sql = "show tables '" + tableName + "'";
System.out.println("Running: " + sql);
ResultSet res = stmt.executeQuery(sql);
if (res.next()) {
System.out.println(res.getString(1));
}
sql = "describe " + tableName;
res = stmt.executeQuery(sql);
while (res.next()) {
System.out.println(res.getString(1) + "\t" + res.getString(2));
}
sql = "select * from " + tableName;
res = stmt.executeQuery(sql);
while (res.next()) {
System.out.println(String.valueOf(res.getInt(1)) + "\t" + res.getString(2));
}
stmt.close();
con.close();
}
}
```