Contoh operasi operasi file HDFS, termasuk mengunggah file ke HDFS, mengunduh file dari HDFS, dan menghapus file pada HDFS.
Salinan kode adalah sebagai berikut:
impor org.apache.hadoop.conf.configuration;
impor org.apache.hadoop.fs.*;
impor java.io.file;
impor java.io.ioException;
kelas publik HadoopFile {
konfigurasi pribadi conf = null;
hadoopfile publik () {
conf = konfigurasi baru ();
conf.addresource (jalur baru ("/hadoop/etc/hadoop/core-site.xml"));
}
hadoopfile publik (konfigurasi conf) {
this.conf = conf;
}
public boolean sendFile (string path, string localfile) {
File file = file baru (localfile);
if (! file.isfile ()) {
System.out.println (file.getName ());
mengembalikan false;
}
mencoba {
Filesystem localfs = filesystem.getLocal (conf);
FileSystem hadoopFs = filesystem.get (conf);
Path HadPath = Jalur Baru (Path);
FsdataoutputStream fsout = hadoopfs.create (path baru (path+"/"+file.getName ()));
FsdatainputStream fsin = localfs.open (jalur baru (localfile));
byte [] buf = byte baru [1024];
int readbytes = 0;
while ((readbytes = fsin.read (buf))> 0) {
fsout.write (buf, 0, readbytes);
}
fsin.close ();
fsout.close ();
Filestatus [] hadfiles = hadoopfs.liststatus (hadpath);
untuk (filestatus fs: hadfiles) {
System.out.println (fs.tostring ());
}
Kembali Benar;
} catch (ioException e) {
e.printstacktrace ();
}
mengembalikan false;
}
public boolean delfile (string hadfile) {
mencoba {
FileSystem hadoopFs = filesystem.get (conf);
Path Hadpath = Jalur Baru (HadFile);
Jalur p = hadpath.getParent ();
boolean rtnval = hadoopfs.delete (hadpath, true);
Filestatus [] hadfiles = hadoopfs.liststatus (p);
untuk (filestatus fs: hadfiles) {
System.out.println (fs.tostring ());
}
return rtnval;
} catch (ioException e) {
e.printstacktrace ();
}
mengembalikan false;
}
public boolean downloadfile (string hadfile, string localpath) {
mencoba {
Filesystem localfs = filesystem.getLocal (conf);
FileSystem hadoopFs = filesystem.get (conf);
Path Hadpath = Jalur Baru (HadFile);
FsdataoutputStream fsout = localfs.create (jalur baru (localpath+"/"+hadpath.getname ()));
FsDataInputStream fsin = hadoopfs.open (Hadpath);
byte [] buf = byte baru [1024];
int readbytes = 0;
while ((readbytes = fsin.read (buf))> 0) {
fsout.write (buf, 0, readbytes);
}
fsin.close ();
fsout.close ();
Kembali Benar;
} catch (ioException e) {
e.printstacktrace ();
}
mengembalikan false;
}
}