我需要編寫一個API來從Hive表中獲取數據,以便我可以處理它,所以我試圖使用Jdbc訪問Hive表。 一個簡單的 「select * from表」 工作正常,但「SELECT COUNT(*)從表中拋出一個錯誤:嘗試使用Jdbc訪問Hive表時出錯
2017-04-20 18:05:57,709 INFO [main] jdbc.Utils (Utils.java:parseURL(310)) - Supplied authorities: 10.94.154.125:10000
2017-04-20 18:05:57,712 INFO [main] jdbc.Utils (Utils.java:parseURL(397)) - Resolved authority: 10.94.154.125:10000
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.
2017-04-20 18:05:57,790 INFO [main] jdbc.HiveConnection (HiveConnection.java:openTransport(203)) - Will try to open client transport with JDBC Uri: jdbc:hive2://10.94.154.125:10000/CloudData
Running: select count(*) from ipticket
java.sql.SQLException: Error while processing statement: FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.mr.MapRedTask
at org.apache.hive.jdbc.HiveStatement.execute(HiveStatement.java:296)
at org.apache.hive.jdbc.HiveStatement.executeQuery(HiveStatement.java:392)
at HiveJdbcClient.main(HiveJdbcClient.java:35)
代碼:
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.hive.jdbc.HiveDriver";
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:hive2://localhost:10000/CloudData", "", "");
Statement stmt = con.createStatement();
String tableName = "table1";
String sql = "select count(*) from " + tableName;
System.out.println("Running: " + sql);
try{
ResultSet res = stmt.executeQuery(sql);
while (res.next()) {
System.out.println(res.getString(1) + "\t" + res.getString(2)+"\t"+res.getString(3) + "\t" + res.getString(4));
}
}catch (SQLException f){
f.printStackTrace();
}
}
}
側前端:[HivePreparedStatement](https://hive.apache.org/javadocs/r0。 10.0/api/org/apache/hadoop/hive/jdbc/HivePreparedStatement.html) – philantrovert