我想将文件上传到HDFS。我使用以下jar作为依赖项编译我的代码:
我的代码:
我用Ant编译了它。但是,它给了我这个错误:No FileSystem for scheme:hdfs
。
但现在我又得到了一个错误:Class org.apache.hdfs.DistributedFileSystem not found
。
怎么了?我该怎么办?
DistributedFileSystem
是hadoop-core
的一部分。
要解决此问题,您还需要包含hadoop-core-1.2.1.jar
(注意:我正在使用Maven进行构建):
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-core</artifactId>
<version>1.2.1</version>
</dependency>
总的来说,我正在使用以下Maven依赖项:
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-mapreduce-client-core</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-core</artifactId>
<version>1.2.1</version>
</dependency>
在获取Hadoop Filesystem对象时,如下面的FileSystem fs = FileSystem.get(hdfsUrl,configuration);
如果您收到以下错误:“No FileSystem for scheme:hdfs”
您可以通过在配置上设置以下2个属性来解决此问题。
configuration.set("fs.hdfs.impl","org.apache.hadoop.hdfs.DistributedFileSystem");
configuration.set("fs.file.impl", "org.apache.hadoop.fs.LocalFileSystem");
现在,您可能会收到如下新错误:
java.lang.ClassNotFoundException: Class org.apache.hadoop.hdfs.DistributedFileSystem not found
Hadoop-common.jar使用Thread.currentThread.getContextClassLoader()和configuration.getClassLoader来加载类。因此,如果您使用设置classLoader
Thread.currentThread.setContextClassLoader(yourClassLoader);
configuration.setClassLoader(yourClassLoader);
你将能够从其他hadoop罐加载所需的类(例如hadoop-hdfs)
如果您需要更多帮助,请告诉我。如果你发现这个有用,别忘了upvote。