2017-04-20 126 views
0

我需要編寫一個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(); 
    } 

    } 
} 
+0

側前端:[HivePreparedStatement](https://hive.apache.org/javadocs/r0。 10.0/api/org/apache/hadoop/hive/jdbc/HivePreparedStatement.html) – philantrovert

回答

0

因爲SELECT COUNT()推出MapReduce的作業時,也許你有你的配置文件的問題 嘗試測試地圖降低羣集上的工作 執行此 和改變。Hadoop的MapReduce的examples- 。與y的罐子版本的.jar我們的MapReduce exemples(如果你的Hadoop 2.6它看起來像/hadoop-mapreduce-examples-2.6.0.jar)

CD $ HADOOP_HOME 然後 Hadoop的罐子./share/hadoop/mapreduce/hadoop- mapreduce-examples- *罐PI 2 5

如果有錯誤則MR沒有被配置爲正常工作

+0

不,查詢在hive cli中正常工作,我能夠看到結果。因此,我確認問題不在MR的配置文件中 –

+0

使用Connection con = DriverManager.getConnection(「jdbc:hive2:// localhost:10000/CloudData」,「usename」,「password」)解決了問題。 –

相關問題