HDFS是一个分布式文件系统,然而对于程序员来说,HDFS就是一个普通文件系统,Hadoop进行的底层封装,程序员按照相应的API来对HDFS上的文件操作,和对本地磁盘文件操作没有太多区别。但是最初接触时可能还是会碰到这样那样的问题。
例如:获取FileSystem实例时会出现
java.lang.NullPointerException
at org.apache.hadoop.conf.Configuration.get(Configuration.java:382)
at org.apache.hadoop.conf.Configuration.getBoolean(Configuration.java:570)
at org.apache.hadoop.fs.FileSystem.get(FileSystem.java:192)
at hadoop.test.URLCat.copyFileToAnotherFile(URLCat.java:38) //这个是我写的一个方法,报错了
at hadoop.test.URLCat.main(URLCat.java:83)
代码:
package hadoop.test;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.FsUrlStreamHandlerFactory;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hdfs.DistributedFileSystem;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.util.Progressable;
public class URLCat extends Configured {
/×static{
Configuration.addDefaultResource("hdfs-default.xml");
Configuration.addDefaultResource("hdfs-site.xml");
Configuration.addDefaultResource("mapred-default.xml");
Configuration.addDefaultResource("mapred-site.xml");
} ×/没有这个static块时就会报上面对错误
public void copyFileToAnotherFile(String[] args)
{
InputStream in = null;
OutputStream out = null;
try {
String sourceFile = args[0];
String targetFile = args[1];
in = new BufferedInputStream(new FileInputStream(sourceFile));
Configuration conf = new Configuration();
System.out.println(conf);
System.out.println(URI.create(targetFile)==null);
System.out.println(conf==null);
System.out.println(FileSystem.get(URI.create(targetFile),conf)==null);
FileSystem fs = DistributedFileSystem.get(URI.create(targetFile),conf);
System.out.println(fs);
out = fs.create(new Path(targetFile),new Progressable(){
public void progress(){System.out.print(".");}
});
IOUtils.copyBytes(in, out, 4096,true);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally
{
IOUtils.closeStream(in);
IOUtils.closeStream(out);
}
}
static {
URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
}
public static void displayFile(String[] args)
{
InputStream in = null;
try {
in = new URL(args[0]).openStream();
IOUtils.copyBytes(in, System.out, 4096,false);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally
{
IOUtils.closeStream(in);
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new URLCat().copyFileToAnotherFile(args);
//URLCat.displayFile(args);
//
}
}
原因:Configuration似乎只会加载基本的两个文件,所以需要将其它配置文件手动导入