Putmerge程序的主要工作内容为:将本地文件合并到hdfs上的某一个文件中。
看似简单的问题,结果bug多多。
public class Putmerge {
public static void main(String[] args) {
Configuration conf = new Configuration();
try {
FileSystem hdfs = FileSystem.get(URI.create("hdfs://carweb94/"),conf);
conf.set("fs.file.impl", "org.apache.hadoop.fs.LocalFileSystem");
LocalFileSystem local = FileSystem.getLocal(conf);
//设定输入目录和输出目录
Path inputDir = new Path(args[0]);
Path hdfsFile = new Path(args[1]);
FSDataOutputStream out = hdfs.create(hdfsFile);
//得到本地文件列表
FileStatus[] inputFiles = local.listStatus(inputDir);
for (int i = 0; i < inputFiles.length; i++) {
FSDataInputStream in = local.open(inputFiles[i].getPath());
int readIndex = 0;
byte[] buffer = new byte[1024];
while((readIndex = in.read(buffer)) > 0) {
out.write(buffer, 0, readIndex);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
1. hdfs java.io.IOException: Mkdirs failed to create
这个问题的出现原因大致是:FileSystem.get(conf);这句实则连接的是本地文件file:///所以bug多多,
下面是某位同学辛苦总结的,并附上解决办法:修改程序: FileSystem hdfs = FileSystem.get(URI.create("hdfs://localhost:9000/"), conf);
http://blog.csdn.net/walkerjong/article/details/37763777
2.接下来你可能会抛这样的错:
Exception in thread "main" java.io.IOException: No FileSystem for scheme: file
这个得解决办法就是添加这句:conf.set("fs.file.impl", "org.apache.hadoop.fs.LocalFileSystem");
这个方法的来源及解释在这里:http://stackoverflow.com/questions/17265002/hadoop-no-filesystem-for-scheme-file
3.第三个错也许就此也上场了:
xception in thread "main" java.io.IOException: No FileSystem for scheme: hdfs
解决办法是加jar包:
hadoop-common,hadoop-hdfs,一个都不能少,听他的,没错!
来源请参考:http://heipark.iteye.com/blog/1997672
4.也许你的程序还会抛:
java.io.FileNotFoundException: XXXX (Is a directory)
很显然,这个程序没有排除子目录,请你本地的路径,也就是args【0】传一个目录,这个目录里只有文件,不能有子目录。
5.也许你的程序,她妹的还会抛错:例如
failed on connection exception: java.net.ConnectException: Connection refused
恭喜你,终于快要看到海岸了。。。这个错就是你连hdfs都没连上啊,哈哈哈,还做嘛呢。。。赶紧的,看看你的hdfs到底该写个啥才能连上吧。
出处:http://write.blog.csdn.net/postedit?ref=toolbar