2

在 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
4

1 回答 1

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 并将它们添加到您的构建路径。希望这对你也有效!

于 2013-12-13T23:04:16.647 回答