从Java客户端连接Hive时出现ClassNotFoundException

时间:2013-10-22 17:58:55

标签: hadoop hive

在Java客户端中,我试图在Hive中的表上执行查询。执行查询时,程序会抛出java.lang.ClassNotFoundException,详情如下。我的类路径中有hadoop-core-1.0.4.jar。 Hive版本为0.12.0,hadoop版本为1.0.4。我无法解决问题。会是什么呢?下面是代码片段和错误。

public static String getDummyData() {
    String result = "";
    String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";
    try {
        Class.forName(driverName);
    } catch (ClassNotFoundException e) {
        log.error("Could not find driver", e);
    }
    try {
        Connection conn = DriverManager.getConnection("jdbc:hive://noon102:10000/default", "","");
        String tableName = "user";
        String query = "select * from " + tableName + " where id = ?";
        PreparedStatement ipMacSt = conn.prepareStatement(query);
        ipMacSt.setInt(1, 6);

        ResultSet res = ipMacSt.executeQuery();
        while (res.next()) {
            result = result + res.getString(2) + "\n";
        }
    } catch (Exception e) {
    log.error("Could not execute the query", e);
    }

    return result;
}

Caused by: java.lang.NoClassDefFoundError: org/apache/hadoop/io/Writable
        at org.apache.hadoop.hive.jdbc.HiveQueryResultSet.initSerde(HiveQueryResultSet.java:101)
        at org.apache.hadoop.hive.jdbc.HiveQueryResultSet.<init>(HiveQueryResultSet.java:66)
        at org.apache.hadoop.hive.jdbc.HiveStatement.executeQuery(HiveStatement.java:198)
        at org.apache.hadoop.hive.jdbc.HiveStatement.execute(HiveStatement.java:132)
        at org.apache.hadoop.hive.jdbc.HiveConnection.configureConnection(HiveConnection.java:133)
        at org.apache.hadoop.hive.jdbc.HiveConnection.<init>(HiveConnection.java:122)
        at org.apache.hadoop.hive.jdbc.HiveDriver.connect(HiveDriver.java:106)
        at java.sql.DriverManager.getConnection(DriverManager.java:582)
        at java.sql.DriverManager.getConnection(DriverManager.java:185)
        at com.**.services.HadoopOperations.getDummyData(HadoopOperations.java:90)
        ... 15 more
Caused by: java.lang.ClassNotFoundException: org.apache.hadoop.io.Writable
        at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:513)
        at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:429)
        at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:417)
        at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:107)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
        ... 25 more

1 个答案:

答案 0 :(得分:2)

偶然发现,我遇到了同样的问题,使用3个Maven依赖项修复了它:

 <dependency>
    <groupId>org.apache.hadoop</groupId>
    <artifactId>hadoop-core</artifactId>
    <version>0.20.0</version>
 </dependency>
 <dependency>
    <groupId>org.apache.hive</groupId>
    <artifactId>hive-exec</artifactId>
    <version>0.7.1</version>
 </dependency>
 <dependency>
    <groupId>org.apache.hive</groupId>
    <artifactId>hive-jdbc</artifactId>
    <version>0.7.1</version>
 </dependency>

如果你不喜欢Maven,只需从Maven中央存储库下载JAR并将它们添加到你的构建路径中。希望这也适合你!