【发布时间】:2014-04-20 08:03:40
【问题描述】:
我正在尝试建立到 Hive 的 JDBC 连接,以便我可以查看和创建表并从 Eclipse 查询 Hive 表。我使用 HiveClient 示例代码:https://cwiki.apache.org/confluence/display/Hive/HiveClient
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;
public class HiveJdbcClient {
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) {
// TODO Auto-generated catch block
e.printStackTrace();
System.exit(1);
}
Connection con = DriverManager.getConnection("jdbc:hive://localhost:10000/default", "", "");
Statement stmt = con.createStatement();
String tableName = "testHiveDriverTable";
stmt.executeQuery("drop table " + tableName);
ResultSet res = stmt.executeQuery("create table " + tableName + " (key int, value string)");
// show tables
String sql = "show tables '" + tableName + "'";
System.out.println("Running: " + sql);
res = stmt.executeQuery(sql);
if (res.next()) {
System.out.println(res.getString(1));
}
// describe table
sql = "describe " + tableName;
System.out.println("Running: " + sql);
res = stmt.executeQuery(sql);
while (res.next()) {
System.out.println(res.getString(1) + "\t" + res.getString(2));
}
// load data into table
// NOTE: filepath has to be local to the hive server
// NOTE: /tmp/a.txt is a ctrl-A separated file with two fields per line
String filepath = "/tmp/a.txt";
sql = "load data local inpath '" + filepath + "' into table " + tableName;
System.out.println("Running: " + sql);
res = stmt.executeQuery(sql);
// select * query
sql = "select * from " + tableName;
System.out.println("Running: " + sql);
res = stmt.executeQuery(sql);
while (res.next()) {
System.out.println(String.valueOf(res.getInt(1)) + "\t" + res.getString(2));
}
// regular hive query
sql = "select count(1) from " + tableName;
System.out.println("Running: " + sql);
res = stmt.executeQuery(sql);
while (res.next()) {
System.out.println(res.getString(1));
}
}
}
然后我将所有必需的 jar 添加到 eclipse 中的 java 构建路径中。我正在使用 Cloudera QuickstartVM 4.6.1 和它附带的 eclipse。这是我尝试运行代码时在 IDE 中遇到的错误。
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" java.sql.SQLException: Could not establish connection to localhost:10000/default: java.net.ConnectException: Connection refused
at org.apache.hadoop.hive.jdbc.HiveConnection.<init>(HiveConnection.java:116)
at org.apache.hadoop.hive.jdbc.HiveDriver.connect(HiveDriver.java:104)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at HiveJdbcClient.main(HiveJdbcClient.java:22)
有人知道我在这里缺少什么吗?
【问题讨论】:
-
您需要启动 Hive Thrift 服务器。能检查一下是否启动了吗?
-
如何查看是否启动?
-
如果你没有启动它,那么它就没有启动。检查文档:my.safaribooksonline.com/book/databases/hadoop/9781449326944/…
-
谢谢。我启动了 thrift 服务器,端口 10000 现在正在监听。现在我在 Eclipse 中有另一条错误消息:线程“main”中的异常 java.sql.SQLException: org.apache.thrift.transport.TTransportException: java.net.SocketException: Connection reset
标签: java eclipse hadoop jdbc hive