HDFSへのファイルのアップロード、HDFSからのファイルのダウンロード、HDFSの削除など、HDFSファイル操作の例を参照してください。
コードコピーは次のとおりです。
Import org.apache.hadoop.conf.configuration;
org.apache.hadoop.fs。*;
java.io.fileをインポートします。
java.io.ioexceptionをインポートします。
パブリッククラスのhadoopfile {
プライベート構成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());
falseを返します。
}
試す {
filesystem localfs = filesystem.getLocal(conf);
ファイルシステムhadoopfs = filesystem.get(conf);
パスはpath = 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());
}
trueを返します。
} catch(ioException e){
e.printstacktrace();
}
falseを返します。
}
public boolean delfile(string hadfile){
試す {
ファイルシステムhadoopfs = filesystem.get(conf);
パスhadpath = new Path(hadfile);
パスp = hadpath.getParent();
boolean 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();
}
falseを返します。
}
パブリックブールダウンロードファイル(String Hadfile、String LocalPath){
試す {
filesystem localfs = filesystem.getLocal(conf);
ファイルシステムhadoopfs = filesystem.get(conf);
パス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();
trueを返します。
} catch(ioException e){
e.printstacktrace();
}
falseを返します。
}
}