HDFs file operation examples, including uploading files to HDFs, downloading files from HDFs, and deleting files on HDFs, refer to the use of
Copy Code code as follows:
Import org.apache.hadoop.conf.Configuration;
Import org.apache.hadoop.fs.*;
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;
}
}