我正在尝试通过jdbc连接到hive。以下是我正在使用的代码:
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Connection;
public class HiveToJava {
private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";
/**
* @param args
* @throws SQLException
*/
public static void main(String[] args) throws SQLException {
try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.exit(1);
}
Connection con = DriverManager.getConnection(
"jdbc:hive://localhost:10000/default", "", "");
Statement stmt = con.createStatement();
String tableName = "processed";
ResultSet res = stmt.executeQuery("describe " + tableName);
while (res.next()) {
System.out.println(res.getString(1) + "\t" + res.getString(2));
}
}
}
当我将这个类作为Java应用程序运行时,我得到了这个消息:
线程“main”中的异常java.sql.SQLException:无法建立 连接到localhost:10000 / default:java.net.ConnectException: 连接被拒绝
请帮忙。
答案 0 :(得分:-1)
尝试以下代码,您可以获得输出....
package com.services.connections;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class ConnectHive
{
public static void main(String[] args) throws SQLException,ClassNotFoundException
{
// TODO Auto-generated method stub
String connectionURL = "jdbc:hive://localhost:9999/javatesting";
String drivername = "org.apache.hadoop.hive.jdbc.HiveDriver";
String username = "";
String password = "";
try {
Class.forName(drivername);
}
catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
System.exit(1);
}
try {
Connection con = DriverManager.getConnection(connectionURL, username, password);
if(con != null)
{
System.out.println("Connected");
}
else
{
System.out.println("Not Connected");
}
Statement stmt = con.createStatement();
// select * query
String sql;
ResultSet res;
sql = "select * from javatesting.testdata";
System.out.println("Running: " + sql);
res = stmt.executeQuery(sql);
while (res.next())
{
System.out.println(String.valueOf(res.getString(1)) + "\t" + res.getString(2));
}
}
catch(SQLException se)
{
se.printStackTrace();
}
}
}