hdfs文件操作操作示例,包括上传文件到hdfs上、从hdfs上下载文件和删除hdfs上的文件,大家参考使用吧
import java.io.file;
import java.io.ioexception;
public class hadoopfile {
private configuration conf =null;
public hadoopfile(){
conf =new configuration();
conf.addresource(new path("/hadoop/etc/hadoop/core-site.xml"));
}
public hadoopfile(configuration conf){
this.conf =conf;
}
public boolean sendfile(string path,string localfile){
file file=new file(localfile);
if (!file.isfile()) {
system.out.println(file.getname());
return false;
}
try {
filesystem localfs =filesystem.getlocal(conf);
filesystem hadoopfs =filesystem.get(conf);
path hadpath=new path(path);
fsdataoutputstream fsout=hadoopfs.create(new path(path+"/"+file.getname()));
fsdatainputstream fsin=localfs.open(new path(localfile));
byte[] buf =new byte[1024];
int readbytes=0;
while ((readbytes=fsin.read(buf))>0){
fsout.write(buf,0,readbytes);
}
fsin.close();
fsout.close();
filestatus[] hadfiles= hadoopfs.liststatus(hadpath);
for(filestatus fs :hadfiles){
system.out.println(fs.tostring());
}
return true;
} catch (ioexception e) {
e.printstacktrace();
}
return false;
}
public boolean delfile(string hadfile){
try {
filesystem hadoopfs =filesystem.get(conf);
path hadpath=new path(hadfile);
path p=hadpath.getparent();
boolean rtnval= hadoopfs.delete(hadpath, true);
filestatus[] hadfiles= hadoopfs.liststatus(p);
for(filestatus fs :hadfiles){
system.out.println(fs.tostring());
}
return rtnval;
} catch (ioexception e) {
e.printstacktrace();
}
return false;
}
public boolean downloadfile(string hadfile,string localpath){
try {
filesystem localfs =filesystem.getlocal(conf);
filesystem hadoopfs =filesystem.get(conf);
path hadpath=new path(hadfile);
fsdataoutputstream fsout=localfs.create(new path(localpath+"/"+hadpath.getname()));
fsdatainputstream fsin=hadoopfs.open(hadpath);
byte[] buf =new byte[1024];
int readbytes=0;
while ((readbytes=fsin.read(buf))>0){
fsout.write(buf,0,readbytes);
}
fsin.close();
fsout.close();
return true;
} catch (ioexception e) {
e.printstacktrace();
}
return false;
}
}
如对本文有疑问, 点击进行留言回复!!
java.lang.UnsatisfiedLinkError:dalvik.system.PathClassLoader...couldn‘t find “xxx.so“的解决方法
荐 小白开发程序之路(2-3)Button(大小、颜色、按压事件、点击事件)
小白开发安卓程序之路(2-2)TextView(解决 点击button不跳转、跑马灯跑不起来、Android studio 启动时黑屏)
SpringCloud从入门到精通教程(一)- 服务的注册与发现(Eureka)
网友评论