Beispiele für HDFS -Dateioperationen, einschließlich Hochladen von Dateien auf HDFs, Herunterladen von Dateien von HDFs und Löschen von Dateien auf HDFs.
Die Codekopie lautet wie folgt:
import org.apache.hadoop.conf.configuration;
import org.apache.hadoop.fs.*;
Import Java.io.file;
importieren java.io.ioException;
öffentliche Klasse Hadoopfile {
private configuration conf = null;
public hadoopfile () {
conf = new configuration ();
conf.adDresource (neuer Pfad ("/Hadoop/etc/hadoop/core-site.xml");
}
public hadoopfile (Konfiguration conf) {
this.conf = conf;
}
public boolean sendFile (String Pfad, String localfile) {
Datei Datei = neue Datei (localFile);
if (! file.isfile ()) {
System.out.println (file.getName ());
false zurückgeben;
}
versuchen {
Dateisystem localfs = Dateisystem.getLocal (conf);
Dateisystem Hadoopfs = Dateisystem.get (conf);
Pfad HadPath = neuer Pfad (Pfad);
FsdataoutputStream fSout = hadoopfs.create (neuer Pfad (Pfad+"/"+file.getName ()));
FsdatainputStream fsin = localfs.open (neuer Pfad (localfile));
byte [] buf = neues 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);
für (filestatus fs: HadFiles) {
System.out.println (fs.toString ());
}
zurückkehren;
} catch (ioException e) {
E. printstacktrace ();
}
false zurückgeben;
}
public boolean delfile (String hadfile) {
versuchen {
Dateisystem Hadoopfs = Dateisystem.get (conf);
Pfad HadPath = neuer Pfad (Hadfile);
Pfad p = HadPath.getParent ();
boolean rtnval = hadoopfs.delete (hadPath, true);
Filestatus [] HadFiles = Hadoopfs.ListStatus (p);
für (filestatus fs: HadFiles) {
System.out.println (fs.toString ());
}
retnval return;
} catch (ioException e) {
E. printstacktrace ();
}
false zurückgeben;
}
public boolean downloadFile (String HadFile, String LocalPath) {
versuchen {
Dateisystem localfs = Dateisystem.getLocal (conf);
Dateisystem Hadoopfs = Dateisystem.get (conf);
Pfad HadPath = neuer Pfad (Hadfile);
FsdataoutputStream fSout = localfs.create (neuer Pfad (LocalPath+"/"+HadPath.getName ()));
FsdatainputStream fsin = hadoopfs.open (HadPath);
byte [] buf = neues byte [1024];
int readBytes = 0;
while ((readBytes = fsin.read (buf))> 0) {
fSout.write (buf, 0, readBytes);
}
fsin.close ();
fSout.close ();
zurückkehren;
} catch (ioException e) {
E. printstacktrace ();
}
false zurückgeben;
}
}