HDFS에 파일 업로드, HDFS에서 파일 다운로드 및 HDF에서 파일 삭제를 포함하여 HDFS 파일 작업 작업의 예는 참조하십시오.
코드 사본은 다음과 같습니다.
import org.apache.hadoop.conf.configuration;
import org.apache.hadoop.fs.*;
import java.io.file;
import java.io.ioexception;
공개 클래스 hadoopfile {
개인 구성 conf = null;
public hadoopfile () {
conf = 새로운 구성 ();
conf.addresource (새 경로 ( "/hadoop/etc/hadoop/core-site.xml"));
}
공개 hadoopfile (구성 conf) {
this.conf = conf;
}
public boolean sendfile (String Path, String localfile) {
파일 = 새 파일 (localfile);
if (! file.isfile ()) {
System.out.println (file.getName ());
거짓을 반환합니다.
}
노력하다 {
FileSystem localfs = fileSystem.getLocal (conf);
FileSystem hadoopfs = filesystem.get (conf);
경로 hadpath = 새로운 경로 (경로);
fsdataOutputStream fsout = hadoopfs.create (new Path (path+"/"+file.getName ()));
fsdatainputstream fsin = localfs.open (new Path (localfile));
바이트 [] buf = 새로운 바이트 [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 ());
}
진실을 반환하십시오.
} catch (ioexception e) {
e.printstacktrace ();
}
거짓을 반환합니다.
}
공개 부울 델리 테 (String Hadfile) {
노력하다 {
FileSystem hadoopfs = filesystem.get (conf);
경로 hadpath = 새로운 경로 (hadfile);
경로 p = hadpath.getParent ();
부울 rtnval = hadoopfs.delete (hadpath, true);
filestatus [] hadfiles = hadoopfs.liststatus (p);
for (filestatus fs : hadfiles) {
System.out.println (fs.toString ());
}
rtnval을 반환합니다.
} catch (ioexception e) {
e.printstacktrace ();
}
거짓을 반환합니다.
}
공개 부울 다운로드 파일 (String Hadfile, String LocalPath) {
노력하다 {
FileSystem localfs = fileSystem.getLocal (conf);
FileSystem hadoopfs = filesystem.get (conf);
경로 hadpath = 새로운 경로 (hadfile);
fsdataOutputStream fsout = localfs.create (new Path (localPath+"/"+hadpath.getName ()));
fsdatainputstream fsin = hadoopfs.open (hadpath);
바이트 [] buf = 새로운 바이트 [1024];
int readbytes = 0;
while ((ReadBytes = fsin.read (buf))> 0) {
fsout.write (buf, 0, ReadBytes);
}
fsin.close ();
fsout.close ();
진실을 반환하십시오.
} catch (ioexception e) {
e.printstacktrace ();
}
거짓을 반환합니다.
}
}