2017-04-20 124 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”)解决了问题。 –