Hadoop Problem : Wrong FS: hdfs://localhost:9000/output, expected: file:///

来源:互联网 发布:微信商城源码 编辑:程序博客网 时间:2024/05/22 15:13

照着《Hadoop实战》写了一个程序:

package my;import java.io.IOException;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.FSDataInputStream;import org.apache.hadoop.fs.FSDataOutputStream;import org.apache.hadoop.fs.FileStatus;import org.apache.hadoop.fs.FileSystem;import org.apache.hadoop.fs.Path;public class PutMerge {/** * @param args * @throws IOException  */public static void main(String[] args) throws IOException {Path inputDir = new Path("C:\\hjfiles\\tmpdir");Path hdfsFile = new Path("hdfs://localhost:9000/output/merge");Configuration conf = new Configuration();FileSystem hdfs = FileSystem.get(conf);<span style="white-space:pre"></span>//问题出在这。FileSystem local = FileSystem.getLocal(conf);FileStatus[] inputFiles = local.listStatus(inputDir);FSDataOutputStream out = hdfs.create(hdfsFile);for (FileStatus fileStatus : inputFiles) {FSDataInputStream in = local.open(fileStatus.getPath());byte[] buffer = new byte[256];int byteRead = 0;while((byteRead = in.read(buffer))>0){out.write(buffer, 0, byteRead);}in.close();}}}

在本地伪分布式环境下开发运行这个程序时,报错:

Exception in thread "main" java.lang.IllegalArgumentException: Wrong FS: hdfs://localhost:9000/output, expected: file:///at org.apache.hadoop.fs.FileSystem.checkPath(FileSystem.java:310)at org.apache.hadoop.fs.RawLocalFileSystem.pathToFile(RawLocalFileSystem.java:47)at org.apache.hadoop.fs.RawLocalFileSystem.mkdirs(RawLocalFileSystem.java:301)at org.apache.hadoop.fs.ChecksumFileSystem.mkdirs(ChecksumFileSystem.java:470)at org.apache.hadoop.fs.ChecksumFileSystem.create(ChecksumFileSystem.java:365)at org.apache.hadoop.fs.FileSystem.create(FileSystem.java:484)at org.apache.hadoop.fs.FileSystem.create(FileSystem.java:465)at org.apache.hadoop.fs.FileSystem.create(FileSystem.java:372)at org.apache.hadoop.fs.FileSystem.create(FileSystem.java:364)at my.PutMerge.main(PutMerge.java:29)


原因:

在定义一个FileSystem变量的时候伪分布式和单机版的方法是不一样的,单机版使用的是FileSystem类的静态函数

<span style="font-size:12px;">FileSystem hdfs = FileSystem.get(conf)  </span>

伪分布式下需要使用Path来获得

<span style="font-size:12px;">Path dstDir = new Path("hdfs://localhost:9000/home/hadoop/hadoop");  FileSystem hdfs = dstDir.getFileSystem(getConf());  </span>



解决:

法一:

//FileSystem hdfs = FileSystem.get(conf);FileSystem hdfs = hdfsFile.getFileSystem(conf);//FileSystem local = FileSystem.getLocal(conf);FileSystem local = inputDir.getFileSystem(conf);

法二:

将hadoop目录中的conf文件夹中的hdfs-site.xml与core-site.xml复制到你的项目的目录之下 (试过,没生效,可能是路径什么的问题)


0 0
原创粉丝点击