看到好几篇文章中提到写文件,自己尝试了一下采用DFSClient写文件,就是不成功,不知道怎么回事。
还有,以前配置0.19版本的时候,在终端执行hadoop dfs -ls打印出的是配置的文件目录,为何现在0.20版本就变成了当前操作目录吗?
以下是文件操作的代码:
Configuration conf = new Configuration();
conf.addResource(new Path("/usr/hadoop/hadoop-conf/core-site.xml"));
conf.addResource(new Path("/usr/hadoop/hadoop-conf/hdfs-site.xml"));
conf.addResource(new Path("/usr/hadoop/hadoop-conf/mapred-site.xml"));
DFSClient client = new DFSClient(conf);
OutputStream fsout = client.create("large.zip", true, (short)1, 512);
String src = "/usr/hadoop/input/bigfile.zip";
try
{
RandomAccessFile in = new RandomAccessFile(src, "r");
byte[] b = new byte[512];
int len = 0;
while((len = in.read(b)) > 0)
{
fsout.write(b, 0, len);
}
in.close();
fsout.close();
}
catch(Exception e)
{
log.error(e);
} |
|