尝试使用Jdbc访问Hive表时出错

时间:2017-04-20 12:54:37

标签: hadoop jdbc hive

我需要编写一个API来从Hive表中获取数据,以便我可以处理它,我试图使用Jdbc访问Hive表。 一个简单的“Select * from table”工作正常,但“从表中选择count(*)会引发错误:

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();
    }

  }
}

1 个答案:

答案 0 :(得分:0)

由于Select count()启动了MapReduce作业,因此您的配置文件可能存在问题 尝试测试群集上的地图减少作业 执行此操作 并改变hadoop-mapreduce-examples - .jar与你的mapreduce例子的jar版本(如果你有hadoop 2.6,它将看起来像/hadoop-mapreduce-examples-2.6。 0.jar)

  

cd $ HADOOP_HOME   然后   hadoop jar ./share/hadoop/mapreduce/hadoop-mapreduce-examples-..*.jar pi 2 5

如果您有错误,则MR未配置为正常工作